Struts2的表单数据验证详解编程语言

Struts2的action对表单数据的验证有多种方式,可以通过覆写 validate()方法进行对该action的所有方法进行验证,也可以通过validateXxx()方法对action中指定的xxx()方法进行验证,也可以通过配置xml文件对其进行验证。

1, 覆写 validate()进行验证,action类必须继承ActionSupport,此方法将在执行指定方法前执行。

package com.my.action; 
 
import org.apache.commons.lang3.StringUtils; 
 
import com.my.bean.User; 
import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.ModelDriven; 
 
public class UserAction extends ActionSupport implements ModelDriven<User>{ 
 
    private User user = new User();  
 
    @Override 
    public String execute() throws Exception { 
 
        return SUCCESS; 
    }    
 
    public String login() throws Exception { 
 
        return SUCCESS; 
    }    
 
    @Override 
    public void validate() { 
        if( StringUtils.isEmpty(user.getUsername())){ 
            this.addFieldError("username", "用户名不能为空"); 
        } 
 
        if( StringUtils.isEmpty(user.getPassword())){ 
            this.addFieldError("password", "密码不能为空"); 
        } 
    } 
 
    @Override 
    public User getModel() { 
        return this.user; 
    } 
 
 
} 

2 ,通过validateXxx()方法对指定的xxx()方法进行验证,action类必须继承ActionSupport

package com.my.action; 
 
import org.apache.commons.lang3.StringUtils; 
 
import com.my.bean.User; 
import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.ModelDriven; 
 
public class UserAction extends ActionSupport implements ModelDriven<User>{ 
 
    private User user = new User();  
 
    @Override 
    public String execute() throws Exception { 
 
        return SUCCESS; 
    }    
 
    public String login() throws Exception { 
 
        return SUCCESS; 
    }    
 
    public void validateLogin() { 
        if( StringUtils.isEmpty(user.getUsername())){ 
            this.addFieldError("username", "用户名不能为空"); 
        } 
 
        if( StringUtils.isEmpty(user.getPassword())){ 
            this.addFieldError("password", "密码不能为空"); 
        } 
    } 
 
    @Override 
    public User getModel() { 
        return this.user; 
    } 
 
 
} 

3, 通过配置xml文件进行验证:
jsp文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
    <s:fielderror></s:fielderror> 
    <h3>struts标签</h3> 
    <s:form namespace="/" action="userAction" method="post" theme="simple">   
        用户名 : <s:textfield name="username"></s:textfield> <br/> 
        密码 : <s:password name="password" showPassword="true"></s:password> <br/> 
        确认密码 : <s:password name="repassword"  showPassword="true"></s:password> <br/> 
        生日 : <s:textfield name="birthday" ></s:textfield> <br/> 
        年龄 : <s:textfield name="age" ></s:textfield> <br/> 
        邮箱 : <s:textfield name="email" ></s:textfield> <br/> 
        <s:submit value="提交"></s:submit> 
    </s:form> 
 
</body> 
</html> 

action类:

public class UserAction extends ActionSupport implements ModelDriven<User> { 
 
    private User user = new User(); 
 
    @Override 
    public String execute() throws Exception { 
        System.out.println("execute" + user) ; 
        return SUCCESS; 
    } 
    public String login() throws Exception { 
        System.out.println("login" + user) ; 
        return SUCCESS; 
    } 
 
    @Override 
    public User getModel() { 
        return this.user; 
    } 
 
} 

xml文件的配置,
文件名为:action类名-validation.xml (eg: UserAction-validation.xml),
如果是对action中单个方法进行验证,
文件名为:Action类名-在xml配置Action名称-validation.xml(eg:UserAction-userAction-validation.xml)
文件位置放置在与该action同包的位置。

 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE validators PUBLIC 
"-//Apache Struts//XWork Validator 1.0.3//EN" 
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> 
<validators> 
<!-- username --> 
<field name="username"> 
<!-- 必填 --> 
<field-validator type="requiredstring"> 
<message key="usernameRequiredString"></message> 
</field-validator> 
<!-- 长度 --> 
<field-validator type="stringlength"> 
<param name="maxLength">18</param> 
<param name="minLength">6</param> 
<message><![CDATA[用户必须填写${minLength}-${maxLength}]]></message> 
</field-validator> 
</field> 
<!-- 密码 password--> 
<field name="password"> 
<!-- 必填 --> 
<field-validator type="requiredstring"> 
<message><![CDATA[密码必须填写 execute]]></message> 
</field-validator> 
<!-- 长度  A-Za-z0-9_-  或 /w- --> 
<field-validator type="regex"> 
<!-- 正在表达式的内容 --> 
<param name="regexExpression"><![CDATA[^[/w-]{6,12}$]]></param> 
<message><![CDATA[密码必须6-12,字母数字和下划线的组合]]></message> 
</field-validator> 
</field> 
<!-- 确认密码 --> 
<field name="repassword"> 
<field-validator type="fieldexpression"> 
<param name="expression"><![CDATA[password == repassword]]></param> 
<message><![CDATA[密码 与确认密码不一致]]></message> 
</field-validator> 
</field> 
<!-- 生日 --> 
<field name="birthday"> 
<field-validator type="date"> 
<param name="max">2014-01-01</param> 
<param name="min">1990-01-01</param> 
<message><![CDATA[日期必须在${min} - ${max}]]></message> 
</field-validator> 
</field> 
<!-- 邮箱 --> 
<field name="email"> 
<field-validator type="email"> 
<message>邮箱格式不正确</message> 
</field-validator> 
</field> 
<!-- 年龄 --> 
<field name="age"> 
<field-validator type="int"> 
<param name="max">120</param> 
<param name="min">23</param> 
<message><![CDATA[日期必须在${min} - ${max}]]></message> 
</field-validator> 
</field>     
</validators>

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/12075.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论