首页 > 代码库 > struts2对action中的方法进行输入校验---xml配置方式(3)

struts2对action中的方法进行输入校验---xml配置方式(3)

上面两篇文章已经介绍了通过编码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=http://www.mamicode.com/"提交">>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

然后打开如下对话框:

点击add,出现如下窗口

点击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了

struts2对action中的方法进行输入校验---xml配置方式(3)