sturts2 验证框架<三>
4. 用户Bean User.java
5.配置验证文件RegisterAction-validation.xml
6.struts2框架默认加载的配置文件struts.xml
7.web服务器启动时加载Struts 配置文件 web.xml
至此,完毕.不过需要注意:
1.配置验证xml文档的名字有讲究:格式为:Action名字(类)-validation.xml,与类在同一包中;
2.验证文档里的<field-validator type="">中的type类型要和VO中的User属性类型一致,否则会报类型转换错误
- package org.kingtoon.bean;
- import java.util.Date;
- public class User {
- private String userName;
- private String password;
- private String rePassword;
- private Integer age;
- private Date birthday;
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- 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 String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- }
package org.kingtoon.bean; import java.util.Date; public class User { private String userName; private String password; private String rePassword; private Integer age; private Date birthday; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } 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 String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
5.配置验证文件RegisterAction-validation.xml
- <validators>
- <field name="user.userName">
- <!-- 验证字符串不能为空 -->
- <field-validator type="requiredstring">
- <!-- 去空格 -->
- <param name="trim">true</param>
- <!-- 错误提示信息 -->
- <message>用户名不能为空</message>
- </field-validator>
- <!-- 验证字符串长度 -->
- <field-validator type="stringlength">
- <param name="minLength">2</param>
- <param name="maxLength">20</param>
- <message>用户名长度应在2到18个字符间</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">6</param>
- <param name="maxLength">18</param>
- <message>密码长度应在6到18个字符之间</message>
- </field-validator>
- </field>
- <field name="user.age">
- <field-validator type="int">
- <param name="min">1</param>
- <param name="max">150</param>
- <message>年龄应在1到150之间</message>
- </field-validator>
- </field>
- <!-- 验证字符串为日期类型 -->
- <field name="user.birthday">
- <field-validator type="date">
- <param name="min">1900-01-01</param>
- <param name="max">2008-10-16</param>
- <message>出生日期应在1900-01-01到2008-10-16</message>
- </field-validator>
- </field>
- </validators>
<validators> <field name="user.userName"> <!-- 验证字符串不能为空 --> <field-validator type="requiredstring"> <!-- 去空格 --> <param name="trim">true</param> <!-- 错误提示信息 --> <message>用户名不能为空</message> </field-validator> <!-- 验证字符串长度 --> <field-validator type="stringlength"> <param name="minLength">2</param> <param name="maxLength">20</param> <message>用户名长度应在2到18个字符间</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">6</param> <param name="maxLength">18</param> <message>密码长度应在6到18个字符之间</message> </field-validator> </field> <field name="user.age"> <field-validator type="int"> <param name="min">1</param> <param name="max">150</param> <message>年龄应在1到150之间</message> </field-validator> </field> <!-- 验证字符串为日期类型 --> <field name="user.birthday"> <field-validator type="date"> <param name="min">1900-01-01</param> <param name="max">2008-10-16</param> <message>出生日期应在1900-01-01到2008-10-16</message> </field-validator> </field> </validators>
6.struts2框架默认加载的配置文件struts.xml
- <struts>
- <constant name="struts.custom.i18n.resources" value="messageResource"></constant>
- <package name="user" extends="struts-default">
- <action name="register" class="org.kingtoon.action.RegisterAction">
- <result name="success">/welcome.jsp</result>
- <result name="input">/register.jsp</result>
- </action>
- </package>
- </struts>
<struts> <constant name="struts.custom.i18n.resources" value="messageResource"></constant> <package name="user" extends="struts-default"> <action name="register" class="org.kingtoon.action.RegisterAction"> <result name="success">/welcome.jsp</result> <result name="input">/register.jsp</result> </action> </package> </struts>
7.web服务器启动时加载Struts 配置文件 web.xml
- <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <filter>
- <filter-name>struts-cleanup</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.ActionContextCleanUp
- </filter-class>
- </filter>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.FilterDispatcher
- </filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts-cleanup</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>register.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <filter-name>struts-cleanup</filter-name> <filter-class> org.apache.struts2.dispatcher.ActionContextCleanUp </filter-class> </filter> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>register.jsp</welcome-file> </welcome-file-list> </web-app>
至此,完毕.不过需要注意:
1.配置验证xml文档的名字有讲究:格式为:Action名字(类)-validation.xml,与类在同一包中;
2.验证文档里的<field-validator type="">中的type类型要和VO中的User属性类型一致,否则会报类型转换错误