+ -
当前位置:首页 → 问答吧 → Struts中execute方法怎么得到页面传过来的值

Struts中execute方法怎么得到页面传过来的值

时间:2011-12-01

来源:互联网


XMLInfo xml = aMapping.get(path);
String username=request.getParameter("username");
// String password=request.getParameter("password");
 
if(xml!=null){
String className = xml.getClassName();
try {
Class cls =Class.forName(className);
//设置action类当中所有属性的值
Method method = cls.getMethod("execute",new Class[]{null});//这里怎样把参数传到action中的execute方法中

String result = (method.invoke(cls.newInstance(), null)).toString();
//把值传递到前台
//System.out.println(xml.getResult().get(resultName));
System.out.println(result);
request.getRequestDispatcher(xml.getResult().get(result).toString()).forward(request, response);



public String execute(){

return "error.jsp";

}
这个方法为什么老是返回error啊

还有那个struts.xml文件为什么要放在tomcat下面才可以解悉

作者: hechao492240640   发布时间: 2011-12-01

我表示完全没看懂你啥意思 你这个是个Action吧 如果是action,就更简单了,struts会直接把你传过来的参数直接赋给action对应的成员变量。你直接在action里定义对应的变量,然后写好getter和setter就行了。

作者: dokia123   发布时间: 2011-12-02

Method method = cls.getMethod("execute",new Class[]{null});//这里是获取名称为
execute的方法
String result = (method.invoke(cls.newInstance(), null)).toString();//这里是执行execute方法

这里是通过反射调用的execute,所以应该你得看execute的源码是怎么写的哦

作者: wangxvwang   发布时间: 2011-12-02

引用 2 楼 wangxvwang 的回复:
Method method = cls.getMethod("execute",new Class[]{null});//这里是获取名称为
execute的方法
String result = (method.invoke(cls.newInstance(), null)).toString();//这里是执行execute方法

这里是通过反射调用的execute,所以应该你得看exec……

看错你意思了,参数是在invoke(cls.newInstance(), null)里面传的,你写的null,其实如果没有参数你还可以试一下invoke(cls.newInstance()),null的位置就是你要传的参数数组

作者: wangxvwang   发布时间: 2011-12-02

struts1.x 中用request.getParameter("paramName");

struts2.x 中直接在action中声明好页面上将要传过来的值对应属性,然后再页面的标签中name的名字和action中的属性名对应,这样struts2的核心控制器会将值放到对应的属性上,你在action中直接用属性就好了,不用再作多余的操作。

作者: peng_hao1988   发布时间: 2011-12-02

写的不错啊!!!!!

作者: houyibiao   发布时间: 2011-12-02

引用 1 楼 dokia123 的回复:
我表示完全没看懂你啥意思 你这个是个Action吧 如果是action,就更简单了,struts会直接把你传过来的参数直接赋给action对应的成员变量。你直接在action里定义对应的变量,然后写好getter和setter就行了。

+1

作者: happyking999   发布时间: 2011-12-02