Fork me on GitHub

Struts2(十七)验证框架二

一、实现注册验证

package com.pb.entity;

import java.util.Date;



/**
 * 用户实体类
 *
 */
public class User {
    /**
     * 住址
     */
    private String address;
    /**
     * 年龄
     */
    private Integer age;
    
    /**
     * 邮箱
     */
    private String email;
    
    /**
     * 密码
     */
    private String password;
    
    /**
     * 电话或者手机 
     */
    private String phone;
    
    /**
     * 用户名
     */
    private String username;

    /**
     * 出生日期
     * 
     */
    private Date brithDate;
    
    
public String getAddress() {
    return address;
}

    public Integer getAge() {
        return age;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    public String getPhone() {
        return phone;
    }

    /**
     * 与之对应的getter和setter方法
     * 
     */
        public String getUsername() {
            return username;
        }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBrithDate() {
        return brithDate;
    }

    public void setBrithDate(Date brithDate) {
        this.brithDate = brithDate;
    }

}

Action

package com.pb.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.pb.entity.User;

public class UserAction extends ActionSupport {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * 用户
     */
    private User user;
    private String repassword;

    /**
     * 注册验证方法
     * 
     */
    public String Register(){
        System.out.println("完成注册");
        
        return SUCCESS;
    }

    public User getUser() {
        return user;
    }


    public void setUser(User user) {
        this.user = user;
    }

    public String getRepassword() {
        return repassword;
    }

    public void setRepassword(String repassword) {
        this.repassword = repassword;
    }
    

}

 

页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<sx:head parseContent="true"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="registor.action" method="post">
用户名:<input type="text" name="user.username" /><br/>
密码:<input type="password" name="user.password" /><br/>
确认密码:<input type="password" name="repassword" /><br/>
年龄:<input type="text" name="user.age" /><br/>
电话或者手机:<input type="text" name="user.phone" /><span>例如:电话:0755-88888888或者手机:13800138000</span><br/>
邮箱:<input type="text" name="user.email" /><br/>
住址:<textarea rows="5" cols="20" name="user.address" ></textarea><br/>

出生日期:<input type="text"  name="user.brithDate"/><br/><span>例如:1970-01-01</span>
<%-- <sx:datetimepicker name="user.brithDate"/><br/> --%>
<input type="submit" value="注册" /><input type="reset" value="重置" />
</form>
<s:fielderror />

</body>
</html>

 

UserAction-registor-validation.xml

<?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>
 <!--  用户名-->
 <field name="user.username">
 <field-validator type="requiredstring">
 <param name="trim">true</param>
 <message>用户名不能为空!</message>
 </field-validator>
 
 <field-validator type="stringlength">
 <param name="trim">true</param>
 <param name="minLength">4</param>
  <param name="maxLength">16</param>
  <message>用户名必须为${minLength} 至${maxLength} 之间,当前为${user.username}</message>
 </field-validator>
 </field>
 <!-- 用户密码 -->
 <field name="user.password">
 <field-validator type="requiredstring">
 <param name="trim">true</param>
 <message>密码不能为空</message>
 </field-validator>
 <field-validator type="stringlength">
 <param name="minLength">4</param>
 <param name="maxLength">20</param>
 <message>密码必须为${minLength} 至${maxLength} 之间,当前为${user.password}</message>
 </field-validator>
 </field>
 <!-- 确认密码 -->
 <field name="repassword">
 <field-validator type="requiredstring">
 <param name="trim">true</param>
 <message>确认密码不能为空!</message>
 </field-validator>
 <field-validator type="fieldexpression">
 <param name="expression">repassword eq user.password</param>
 <message>确认密码必须与密码输入一致!</message>
 </field-validator>
 </field>
 <!--年龄  -->
 <field name="user.age">
 <field-validator type="required">
 <param name="trim">true</param>
 <message>年龄不能为空</message>
 </field-validator>
 <field-validator type="int">
 <param name="min">1</param>
 <param name="max">200</param>
 <message>年龄必须在${min} 至${max}之间,当前为${user.age}</message>
 </field-validator>
 </field>
 <!-- 电话user.phone -->
 <field name="user.phone">
 <field-validator type="requiredstring">
 <param name="trim">true</param>
 <message>电话号码不能为空!</message>
 </field-validator>
 <field-validator type="regex">
 <param name="regex">^((\d{3,4}-){0,1}(\d{7,8}))|(\d{11})$</param>
 <message>电话号码或者手机号格式不正确!</message>
 </field-validator>
 </field>
 <!--邮箱验证  -->
 <field name="user.email">
 <field-validator type="requiredstring">
 <param name="trim">true</param>
 <message>邮箱不能为空!</message>
 </field-validator>
 <field-validator type="email">
 <param name="email"></param>
 <message>邮箱格式不正确!</message>
 </field-validator>
 </field>
 <!--  住址-->
 <field name="user.address">
 <field-validator type="requiredstring">
 <param name="trim">true</param>
 <message>住址不能为空!</message>
 </field-validator>
 <field-validator type="stringlength">
 <param name="minLength">5</param>
  <param name="maxLength">200</param>
  <message><![CDATA[地址长度为${minLength} 至${maxLength} 之间,当前为${user.address}]]></message>
 </field-validator>
 </field>
 <!-- 出生日期 -->
 <field name="user.brithDate">
 <field-validator type="required">
 <param name="trim">true</param>
 <message>出生日期不能为空</message>
 </field-validator>
 <field-validator type="regex">
 <param name="regex">^\d{2,4}-\d{1,2}-\d{2,4}$
</param>

 <message>日期格式不正确!</message>
 </field-validator>
 </field>
 </validators>

 

posted @ 2015-04-06 20:54  森林森  阅读(713)  评论(6编辑  收藏  举报