struts2表单校验

表单校验
(1)自己写validate方法
a.Action组件继承ActionSupport
b.编写validate或validateXxxx方法
c.修改struts.xml,为<action>元素添加<result name="input">视图
d.JSP可使用标签或EL显示提示信息 ${errors.name }
(2)使用XML配置校验(了解)
 
 
1、在页面中用<s:fielderror></s:fielderror>标签显示错误信息,
但是该标签必须写在form中
2、在action中写validate方法
//这种所有的都校验
public void validate() {
if(this.username.equals("")){
this.addFieldError("username", "用户名不能为空");
}
if(this.password.equals("")){
this.addFieldError("password", "密码不能为空");
}else if(this.password.length()<6){
this.addFieldError("password", "密码不能小于6个字符");
}
}
//会存在errors中
其中的"username","password"和表单中的name的属性对应
3、在配置文件中:
<result name="input">validate.jsp</result> input是固定写法
4.在jsp页面中输出错误信息${errors.username}或<s:fielderror />
 
在第二步中,也可以怎么写:
public void validateXxx(){ xxx为当前请求的action中的方法
//写验证逻辑
}

猜你喜欢

转载自h496950806.iteye.com/blog/2041579