上面两篇文章已经介绍了通过编码java代码的方式实现action方法校验,这里我们介绍第二种方式:xml配置文件
首先我们来看一个样例:
ValidateAction.java:
package com.itheima.action; import com.opensymphony.xwork2.ActionSupport; public class ValidateAction extends ActionSupport { private String username;
private String tel;
private String msg; public void setUsername(String username) {
this.username = username;
} public void setTel(String tel) {
this.tel = tel;
} public String getUsername() {
return username;
} public String getTel() {
return tel;
} public String getMsg() {
return msg;
} public String execute1() {
msg = "execute1";
return "success";
} public String execute2() {
msg = "execute2";
return "success";
}
}
这里大家也看到了在该action中我们并没有重写validate方法。
可是须要注意的是,通过配置xml文件这时就须要加入getXXX方法。不然框架获取不到字段的值
struts2.xml:
<action name="validateAction_*" class="com.itheima.action.ValidateAction" method="{1}">
<result name="success">/success.jsp</result>
<result name="input">/person.jsp</result>
</action>
person.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>
<s:fielderror/>
<form action="${pageContext.request.contextPath }/validateAction_execute1.action" method="post">
用户名:<input type="text" name="username"><br>
手机号:<input type="text" name="tel"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
success.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>
${msg }
</body>
</html>
然后我们配置xml文件:
首先须要将xml文件配置在须要校验的action所在的包下(同包),然后文件名称有个严格的格式:ActionClassName-validation.xml
当中ActionClassName为校验的action名称,-validation.xml部分固定不变。
本例中为ValidateAction-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"> <validators>
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户名不能为空。。。 </message>
</field-validator>
</field>
<field name="tel">
<field-validator type="requiredstring">
<message>手机号不能为空。 。。 </message>
</field-validator>
<field-validator type="regex">
<param name="regexExpression"><![CDATA[^1[358]\d{9}$]]></param>
<message>手机号格式不对。。 。</message>
</field-validator>
</field>
</validators>
简介下:
(1)field:须要校验的字段。name属性值须要和action中的字段一致;
(2)field-validator:校验器,type属性值为struts2框架提供的校验器。路径为:项目导入的jar包xwork-core-2.3.16.3.jar/com.opensymphony.xwork2.validator.validators/default.xml
以下是default.xml中全部的校验器:
<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"/>
<validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>
</validators>
(3)接着param是为type指定的校验器提供參数值,由于一个校验器相应一个java类,能够通过param标签为该类中的字段指定值。
这里<param name="trim">true</param>意思是设置requiredstring校验器所引用的类com.opensymphony.xwork2.validator.validators.RequiredStringValidator中的字段trim的值为true,意思是对username属性值作去前后空格处理。
(4)<message>用于发送错误信息
(5)此外xml文件的模版能够在struts2官方提供的框架压缩包中找到,路径为:struts-2.3.16.3\src\xwork-core\src\main\resources
(6)另一个须要注意的地方是:
ValidateAction-validation.xml文件引用的dtd文件来自网络,也就是说假设电脑没有联网,编辑该文件时按住alt+/就不会自己主动提示,所以我们这里能够这么做:
ValidateAction-validation.xml文件顶部有例如以下内容:
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
即:<!DOCTYPE 根标签名 public dtd名称 dtdurl地址>,public代表引用网络资源
我这里以eclipse做说明:
windows/preferences/xml/xml catalog
然后打开例如以下对话框:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbTYzMTUyMTM4Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
点击add。出现例如以下窗体
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbTYzMTUyMTM4Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
点击system file,然后选择上述路径下的xwork-validator-1.0.3.dtd文件,然后在key type下拉框中选择URI选项
在key输入框中输入引用的网络dtd文件的网络位置,这里为:http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd,然后保存就ok了