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中的方法
//写验证逻辑
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值