struts2中的校验规则文件

◆使用客户端校验
校验规则文件的根元素是<validators…/>,<validators…/>元素可包含多个<filed…/>或<validator…/>元素,他们都用于配置校验规则,区别是:<filed-validator…/>是字段校验器的配置风格,而<validator…/>是非字段校验器的配置风格。
Struts2中每个Action中都有一个校验文件,文件名遵守如下规则:
<Action名字>-validation.xml,并且该文件必须放在与Action同一个目录下
Struts2使用客户端校验只需改变两个地方:
●将输入页面的表单元素改为使用Struts2标签来生成表单
●为该<s:form …/>元素增加validate=”true”属性

一个注册界面的实现:

图1 工程结构
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>请输入你的注册信息</title>
</head>
<body>
<h1>请输入你的注册信息</h1>
<s:form action="registerAction" method="post" validate="true">
<s:textfield label="用户名" name="name" size="20"/>
<s:password label="密码" name="pass" size="20"/>
<s:textfield label="年龄" name="age" size="20"/>
<s:textfield label="生日" name="birth" size="20"/>
<s:submit value="确认"/>

<!-- jsp代码 -->
<!-- 用户名:<input type="text" name="name"><br>
密&nbsp;&nbsp;码:<input type="password" name="pass"><br>
年&nbsp;&nbsp;龄:<input type="text" name="age"><br>
生&nbsp;&nbsp;日:<input type="text" name="birth"><p>
<input type="submit" value="注册"> -->
</s:form>
</body>
</html>

web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
     <display-name>register</display-name>
     <filter>
         <filter-name>struts2</filter-name> 
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
     <welcome-file-list>
         <welcome-file>index.html</welcome-file>
     </welcome-file-list>
</web-app>

struts.xml
<!DOCTYPE struts PUBLIC
         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
         "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
     <include file="struts-default.xml"/>
</struts>

RegisterAction.java

package com.sff.project;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class RegisterAction extends ActionSupport {

private String name;
private String pass;
private int age;
private Date birth;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the pass
*/
public String getPass() {
return pass;
}
/**
* @param pass the pass to set
*/
public void setPass(String pass) {
this.pass = pass;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the birth
*/
public Date getBirth() {
return birth;
}
/**
* @param birth the birth to set
*/
public void setBirth(Date birth) {
this.birth = birth;
}
public String execute(){
return SUCCESS;
}
}

RegisterAction-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>*必须输入名字</message>
</field-validator>
<field-validator type="regex">
<param name="expression"><![CDATA[(\w{4,25})]]></param>
<message>用户名只能是字母和数字,且长度必须在4到25之间</message>
</field-validator>
</field>

<field name="pass">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>*必须输入密码</message>
</field-validator>
<field-validator type="regex">
<param name="expression"><![CDATA[(\w{2,25})]]></param>
<message>密码只能是字母和数字,且长度必须在4到25之间</message>
</field-validator>
</field>
<!-- 指定age的范围 -->
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>年纪必须在1到150之间</message>
</field-validator>
</field>

<!-- 指定birth的范围 -->
<field name="birth">
<field-validator type="date">
<param name="min">1900-01-01</param>
<param name="max">2050-01-01</param>
<message>年纪必须在${min}到$(max)之间</message>
</field-validator>
</field>
</validators>

运行结果:

图2  运行结果

注意检查浏览器的地址栏,该页面停留在原来的页面上并未提交到对应的Action中,这就表明上面的数据校验过程是客户端完成的。

◆ 非字段校验器配置风格
注意格式问题:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<!-- 非字段校验器风格 -->
<!-- 配置制定必填字符串的校验器 -->
<validator type="requiredstring">
<!-- 使用校验器校验name属性 -->
<param name="fieldName">name</param>
<param name="trim">true</param>
<!-- 指定校验失败后输出name.required对应的国际化信息 -->
<message>${getText("name.requried")}</message>
</validator>
<!-- 配置指定正则表达式的校验器 -->
<validator type="regex">
<param name="fieldName">name</param>
<param name="trim">true</param>
<param name="expression"><![CDATA[(\w{4,25})]]></param>
<message>${getText("name.regex")}</message>
</validator>

<validator type="requiredstring">
<param name="fieldName">pass</param>
<param name="trim">true</param>
<message>${getText("pass.required")}</message>
</validator>
<validator type="regex">
<param name="fieldName">pass</param>
<param name="trim">true</param>
<param name="expression"><![CDATA[(\w{4,25})]]></param>
<message>${getText("pass.regex})"}</message>
</validator>

<validator type="int">
<param name="fieldName">age</param>
<param name="min">1</param>
<param name="max">150</param>
<message>${getText("age.range")}</message>
</validator>

<validator type="date">
<param name="fieldName">birth</param>
<param name="min">1900-1-1</param>
<param name="max">2050-12-31</param>
<message>${getText("birth.range")}</message>
</validator>
</validators>

◆ 短路校验器
采用短路校验器只需要在<validator…/>或<field-validator…/>中增加short-circuit=“true”即可。

你可能感兴趣的:(apache,xml,Web,struts,正则表达式)