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
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 jquery 打造无限层次的树
- ajax交互Struts2的action(客户端/服务器端)
- struts2 spring整合fieldError问题
- Struts2的s:radio标签使用及用jquery添加change事件
- Struts2访问servlet分享
- Java(基于Struts2) 分页实现代码
- 从搭建Struts2 开发环境说起
- 关于国际化、OGNL表达式语言
- 在Struts2中如何将父类属性序列化为JSON格式的解决方法
- 在Struts2中的结果集类型
- 关于Struts2的类型转换详解
- 搭建EXTJS和STRUTS2框架(ext和struts2简单实例)
- 关于Struts2文件上传与自定义拦截器
- JavaWeb中struts2实现文件上传下载功能实例解析
- Struts2的学习笔记(2)
- Struts2的学习笔记(3)
- Struts2的学习笔记(4)
- iterator 标签的使用
- struts2注解配置注意事项
- 使用open flash chart制作报表