Struts2提供了validation框架,可以使用该框架进行数据验证,和Struts1.x相同,也是通过xml文件的形式进行配置.
需要commons-validator.jar支持.
服务器端验证:
1. 创建action----ValidateAction.java
package com.validation.action;
import com.opensymphony.xwork2.ActionSupport;
public class ValidateAction extends ActionSupport {
private String name; //必须输入
private int age; //必须介于12-25之间
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.配置struts.xml文件
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="Validate" class="com.validation.action.ValidateAction">
<!-- 注意:如果使用了验证框架,就必须有一个name为input的返回结果 -->
<result name="input">/jsp/user_info.jsp</result>
<result>/jsp/user_info.jsp</result>
</action>
</package>
</struts>
3.编写验证规则配置文件
这是一个基于XML的配置文件,和struts1.x中的validator框架的验证规则配置文件类似,但一般放到和要验证的.class文件在同一目录下,而且配置文件名必须使用如下两个规则中的一个来命名:
<ActionClassName>-validation.xml
<ActionClassName>-<ActionAliasName>-validation.xml
其中<ActionAliasName>就算struts.xml文件中的<action>的name属性,这里用第一种命名规则,所以文件名是ValidateAction-validation.xml。内容如下:
<!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">
<message>请输入姓名</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">12</param>
<param name="max">25</param>
<message>年龄必须介于12-25之间</message>
</field-validator>
</field>
</validators>
其他验证规则(type属性的取值)如下:
<validators>
<validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
<validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
<validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
<validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
<validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
<validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
<validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
<validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
<validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
<validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
<validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
<validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
<validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
<validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
<validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
</validators>
4.jsp页面----user_info.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ValidationTest</title>
<link rel="stylesheet" href="../style.css" type="text/css"></link>
</head>
<body>
<s:form action="Validate">
<s:textfield name="name" label="姓名" />
<br />
<s:textfield name="age" label="年龄" />
<br />
<s:submit />
</s:form>
</body>
</html>
访问如下路径,点击提交
触发验证效果....
客户端验证:只需要在form中加入validate属性,值为true即可.
<s:form action="Validate" validate="true">
......
</s:form>