struts2开发_validation_struts2客户端校验

项目结构:

运行结果:

运行结果:注册成功

 

==============================================

/struts2_0200_validation/src/com/b510/register/action/RegistAction.java

  1 package com.b510.register.action;
2
3 import com.opensymphony.xwork2.ActionSupport;
4
5 import java.util.Date;
6
7 /**
8 * 注册信息Action
9 *
10 * @author Hongten
11 *
12 */
13 public class RegistAction extends ActionSupport {
14 private static final long serialVersionUID = 1L;
15
16 /**
17 * 用户名
18 */
19 private String name;
20 /**
21 * 密码
22 */
23 private String password;
24 /**
25 * 确认密码
26 */
27 private String repassword;
28 /**
29 * 年龄
30 */
31 private int age;
32 /**
33 * 工资
34 */
35 private double salary;
36 /**
37 * 生日
38 */
39 private Date birthday;
40 /**
41 * 邮箱
42 */
43 private String email;
44 /**
45 * 个人主页
46 */
47 private String personPage;
48
49 public int getAge() {
50 return age;
51 }
52
53 public Date getBirthday() {
54 return birthday;
55 }
56
57 public String getEmail() {
58 return email;
59 }
60
61 public String getName() {
62 return name;
63 }
64
65 public String getPassword() {
66 return password;
67 }
68
69 public String getPersonPage() {
70 return personPage;
71 }
72
73 public String getRepassword() {
74 return repassword;
75 }
76
77 public double getSalary() {
78 return salary;
79 }
80
81 public void setAge(int age) {
82 this.age = age;
83 }
84
85 public void setBirthday(Date birthday) {
86 this.birthday = birthday;
87 }
88
89 public void setEmail(String email) {
90 this.email = email;
91 }
92
93 public void setName(String name) {
94 this.name = name;
95 }
96
97 public void setPassword(String password) {
98 this.password = password;
99 }
100
101 public void setPersonPage(String personPage) {
102 this.personPage = personPage;
103 }
104
105 public void setRepassword(String repassword) {
106 this.repassword = repassword;
107 }
108
109 public void setSalary(double salary) {
110 this.salary = salary;
111 }
112
113 }

/struts2_0200_validation/src/com/b510/register/action/RegistAction-validation.xml

 1 <?xml version="1.0" encoding="GBK"?>
2 <!-- 指定校验配置文件的DTD信息 -->
3 <!DOCTYPE validators PUBLIC
4 "-//OpenSymphony Group//XWork Validator 1.0.3//EN"
5 "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">
6 <!-- 校验文件的根元素 -->
7 <validators>
8 <!-- 校验Action的name属性 -->
9 <field name="name">
10 <!-- 指定name属性必须满足必填规则 -->
11 <field-validator type="requiredstring">
12 <param name="trim">true</param>
13 <message>必须输入名字</message>
14 </field-validator>
15 <!-- 指定name属性必须匹配正则表达式 ,长度位4到20之间-->
16 <field-validator type="regex">
17 <param name="expression"><![CDATA[(\w{4,20})]]></param>
18 <message>您输入的用户名只能是字母和数字
19 ,且长度必须在4到20之间</message>
20 </field-validator>
21 </field>
22 <!-- 校验Action的password属性 -->
23 <field name="password">
24 <!-- 指定password属性必须满足必填规则 -->
25 <field-validator type="requiredstring">
26 <param name="trim">true</param>
27 <message>必须输入密码</message>
28 </field-validator>
29 <!-- 指定password属性必须满足匹配指定的正则表达式 -->
30 <field-validator type="regex">
31 <param name="expression"><![CDATA[(\w{4,25})]]></param>
32 <message>您输入的密码只能是字母和数字
33 ,且长度必须在4到25之间</message>
34 </field-validator>
35 </field>
36 <!-- 校验Action的repassword属性 -->
37 <field name="repassword">
38 <!-- 指定repassword属性必须满足必填规则 -->
39 <field-validator type="requiredstring">
40 <param name="trim">true</param>
41 <message>必须输入确认密码</message>
42 </field-validator>
43 <!-- 指定repassword属性必须满足匹配指定的正则表达式 -->
44 <field-validator type="fieldexpression">
45 <param name="expression"><![CDATA[(repassword==password)]]></param>
46 <message>您输入的确认密码错误!请重新输入</message>
47 </field-validator>
48 </field>
49 <!-- 指定age属性必须在指定范围内-->
50 <field name="age">
51 <field-validator type="int">
52 <param name="min">1</param>
53 <param name="max">120</param>
54 <message>年纪必须在${min}到${max}之间</message>
55 </field-validator>
56 </field>
57 <!-- 指定salary属性必须在指定的范围内 -->
58 <field name="salary">
59 <field-validator type="double">
60 <param name="min">2000.0</param>
61 <param name="max">10000.0</param>
62 <message>工资必须在${min}到${max}之间</message>
63 </field-validator>
64 </field>
65 <!-- 指定birthday属性必须在指定范围内-->
66 <field name="birthday">
67 <field-validator type="date">
68 <!-- 下面指定日期字符串时,必须使用本Locale的日期格式 -->
69 <param name="min">1950-01-01</param>
70 <param name="max">2100-01-01</param>
71 <message>生日必须在${min}到${max}之间</message>
72 </field-validator>
73 </field>
74 <!-- 指定email属性必须在指定的范围内 -->
75 <field name="email">
76 <!-- 指定需要校验的字段名为email -->
77 <field-validator type="email">
78 <!-- 指定校验失败的提示信息 -->
79 <message>你输入的邮箱必须是一个有效,如:example@163.com</message>
80 </field-validator>
81 </field>
82 <!-- 指定personPage属性必须在指定的范围内 -->
83 <field name="personPage">
84 <!-- 指定需要校验的字段名为personPage -->
85 <field-validator type="url">
86 <!-- 指定校验失败的提示信息 -->
87 <message>你输入的个人主页地址必须有效,如:http://www.example.com</message>
88 </field-validator>
89
90 </field>
91 </validators>

/struts2_0200_validation/src/struts-validation.xml

 1 <?xml version="1.0" encoding="GBK"?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
4 "http://struts.apache.org/dtds/struts-2.1.7.dtd">
5 <struts>
6 <package name="hongtenRegister" extends="struts-default">
7 <!-- 用户注册的Action -->
8 <action name="regist" class="com.b510.register.action.RegistAction">
9 <!-- 类型转换失败、输入校验失败,转入该页面 -->
10 <result name="input">/regist.jsp</result>
11 <result>/show.jsp</result>
12 </action>
13 <action name="">
14 <result>.</result>
15 </action>
16 </package>
17 </struts>

/struts2_0200_validation/src/struts.xml

1 <?xml version="1.0" encoding="GBK"?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
4 "http://struts.apache.org/dtds/struts-2.1.7.dtd">
5 <struts>
6 <constant name="struts.i18n.encoding" value="GBK"/>
7 <constant name="struts.devMode" value="true"></constant>
8 <include file="struts-validation.xml"></include>
9 </struts>

/struts2_0200_validation/WebRoot/regist.jsp

 1 <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
2 <%@taglib prefix="s" uri="/struts-tags"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head>
7
8 <title>请输入您的注册信息</title>
9 <s:head/>
10
11 </head>
12
13 <body>
14 <h2>请输入您的注册信息</h2>
15 <s:form action="regist">
16 <s:textfield name="name" label="用户名"/>
17 <s:password name="password" label="密码"/>
18 <s:password name="repassword" label="确认密码"/>
19 <s:textfield name="age" label="年龄"/>
20 <s:textfield name="salary" label="工资"/>
21 <s:textfield name="birthday" label="生日"/>
22 <s:textfield name="email" label="邮箱"/>
23 <s:textfield name="personPage" label="个人主页"/>
24 <s:submit value="注册"/>
25 </s:form>
26 </body>
27 </html>

/struts2_0200_validation/WebRoot/show.jsp

 1 <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
2 <%@taglib prefix="s" uri="/struts-tags"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head>
7
8 <title>校验成功</title>
9
10 </head>
11
12
13 <body>
14 <h3>校验成功</h3>
15 用户名:<s:property value="name"/><br/>
16 密码:<s:property value="password"/><br/>
17 工资:<s:property value="salary"/><br/>
18 年龄:<s:property value="age"/><br/>
19 生日:<s:property value="birthday"/><br/>
20 邮箱:<s:property value="email"/><br />
21 个人主页:<s:property value="personPage"/>
22 </body>
23
24 </html>

 

posted @ 2012-04-06 17:01  Hongten  阅读(3187)  评论(2编辑  收藏  举报
Fork me on GitHub