图示:
User 实体类
package lesson10;
import java.util.Date;
public class User {
// 用户id
private Long userid;
// 用户名
private String username;
// 用户密码
private String password;
// 再输一次密码
private String rePassword;
// 年龄
private Integer age;
// 生日
private Date birth;
// 身高
private Double height;
// 电子邮件
private String email;
public Long getUserid() {
return userid;
}
public void setUserid(Long userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRePassword() {
return rePassword;
}
public void setRePassword(String rePassword) {
this.rePassword = rePassword;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Register 控制器
package lesson10;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class Register extends ActionSupport implements SessionAware {
private static final long serialVersionUID = -1035041460500572216L;
private User user;
private Map<String, Object> session;
// 执行登录的方法
public String execute() {
// 将登录的用户对象存放在session中
session.put("user", user);
return SUCCESS;
}
// 实现 SessionAware 接口
public void setSession(Map<String, Object> session) {
this.session = session;
}
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
}
Register-register-validation.xml 校验器
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">
<validators>
<!-- 校验用户id -->
<field name="user.userid">
<!-- 非字符串类型用 required 来表示必填 -->
<field-validator type="required">
<message>用户id是必填项。</message>
</field-validator>
<field-validator type="long">
<param name="min">10000000</param>
<param name="max">99999999</param>
<message>
用户id必须在 ${min} 至 ${max} 之间, 当前值为 ${user.userid}。
</message>
</field-validator>
</field>
<!-- 校验用户名 -->
<field name="user.username">
<!-- 字符串类型用 required 来表示必填 -->
<field-validator type="requiredstring">
<message>用户名是必填项。</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">4</param>
<param name="maxLength">20</param>
<param name="trim">true</param>
<message>用户名的长度必须在 ${minLength} 至 ${maxLength} 之间。</message>
</field-validator>
</field>
<!-- 校验用户密码 -->
<field name="user.password">
<field-validator type="requiredstring">
<message>用户密码是必填项。</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">8</param>
<param name="maxLength">20</param>
<param name="trim">true</param>
<message>密码的长度必须在 ${minLength} 至 ${maxLength} 之间。</message>
</field-validator>
</field>
<!-- 校验用户密码 -->
<field name="user.rePassword">
<field-validator type="fieldexpression">
<!-- 使用OGNL表达式判断再次密码是否输入一致。 -->
<param name="expression"><![CDATA[ user.password == user.rePassword ]]></param>
<message>两次密码不一致。</message>
</field-validator>
</field>
<!-- 校验用户年龄 -->
<field name="user.age">
<field-validator type="int">
<param name="min">14</param>
<param name="max">60</param>
<message>
年龄必须在 ${min} 至 ${max} 之间, 当前值为 ${user.age}。
</message>
</field-validator>
</field>
<!-- 校验用户生日 -->
<field name="user.birth">
<field-validator type="date">
<param name="min">1980-01-01</param>
<param name="max">2010-12-31</param>
<message>生日必须为合法格式 yyyy-MM-dd 且必须在 1980-01-01 至 2010-12-31 之间。</message>
</field-validator>
</field>
<!-- 校验用户身高 -->
<field name="user.height">
<field-validator type="double">
<!--
minInclusive,maxInclusive 指的是包括边界值
minExclusive,maxExclusive 指的是不包括边界值
即开区间与闭区间
-->
<param name="minInclusive">0.0</param>
<param name="maxInclusive">300.0</param>
<message>
身高必须在 ${minInclusive}cm 至 ${maxInclusive}cm 之间, 当前值为
${user.height}cm。
</message>
</field-validator>
</field>
<!-- 校验email -->
<field name="user.email">
<!-- email的规则struts2是内置的 -->
<!-- 但这个例子是想用正则表达式来校验email -->
<field-validator type="regex">
<param name="expression">
(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)
</param>
<message>email格式不正确</message>
</field-validator>
</field>
</validators>
ClientValidate 控制器
package lesson10;
import com.opensymphony.xwork2.ActionSupport;
public class ClientValidate extends ActionSupport {
private static final long serialVersionUID = -1035041460500572216L;
private String name;
private int age;
private String answer;
// 执行客户端校验的方法
public String execute() {
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
ClientValidate-validation.xml 校验器
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring">
<message>名称不允许为空。</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">18</param>
<param name="max">45</param>
<message>
年龄必须为18至45周岁之间。
</message>
</field-validator>
</field>
<field name="answer">
<field-validator type="requiredstring">
<message>答案不允许为空。</message>
</field-validator>
</field>
</validators>
validation.properties 文档
user.userid.requried=\u7528\u6237ID\u4E0D\u80FD\u4E3A\u7A7A\!
user.username.long=\u7528\u6237ID\u7684\u957F\u5EA6\u5FC5\u987B\u5728 ${min} \u81F3 ${max} \u4E4B\u95F4\uFF0C\u5F53\u524D\u503C\u4E3A ${user.userid}
struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 定义全局资源文件名叫validation,放在classpath根目录下 -->
<constant name="struts.custom.i18n.resources" value="validation" />
<package name="register" namespace="/" extends="struts-default">
<action name="register" class="lesson10.Register">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
</action>
<action name="answer" class="lesson10.ClientValidate">
<result name="success">/answer.jsp</result>
<result name="input">/message.jsp</result>
</action>
</package>
</struts>
register.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!-- 使用sx:datetimepicker标签来显示日期,必须引入 dojo 插件,将struts2-dojo-plugin.jar 加入 -->
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
<html>
<head>
<title>登录实例</title>
<!-- 必须设置 sx:head dojo 标签才能生效-->
<sx:head/>
</head>
<body>
单独通过标签将必填项的错误信息展示出来:<br />
<s:fielderror>
<s:param>user.userid</s:param>
</s:fielderror>
<s:fielderror>
<s:param>user.username</s:param>
</s:fielderror>
<s:fielderror>
<s:param>user.password</s:param>
</s:fielderror>
<br />
请输入用户名和密码:
<br>
<!-- 当后台有fielderror与前台struts2标签的name一致时,会将相应的fielderror信息显示出来 -->
<s:form action="register" method="post" namespace="/">
<s:textfield label="用户ID" name="user.userid" />
<s:textfield label="用户名" name="user.username" />
<!-- showPassword=true 表示校验不通过后,输入的密码不会丢失 -->
<s:password label="密 码" name="user.password" showPassword="true"/>
<s:password label="再输一次" name="user.rePassword" showPassword="true"/>
<s:textfield label="年 龄" name="user.age" />
<!-- 使用了dojo的 datetimepicker 标签-->
<sx:datetimepicker label="生 日" name="user.birth" displayFormat="yyyy-MM-dd"/>
<s:textfield label="身 高" name="user.height" />
<s:textfield label="电子邮件" name="user.email" />
<s:submit value="注册" />
</s:form>
</body>
</html>
success.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>注册成功</title>
</head>
<body>
<p>
尊敬的用户名,您已经注册成功,你的基本信息如下:
</p>
用户ID:
<s:property value="#session.user.userid" />
<br />
用户名:
<s:property value="#session.user.username" />
<br />
密码:
<s:property value="#session.user.password" />
<br />
年龄:
<s:property value="#session.user.age" />
<br />
生日:
<!-- 默认日期显示的是SHORT格式,这里用date标签来显示成yyyy-MM-dd格式 -->
<s:date name="#session.user.birth" format="yyyy-MM-dd" />
<br />
身高:
<s:property value="#session.user.height" />
<br />
电子邮件:
<s:property value="#session.user.email" />
</body>
</html>
效果图:
message.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>客户端校验</title>
<s:head />
</head>
<body>
请输入您的留言:
<br>
<!--
struts2这里好像有一个bug。
一般写action="answer"就可以了,struts2框架会将默认的action扩展名加上。
但对于客户端校验,如果不写 action="answer.action" 直接访问就会报错。
-->
<s:form method="post" validate="true" action="answer.action" namespace="/">
<s:textfield label="用户名" name="name" />
<s:textfield label="年龄" name="age" />
<s:textfield label="留言" name="answer" />
<s:submit value="注册" />
</s:form>
</body>
</html>
ajaxMessage.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!-- 使用dojo插件标签 -->
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<html>
<head>
<title>客户端校验</title>
<!-- 注意使用 <sx:head /> -->
<sx:head />
</head>
<body>
请输入您的留言:
<br>
<!--
如果加上 validate="true",则客户端校验会优先于Ajax校验。
-->
<s:form method="post" validate="true" action="answer.action"
namespace="/">
<s:textfield label="用户名" name="name" />
<s:textfield label="年龄" name="age" />
<s:textfield label="留言" name="answer" />
<!-- 必须使用 <sx:submit />标签,并且设置 validate="true" 才能触发Ajax校验 -->
<sx:submit value="注册" validate="true" />
</s:form>
</body>
</html>
answer.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>注册成功</title>
</head>
<body>
<p>
尊敬的用户,你的反馈信息如下:
</p>
用户名:
<s:property value="name" />
<br />
年龄:
<s:property value="age" />
<br />
留言:
<s:property value="answer" />
<br />
</body>
</html>
效果图: