Struts2 框架验证

 一、对于输入校验struts2提供了两种实现方法:
        1、采用手工编写代码实现。
        2、基于XML配置方式实现。
         注意:配置验证文件ActionName-alias-validation.xml(常用)
使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport ,并且提供校验文件和action类放在同一个包下 ,
    ActionName是action的类名; alias是表单提交的action名; validation是固定写法 

                      

 二、具体的步骤:


 1、编写数据输入页面
  
<style type="text/css">
span {
	color: red;
	font-size: 12px;
}
</style>

</head>

<body>


	<!-- 错误提示 -->
<!-- 	<s:fielderror></s:fielderror> -->

	<h3>使用RegisterAction-alia-validation.xml进行验证</h3>
	<s:form action="reg2" method="post" namespace="/">
		用户名:<s:textfield name="user.username" />
          <span>${fieldErrors['user.username'][0] }</span>
		<br />
		
		密    码:<s:password name="user.password" />
		    <span>${fieldErrors['user.password'][0] }</span>
		<br />
		
		年    龄:<s:textfield name="user.age" />
	    <span>${fieldErrors['user.age'][0] }</span>
		<br />
		
		邮    箱:<s:textfield name="user.email" />
	    <span>${fieldErrors['user.email'][0] }</span>
		<br />
		
		电    话:<s:textfield name="user.phone"
			maxlength="11" />
		    <span>${fieldErrors['user.phone'][0] }</span>
		<br />
		<s:submit value="提交" />
	</s:form>
	<s:debug />
</body>
</html>

2、编写Action类并配置相关action

public class RegisterActionValidate extends ActionSupport {

	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	@Override
	public String execute() throws Exception {

		// 5)配置验证文件ActionName-alias-validation.xml
		// ActionName是Action的名称 RegisterAction
		// alia表单提交的action的名称

		return SUCCESS;
	}
}

3、在Action类同目录下创建验证文件ActionName-alias-validation.xml

4、使用验证器编写验证规则

           
<?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="user.username">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>用户名不能为空!</message>
		</field-validator>
		<field-validator type="stringlength">
			<param name="minLength">6</param>
			<param name="maxLength">8</param>
			<message>用户名必须是在${minLength}~${maxLength}之间的字符!</message>
		</field-validator>
	</field>

	<field name="user.password">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>密码不能为空!</message>
		</field-validator>
		<field-validator type="stringlength">
			<param name="minLength">6</param>
			<param name="maxLength">8</param>
			<message>密码必须是在${minLength}~${maxLength}之间的字符!</message>
		</field-validator>
	</field>

	<field name="user.age">
		<field-validator type="required">
			<message>年龄不能为空!</message>
		</field-validator>
		<field-validator type="int">
			<param name="min">1</param>
			<param name="max">120</param>
			<message>年龄必须是在${min}~${max}之间的字符!</message>
		</field-validator>
	</field>

	<field name="user.email">
		<field-validator type="requiredstring">
			<message>Email不能为空!</message>
		</field-validator>
		<field-validator type="email">
			<message>Email格式不正确!比如:([email protected])</message>
		</field-validator>
	</field>


	<field name="user.phone">
		<field-validator type="requiredstring">
			<message>手机不能为空!</message>
		</field-validator>
		<field-validator type="regex">
			<param name="regexExpression"><![CDATA[^1[358]\d{9}$]]></param>
			<message>手机必须是13,15,18开头的号!</message>
		</field-validator>
	</field>




</validators>



5、在页面上输出验证信息
    Struts2 框架验证_第1张图片



你可能感兴趣的:(Struts2 框架验证)