一.知识点总结
后台验证有两种实现方式:
1 手工验证顺序:validateXxx(针对Action中某个业务方法验证)--> validate(针对Action中所有的业务方法验证)
2 声明式验证
validate()===>"Action的类名-validation.xml"===>必须放置在Action类的同目录下
validateXxx()===>"Action的类名-<action>标签中的name属性值-validation.xml"必须 放置在与Action类同目录下.
当存在两种验证文件时,其结果是二者验证效果之和,针对下面的例子:先执行ValidatorAction-validation.xml,后执行ValidationAction-ValidationAction-validation.xml
二.需求
这里要求使用后台验证,而非JS实现如下需求:
1.给定一个表单如下图所示:
2.所有选项都是必填的,这里要做个验证,提示信息如下图所示:
3.并且有固定的格式,
1)用户名必须是中文!
2)密码长度应为6-20位!
3)薪水范围为4000-10000!
4)生日必须介于1970-1-1到2070-1-1之间
4.如果验证成功,那么出现如下提示页面
三.实现
需求分析:如果手动式验证,可能要写很多判断,而且实现起来可能还比较麻烦.对于上面常见的验证需求,sturts2做了很好的封装,这里就介绍关于使用声明式的验证来完成上述需求
1.实现代码
validator.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>validator</title>
</head>
<body>
<s:form action="ValidatorAction" type="POST">
<s:textfield label="用户名" name="username" />
<s:password label="密码" name="password" showPassword="true"/>
<s:textfield label="薪水" name="salary" />
<s:textfield label="生日" name="birthday"/>
<s:submit name="submit"/>
</s:form>
</body>
</html>
validator_success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <!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>Insert title here</title> </head> <body> 验证成功!<br><hr> 用户名:<s:property value="username"/><br> 密码:<s:property value="password"/><br> 薪水:<s:property value="salary"/><br> 生日:<s:property value="birthday"/><br> </body> </html>
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> <!--<include file="config/upload.xml"></include> --> <!-- 加载其他配置文件 --> <!-- <include file="config/upload-interceptor.xml"></include> --> <!-- 加载属性文件-国际化 --> <!-- <constant name="struts.custom.i18n.resources" value="message"></constant> --> <!-- 结果集 --> <!-- <include file="config/result_struts.xml"></include> --> <!-- 类型转换 --> <!-- <include file="config/type_struts.xml"></include> --> <!-- 文件下载 --> <!-- <include file="config/download_struts.xml"></include> --> <!-- 验证validator --> <include file="config/validator_struts.xml"></include> </struts>
ValidatorAction.java
package validator; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; /** * @ClassName: ValidatorAction * @Description: 验证action * @author: amosli * @email:amosli@infomorrow.com * @date Feb 16, 2014 10:13:50 PM */ public class ValidatorAction extends ActionSupport { private static final long serialVersionUID = 3437178521341339431L; private String username;// 用户名 private String password;// 密码 private Double salary;// 薪水 private Date birthday;// 生日 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String execute() throws Exception { return SUCCESS; } }
ValidatorAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <!-- 针对Action中所有的业务方法验证,类似于Validate()方法 --> <validators> <!-- name表示需要验证的字段 --> <field name="username"> <!-- type表示需要使用的struts2内置验证器的名字 --> <field-validator type="requiredstring"> <!-- 参数trim表示去除空格 --> <param name="trim">true</param> <!-- 验证错误提示信息 --> <message>用户名必填!</message> </field-validator> <field-validator type="regex"> <param name="regexExpression">[u4E00-uFA29]+</param> <message>用户名必须是中文!</message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message>密码必填!</message> </field-validator> <field-validator type="regex"> <param name="regexExpression">w{6,20}</param> <message>密码长度应为6-20位!</message> </field-validator> </field> <field name="salary"> <field-validator type="required"> <param name="trim">true</param> <message>薪水必填!</message> </field-validator> <field-validator type="double"> <param name="minInclusive">4000</param> <param name="maxInclusive">10000</param> <message>薪水范围为4000-10000</message> </field-validator> </field> <field name="birthday"> <field-validator type="required"> <param name="trim">true</param> <message>生日必填!</message> </field-validator> <field-validator type="date"> <param name="min">1970-1-1</param> <param name="max">2070-1-1</param> <message>生日必须介于1970-1-1到2070-1-1之间</message> </field-validator> </field> </validators>
2.代码分析
1).validator.jsp是程序入口,在其中定义了一个form表单,触发一个ValidatorAction
2).struts.xml使用include属性加载validator_struts.xml
3).validator_struts.xml,配置ValidatorAction,配置成功和出现异常的两种跳转,成功则跳转到validator_success.jsp,失败则跳回到validator.jsp
4).ValidatorAction.java,对应validator.jsp,定义username,passoword,salary,birthday,并提供set/get方法供注入和调用.
5).ValidatorAction-validation.xml,必须放在与ValidatorAction.java同一目录下,其命名规则为"xxxAction--validation.xml",这里主要调用struts2中内置的验证方法.也是本文重点.
/home/amosli/.m2/repository/org/apache/struts/xwork/xwork-core/2.3.16/xwork-core-2.3.16.jar/xwork-validator-1.0.3.dtd定义了ValidatorAction-validation.xml的格式规则.
xwork-core-2.3.16.jar/com/opensymphony/xwork2/validator/validators/default.xml,是验证器映射配置所在,也是核心配置源码所在.
<field name="xx"> ==> name表示需要验证的字段
<field-validator type="requiredstring"> ==> type表示需要使用的struts2内置验证器的名字,requiredstring和required分别表示,此项是必须是字符串类型的和此项是必须的,其中参数
<param name="trim">true</param> ==>trim表示去除首尾空格.
<message>xxx</message> ==>表示提示信息
double表示验证的类型必须的是Double型的.
<param name="minInclusive">4000</param> ===>minInclusive相当于>=,表示数值要大于等于参数值.
<param name="maxInclusive">10000</param> ===>maxInclusive相当于<=,表示数值要小于等于参数值.
还有minExclusive,maxExclusive相对应的表示>,<
date表示验证的类型将必须是Date类型的.其中参数
<param name="min">1970-1-1</param> ===> min即表示起始日期,最小日期
<param name="max">2070-1-1</param> ===>min即表示截止日期,最大日期
regex,表示可以使用正则表达式来实现,如下所示
<field-validator type="regex">
<param name="regexExpression">w{6,20}</param> ===>regexExpression表示将要匹配的正则表达式
<message>密码长度应为6-20位!</message> ===> 自定义提示信息
</field-validator>
具体内容可以参见其源码实现,在 xwork-core-2.3.16.jar/com/opensymphony/xwork2/validator/validators/default.xml有对应的源码,ctrl+shitf+t,即可跳转到对应的源码,快速入门可以先看注释,再看其实现代码.
6)validator_success.jsp,使用struts-tags标签来调用ValidatorAction中的get方法来获取输入的值.最终显示到页面上.