您的位置:首页 > 编程语言 > Java开发

struts2 中同一个action的实现中对应多个input的处理方法

2013-10-04 00:00 615 查看
在struts2中可能会遇到如下的情形:

public class MyAction extends Action{

public String method1(){

return SUCCESS;

}

public String method2(){

return SUCCESS;

}

}

上 面这段代码包含两个方法method1, method2,并且这两个方法都会受到validate interceptor的影响,会存在校验失败的情况存在,因为struts2中如果校验失败的话会默认的返回到INPUT指定的视图,但情况是我们可能 根据不同的方法返回到不同的视图,这种情况怎么处理呢?

在校验的XML配置中我还真是没有找到处理方法,但可以通过annotation来进行解决。

1、解决办法如下:

public class MyAction extends Action{

@InputConfig(resultName="InputresultName1")

public String method1(){

return SUCCESS;

}

// 下面这个的最终结果类似@InputConfig(resultName="method2_input")

@InputConfig(methodName="inputMethodName2")

public String method2(){

return SUCCESS;

}

public String inputMethodName2(){

return "method2_input";

}

}

然后在struts.xml配置文件中添加:
<result name="input">/a.jsp</result>

<result name="method2_input">/b.jsp</result>

2、原理

之所以通过上面的方式可以解决,主要如下的几个实现

com.opensymphony.xwork2.interceptor.annotations.InputConfig

com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor#doIntercept
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts2 多个input