`
kuyuyingzi
  • 浏览: 53570 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

struts2的验证框架

 
阅读更多
1、校验,struts2提供的多种校验方式:
*验证Action中全部方法
在Action同包下,创建:Action类名-validation.xml如:LoginAction创建LoginAction-validation.xml
在这里LoginAction只有一个方法那就是login方法,当我们访问login方法的时候,struts2中的验证拦截器
就会加载LoginAction-validation.xml的配置文件进行验证
局限:如果action有其他方法,当我们访问这些方法,拦截器一样会进行验证操作,然后就会跳到input指定的错误页面;
而我们的需求是这些方法不用进行验证,于是就有了下面这个方法
*验证Action中单个方法
每个方法单独配置一个validation.xml
具体:在Action同包下,创建:Action类名-action方法对应的名称-validation.xml
例如:<action name="userAction_*" method="{1}" class="struts2.action.UserAction">
要创建UserAction-userAction_doAdd-validation.xml,userAction_doAdd为访问这个action方法的路径




2、struts2 中同一个action的实现中对应多个input的处理方法
(1)解决方法:在方法上加上
@InputConfig(resultName = "addInput")/@InputConfig(methodName="addInput")
public String doAdd(){
@InputConfig(resultName = "editInput")
public String doEdit(){
(2)然后在struts.xml配置文件中添加:
<result name="input">WEB-INF/user/error.jsp</result>
<result name="addInput">WEB-INF/user/add.jsp</result>
<result name="editInput">WEB-INF/user/edit.jsp</result>
(3)原理:
之所以通过上面的方式可以解决,主要如下的几个实现
com.opensymphony.xwork2.interceptor.annotations.InputConfig
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor#doIntercept


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics