<?xml version="1.0" encoding="GBK"?> <project name="struts" basedir="." default=""> <property name="dist" value="classes"/> <property name="src" value="src"/> <path id="classpath"> <fileset dir="lib"> <include name="*.jar"/> </fileset> <pathelement path="${dist}"/> </path> <target name="compile" description="Compile all source code"> <delete dir="${dist}"/> <mkdir dir="${dist}"/> <copy todir="${dist}"> <fileset dir="${src}"> <exclude name="**/*.java"/> </fileset> </copy> <javac destdir="classes" debug="true" includeantruntime="yes" deprecation="false" optimize="false" failonerror="true"> <src path="${src}"/> <classpath refid="classpath"/> </javac> </target> </project>
<?xml version="1.0" encoding="GBK"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 定义Struts 2的核心Filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 让Struts 2的核心Filter拦截所有请求 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>请输入您的注册信息</title> <s:head/> </head> <body> <h3>请输入您的注册信息</h3> <s:form action="regist"> <s:textfield name="user.name" label="用户名"/> <s:textfield name="user.pass" label="密码"/> <s:textfield name="user.age" label="年龄"/> <s:textfield name="user.birth" label="生日"/> <s:submit value="注册"/> </s:form> </body> </html>
<%-- 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> author yeeku.H.lee kongyeeku@163.com version 1.0 Copyright (C), 2001-2016, yeeku.H.Lee This program is protected by copyright laws. Program Name: Date: --%> <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>校验成功</title> </head> <body> <h3>校验成功</h3> 用户名:<s:property value="user.name"/><br/> 密码:<s:property value="user.pass"/><br/> 年龄:<s:property value="user.age"/><br/> 生日:<s:property value="user.birth"/><br/> </body> </html>
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.i18n.encoding" value="GBK"/> <package name="lee" extends="struts-default"> <action name="regist" class="org.crazyit.app.action.RegistAction"> <result name="input">/WEB-INF/content/registForm.jsp</result> <result>/WEB-INF/content/show.jsp</result> </action> <action name="*"> <result>/WEB-INF/content/{1}.jsp</result> </action> </package> </struts>
package org.crazyit.app.action; import com.opensymphony.xwork2.ActionSupport; import org.crazyit.app.domain.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class RegistAction extends ActionSupport { // 定义User类型的成员变量用于封装请求参数 private User user; // user的setter和getter方法 public void setUser(User user) { this.user = user; } public User getUser() { return (this.user); } }
<?xml version="1.0" encoding="GBK"?> <!-- 指定校验规则文件的DTD信息 --> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <!-- 校验规则文件的根元素 --> <validators> <!-- 指定校验user字段 --> <field name="user"> <!-- 使用visitor校验器 --> <field-validator type="visitor"> <!-- 指定校验规则文件的context --> <param name="context">userContext</param> <!-- 指定校验失败后提示信息是否添加下面前缀 --> <param name="appendPrefix">true</param> <!-- 指定校验失败的提示信息前缀 --> <message>用户的:</message> </field-validator> </field> </validators>
package org.crazyit.app.domain; import java.util.*; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class User { private String name; private String pass; private int age; private Date birth; // name的setter和getter方法 public void setName(String name) { this.name = name; } public String getName() { return this.name; } // pass的setter和getter方法 public void setPass(String pass) { this.pass = pass; } public String getPass() { return this.pass; } // age的setter和getter方法 public void setAge(int age) { this.age = age; } public int getAge() { return this.age; } // birth的setter和getter方法 public void setBirth(Date birth) { this.birth = birth; } public Date getBirth() { return this.birth; } }
xwork.default.invalid.fieldvalue={0}字段无效 name.requried=用户名必填! name.regex=用户名必须是字母和数字,且长度必须在4~25之间! pass.requried=用户名必填! pass.regex=用户名必须是字母和数字,且长度必须在4~25之间! age.range=年龄必须在${min}和${max}之间。 birth.range=生日必须在${min}和${max}之间。
xwork.default.invalid.fieldvalue={0}\u5b57\u6bb5\u65e0\u6548 name.requried=\u7528\u6237\u540d\u5fc5\u586b\uff01 name.regex=\u7528\u6237\u540d\u5fc5\u987b\u662f\u5b57\u6bcd\u548c\u6570\u5b57\uff0c\u4e14\u957f\u5ea6\u5fc5\u987b\u57284~25\u4e4b\u95f4\uff01 pass.requried=\u7528\u6237\u540d\u5fc5\u586b\uff01 pass.regex=\u7528\u6237\u540d\u5fc5\u987b\u662f\u5b57\u6bcd\u548c\u6570\u5b57\uff0c\u4e14\u957f\u5ea6\u5fc5\u987b\u57284~25\u4e4b\u95f4\uff01 age.range=\u5e74\u9f84\u5fc5\u987b\u5728${min}\u548c${max}\u4e4b\u95f4\u3002 birth.range=\u751f\u65e5\u5fc5\u987b\u5728${min}\u548c${max}\u4e4b\u95f4\u3002
<?xml version="1.0" encoding="GBK"?> <!-- 指定校验配置文件的DTD信息 --> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <!-- 校验文件的根元素 --> <validators> <!-- 校验user的name属性 --> <field name="name"> <!-- 指定name属性必须满足必填规则 --> <field-validator type="requiredstring"> <param name="trim">true</param> <!-- 如果校验失败,输出name.requried对应的国际化信息 --> <message key="name.requried"/> </field-validator> <!-- 指定name属性必须匹配正则表达式 --> <field-validator type="regex"> <param name="regex"><![CDATA[(\w{4,25})]]></param> <!-- 如果校验失败,输出name.regex对应的国际化信息 --> <message key="name.regex"/> </field-validator> </field> <!-- 校验user的pass属性 --> <field name="pass"> <!-- 指定pass属性必须满足必填规则 --> <field-validator type="requiredstring"> <param name="trim">true</param> <!-- 如果校验失败,输出pass.requried对应的国际化信息 --> <message key="pass.requried"/> </field-validator> <!-- 指定pass属性必须满足匹配指定的正则表达式 --> <field-validator type="regex"> <param name="regex"><![CDATA[(\w{4,25})]]></param> <!-- 如果校验失败,输出pass.regex对应的国际化信息 --> <message key="pass.regex"/> </field-validator> </field> <!-- 指定user的age属性必须在指定范围内--> <field name="age"> <field-validator type="int"> <param name="min">1</param> <param name="max">150</param> <!-- 如果校验失败,输出age.range对应的国际化信息 --> <message key="age.range"/> </field-validator> </field> <!-- 指定user的birth属性必须在指定范围内--> <field name="birth"> <field-validator type="date"> <!-- 下面指定日期字符串时,必须使用本Locale的日期格式 --> <param name="min">1900-01-01</param> <param name="max">2050-02-21</param> <!-- 如果校验失败,输出birth.range对应的国际化信息 --> <message key="birth.range"/> </field-validator> </field> </validators>