刚遇到的问题,在网上找了些资料,然后自己试验了后,总结有三个方法:
1、actionform里加入
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
String method=request.getParameter("method");
if(method.equals("addUser"))
return super.validate(mapping, request);
else
return null;
}
2、struts-config。xml中加入
validate="false"
令struts-config.xml中的validate=false,然后在自己的action中需要校验的地方追加以下代码:
ActionMessages errors = form.validate(mapping, request);//在需要的地方调用validate()方法,其中validate()方法的校验规则已经定义在validation.xml中了
if(!errors.isEmpty()){
saveErrors(reqeust, errors);
return mapping.getInputForward();
}
3、加入多个aciton 映射
<action path="/removeUserAction" type="UserAction" parameter="method"/>
<action path="/saveUserAction" type="UserAction" paramert="method"/>
然后在validation.xml写:
<form name="/removeUserAction">
<filed ....../>
</form>
先写着。。以后再补充详细的。。。继续完成项目了。。O(∩_∩)O
本文介绍了三种在Struts2框架中实现表单验证的方法:通过ActionForm自定义验证、修改struts-config.xml配置文件以及利用多个Action映射配合validation.xml进行细粒度控制。

2797

被折叠的 条评论
为什么被折叠?



