struts2校验の实现
1.JSP
<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" pageEncoding="utf-8" %> <%@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> <h2>请输入您的注册信息</h2> <s:fielderror/> <s:form action="regist"> <s:textfield name="name" label="用户名"/> <s:textfield name="pass" label="密码"/> <s:textfield name="age" label="年龄"/> <s:textfield name="birth" label="生日"/> <s:submit value="注册"/> </s:form> </body> </html>
2.Action
package com.mi.action;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class RegistAction extends ActionSupport
{
/**
*
*/
private static final long serialVersionUID = 1L;
// カィメ�4クノヤアア菽ソキ籏ーヌ�ヌホハ�
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;
}
}
3.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> <constant name="struts.i18n.encoding" value="utf-8"/> <package name="lee" extends="struts-default"> <!-- 用户注册的Action --> <action name="regist" class="com.mi.action.RegistAction"> <!-- 类型转换失败、输入校验失败,转入input对应的视图 --> <result name="input">/content/registForm.jsp</result> <result>/content/show.jsp</result> </action> <action name="*"> <result>/content/{1}.jsp</result> </action> </package> </struts>
4.RegistAction-validation.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 指定校验配置文件的DTD信息 --> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <!-- 校验文件的根元素 --> <validators> <!-- 校验Action的name属性 --> <field name="name"> <!-- 指定name属性必须满足必填规则 --> <field-validator type="requiredstring"> <param name="trim">true</param> <message key="name.requried"/> </field-validator> <!-- 指定name属性必须匹配正则表达式 --> <field-validator type="regex"> <param name="regex"><![CDATA[(\w{4,25})]]></param> <message key="name.regex"> </message> </field-validator> </field> <!-- 校验Action的pass属性 --> <field name="pass"> <!-- 指定pass属性必须满足必填规则 --> <field-validator type="requiredstring"> <param name="trim">true</param> <message key="pass.requried"/> </field-validator> <!-- 指定pass属性必须满足匹配指定的正则表达式 --> <field-validator type="regex"> <param name="regex"><![CDATA[(\w{4,25})]]></param> <message key="pass.regex"/> </field-validator> </field> <!-- 指定age属性必须在指定范围内--> <field name="age"> <field-validator type="int"> <param name="min">1</param> <param name="max">150</param> <message key="age.range"/> </field-validator> </field> <!-- 指定birth属性必须在指定范围内--> <field name="birth"> <field-validator type="date"> <!-- 下面指定日期字符串时,必须使用本Locale的日期格式 --> <param name="min">1900-01-01</param> <param name="max">2050-02-21</param> <message key="birth.range"/> </field-validator> </field> </validators>