在上节讨论过了,类型转换的原理,关键是这些操作都是在交给Action之前,被拦截器拦下后添加的操作,今天的校验器也是如此,下面介绍原理:
1、前台表单提交;
2、web.xml中过滤器,拦截请求,读取struts.xml文件;
3、找到请求相应的Action,准备将请求转发给Action;
4、被struts2的拦截器拦下;
5、按照strust.xml文件中的配置,检查是否有属性文件的配置;
6、struts去项目中的相应目录下,寻找属性文件;
7、如果找到了符合类型转换的属性文件,表明该项目使用了自定义的类型转换;
8、struts便去寻找相应的类型转换器,进行类型的转化;如果没有,就使用struts默认的类型转化;
9、如果找到了符合校验器的属性文件或者xml文件;表明该项目使用了struts的校验器;因为struts的校验器,使用起来很方便了,能够满足大多数的需求;自定义的情况不多;
10、如果在struts.xml同一目录下找到了validators.xml文件,表明该项目使用自定义的校验器;
下面讲解使用校验器的具体过程:
(1)、最基本的校验器使用:
前台JSP文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'basic-validation.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:form action="basicValidation"> <s:textfield name="username" label="用户名"></s:textfield> <!-- 用户名:<input type="text" /> --> <s:password name="password" label="密码"></s:password> <s:select label="性别" name="gender" list="#{'1' : '男', '0' : '女'}" listKey="key" listValue="value" value="0"/> <s:textfield name="age" label="年龄"></s:textfield> <s:textfield name="birthDate" label="出生日期"></s:textfield> <s:textfield name="tel" label="联系电话"></s:textfield> <s:textfield name="email" label="Email"></s:textfield> <s:textfield name="url" label="个人首页"></s:textfield> <s:submit></s:submit> </s:form> <hr/> <s:fielderror></s:fielderror> </body> </html>
对应的Action文件:
package struts.validation; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class BasicValidationAction extends ActionSupport { private static final long serialVersionUID = 1L; private String username; private String password; private int gender; private int age; private Date birthDate; private String tel; private String email; private String url; 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 int getGender() { return gender; } public void setGender(int gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } @Override public String execute() throws Exception { System.out.println("gender is "+gender); return SUCCESS; } public String login(){ return SUCCESS; } }
校验器:注意文件名(Action类型-validation.xml)
<?xml version="1.0" encoding="GBK"?> <!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"> <param name="fieldname">username</param> <param name="trim">true</param> <message>${getText("user.username.required")}</message> <!-- message>用户名不能为空</message--> </validator> <!-- 配置用户名的正则表达式校验 --> <validator type="regex"> <param name="fieldname">username</param> <param name="expression"><![CDATA[(\w{5,50})]]></param> <message>${getText("user.username.regex")}</message> <!-- message>用户名输入不合法,必须为长度为5~50之间的字母或汉字</message--> </validator> <!-- 配置密码的必填字符串校验 --> <validator type="requiredstring"> <param name="fieldname">password</param> <param name="trim">true</param> <message>${getText("user.password.required")}</message> <!-- message>密码不能为空</message--> </validator> <!-- 配置密码的正则表达式校验--> <validator type="regex"> <param name="fieldname">password</param> <param name="expression"><![CDATA[(\w{5,50})]]></param> <message>${getText("user.password.regex")}</message> <!--message>密码输入不合法,必须为长度为5~50之间的字母或汉字</message--> </validator> <!-- 配置年龄的整数范围校验 --> <validator type="int"> <param name="fieldname">age</param> <param name="min">1</param> <param name="max">130</param> <message>${getText("user.age")}</message> <!-- message>年龄输入不合法</message--> </validator> </validators>
使用的国际化的一些资源文件:
xwork.default.invalid.fieldvalue=globalMessage----{0}\u5B57\u6BB5\u65E0\u6548 user.username.required=globalMessage----\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A user.username.regex=globalMessage----\u7528\u6237\u540D\u8F93\u5165\u4E0D\u5408\u6CD5\uFF0C\u5FC5\u987B\u4E3A\u957F\u5EA6\u4E3A5~50\u4E4B\u95F4\u7684\u5B57\u6BCD\u6216\u6570\u5B57 user.password.required=globalMessage----\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A user.password.regex=globalMessage----\u5BC6\u7801\u8F93\u5165\u4E0D\u5408\u6CD5\uFF0C\u5FC5\u987B\u4E3A\u957F\u5EA6\u4E3A5~50\u4E4B\u95F4\u7684\u5B57\u6BCD\u6216\u6570\u5B57 user.age=globalMessage----\u5E74\u9F84\u5FC5\u987B\u5728${min}\u548C${max}\u4E4B\u95F4\! user.birthDate=globalMessage----\u51FA\u751F\u65E5\u671F\u8F93\u5165\u4E0D\u5408\u6CD5 user.email=globalMessage----Email\u5730\u5740\u8F93\u5165\u4E0D\u5408\u6CD5 user.url=globalMessage----\u4E2A\u4EBA\u9996\u9875\u8F93\u5165\u4E0D\u5408\u6CD5
既然使用资源文件,在struts.xml文件中应该用:
<constant name="struts.custom.i18n.resources" value="globalMessages" />
本例子中资源文件的名字:globalMessages_zh_CN.properties;注意value的值;
这样最基本的校验便可以实现;注意校验的文件XXX.xml与action是同一个目录;