Struts2 中,相同的验证规则,如何使用同一条异常响应信息,及动态调用各自对应的名称?

如果相同的验证规则作为公共验证器使用,仅需修改 短路验证 中的  三个国际化资源文件,及公共验证器,和在页面与作为Model的类中添加相映的字段,number


示例如下:

~~~~~~~~~~~~~~~~~~~~~~~分割线~~~~~~~~~~~~~~~~~~~~~~~~~

三个国际化资源文件

基类 :nihao.properties 

注意:动态调用fieldName 使用的方法是${getText(fieldName)} ,而后把fieldName name属性名也配置到国际化资源文件中,这样可以实现多语言的切换

username=UserName
password=Password
password2=Password
submit=Submit
time=Time:{0}
error.int=${getText(fieldName)} needs to be between ${min} and ${max}
age=Age
number=Number
中文:nihao_zh_CN.properties
username=\u7528\u6237\u540D
password=\u8F93\u5165\u5BC6\u7801
password2=\u518D\u6B21\u786E\u8BA4\u5BC6\u7801
submit=\u63D0\u4EA4
time=\u65F6\u95F4:{0}
time2=\u65F6\u95F4:${birth}
error.int=${getText(fieldName)} \u9700\u8981\u5728 ${min} \u548C ${max}\u4E4B\u95F4
age=\u5E74\u9F84
number=\u6570\u5B57
英文:nihao_en_US.properties
username=UserName
password=Password
password2=Password
submit=Submit
time=Time:{0}
time2=Time:${birth}
error.int=${getText(fieldName)} needs to be between ${min} and ${max}
age=Age
number=Number

公共验证器

TestAgeValidation-validation.xml

<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator 1.0.2//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">

<validators>

     <field name="age">
     	<!-- 类型转换验证 -->
     	<field-validator type="conversion" short-circuit="false" >
	               <message>类型转换错误</message>
	      </field-validator>
     	<!-- 
     		看看添加了短路验证 short-circuit="false" 
     		若类型转换错误的时候,还会不会进行下面的 int整型验证 
     	-->
     
         <field-validator type="int" short-circuit="false">
             <param name="min">1</param>
             <param name="max">130</param>
             <message key="error.int"></message>
         </field-validator>
     </field>
   	
   	<!-- 新增此条验证器 -->
      <field name="number">
     	<!-- 类型转换验证 -->
     	<field-validator type="conversion" short-circuit="false" >
	               <message>类型转换错误</message>
	      </field-validator>
     
         <field-validator type="int" short-circuit="false">
             <param name="min">2</param>
             <param name="max">20</param>
             <message key="error.int"></message>
         </field-validator>
     </field>

      <validator type="expression">
         <param name="expression"><![CDATA[password==password2]]></param>
         <message>密码不一致</message>
      </validator>

</validators>

添加number 字段后的Person 类

package com.baidu.domain;

public class Person {
	private Integer age;
	private String password;
	private String password2;
	private Integer number;


	public Integer getNumber() {
		return number;
	}
	public void setNumber(Integer number) {
		this.number = number;
	}
	public String getPassword2() {
		return password2;
	}
	public void setPassword2(String password2) {
		this.password2 = password2;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [age=" + age + ", password=" + password + ", password2="
				+ password2 + ", number=" + number + "]";
	}

}

页面:

仅以 validation.jsp 页面为例,其他类同!

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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><br>
	<s:debug></s:debug>	
	<a href="testAge3.action?request_locale=zh_CN">中文</a>	
	 
	<a href="testAge3.action?request_locale=en_US">English</a>	
		<br><br>
	<a href="index.jsp">主页</a>	
	
	<center>
		
		<s:actionerror/>
		<s:form action="testAge1"   >
			<s:textfield name="age" key="age"></s:textfield>
			<s:textfield name="number" key="number"></s:textfield>	
			<s:password  name="password" key="password"></s:password>
			<s:password  name="password2" key="password2"></s:password>
			
			<s:submit key="submit"></s:submit>
		</s:form>
	
	</center>
	
</body>
</html>

效果:

英文:

Struts2 中,相同的验证规则,如何使用同一条异常响应信息,及动态调用各自对应的名称?_第1张图片

中文:

Struts2 中,相同的验证规则,如何使用同一条异常响应信息,及动态调用各自对应的名称?_第2张图片


  实现了 相同的验证规则,可以使用同一条响应信息,且能动态的切换 相对映的 fieldNamename 属性名

你可能感兴趣的:(Struts2 中,相同的验证规则,如何使用同一条异常响应信息,及动态调用各自对应的名称?)