你的位置:首页 > Java教程

[Java教程]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.

<?^\d{2,4}-\d{1,2}-\d{2,4}$</param> <message>日期格式不正确!</message> </field-validator> </field> </validators>