欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2--输入校验的国际化信息

程序员文章站 2022-03-03 12:37:36
...

strust2国际化:

 一:简单理解

    国际化简称i18n,其来源是英文单词 internationalization的首末字符i 和n。18为中间的字符数。

    随着全球经济的一体化,软件开发者应该开发出支持多国语言、国际化的Web应用。对于Web应用来说,同样的页面在不同的语言环境下需要显示不同的效果。

    国际化文件的命名规则:

          1、基本名.properties 如:message.properties

          2、基本名_语言编码_国家编码.properties 如:message_zh_CN.properties, message_en_US.properties 其中语言编码和国家编码是固定的,可以在JDK中Locale类的常量中找到。


两种形式:

对于两种形式的国际化资源文件配置,在Action同目录下创建Action名.properties文件:

这里为LoginAction.properties:

user.username.requiredstring = \u8BF7\u8F93\u5165\u59D3\u540D
user.username.regex = \u60A8\u8F93\u5165\u7684\u59D3\u540D\u53EA\u80FD\u662F\u5B57\u6BCD\u548C\u6570\u5B57\uFF0C\u4E14\u957F\u5EA6\u57284\u523020\u4E4B\u95F4
user.password.requiredstring = \u8BF7\u8F93\u5165\u5BC6\u7801
user.password.regex = \u60A8\u8F93\u5165\u7684\u59D3\u540D\u53EA\u80FD\u662F\u5B57\u6BCD\u548C\u6570\u5B57\uFF0C\u4E14\u957F\u5EA6\u4E3A6
user.tel.requiredstring = \u8BF7\u8F93\u5165\u7535\u8BDD\u53F7\u7801
user.tel.regex = \u60A8\u8F93\u5165\u7684\u683C\u5F0F\u4E0D\u6B63\u786E
user.age.int = \u8BF7\u8F93\u5165\u60A8\u7684\u771F\u5B9E\u5E74\u9F84
user.birthday.date = \u8BF7\u8F93\u5165\u6B63\u786E\u7684\u751F\u65E5

后面的编码为汉字,可以通过properties查看具体信息:

struts2--输入校验的国际化信息

一:通过key来指定国际化提示信息

LoginAction:

package com.action;

import com.bean.User;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
 private User user;
 public String execute() {
	return SUCCESS;
}
public User getUser() {
	return user;
}

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

LoginAction-validate.properties文件:

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE validators PUBLIC
  		"-//OpenSymphony Group//XWork Validator 1.0.3//EN"
  		"http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">

<validators>
  <field name  = "user.username">
    <field-validator type = "requiredstring">
       <param name = "doTrim">true</param>
       <message key = "user.username.requiredstring"/>
    </field-validator>
    <field-validator type = "regex">
       <param name = "expression"><![CDATA[(\w{4,20})]]></param>
       <message key = "user.username.regex"/>
    </field-validator>
  </field>
  
  <field name = "user.password">
    <field-validator type = "requiredstring">
       <param name = "doTrim">true</param>
       <message key = "user.password.requiredstring"/>
    </field-validator> 
    <field-validator type = "regex">
       <param name = "expression"><![CDATA[(\w{6})]]></param>
       <message key = "user.password.regex"/>
    </field-validator> 
  </field>
  
    <field name = "user.tel">
    <field-validator type = "requiredstring">
       <param name = "doTrim">true</param>
       <message key = "user.tel.requiredstring"/>
    </field-validator> 
    <field-validator type = "regex">
       <param name = "expression"><![CDATA[(^1[3578]\d{9}$)]]></param>
       <message key = "user.tel.regex"/>
    </field-validator> 
  </field>
  
   <field name = "user.age">
    <field-validator type = "int">
       <param name = "min">1</param>
       <param name = "max">100</param>
       <message key = "user.age.int"/>
    </field-validator> 
  </field>
  
   <field name = "user.birthday">
    <field-validator type = "date">
       <param name = "min">1990-01-01</param>
       <param name = "max">2020-01-01</param>
       <message key = "user.birthday.date"/>
     </field-validator> 
  </field>
</validators>        


二:通过ActionSupport的getText()方法来获取国际化提示信息

LoginAction-validate.properties文件:

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE validators PUBLIC
  		"-//OpenSymphony Group//XWork Validator 1.0.3//EN"
  		"http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">

<validators>
  <field name  = "user.username">
    <field-validator type = "requiredstring">
       <param name = "doTrim">true</param>
       <message>${getText("user.username.requiredstring")}</message>
    </field-validator>
    <field-validator type = "regex">
       <param name = "expression"><![CDATA[(\w{4,20})]]></param>
       <message>${getText("user.username.regex")}</message>
    </field-validator>
  </field>
  
  <field name = "user.password">
    <field-validator type = "requiredstring">
       <param name = "doTrim">true</param>
       <message>${getText("user.password.requiredstring")}</message>
    </field-validator> 
    <field-validator type = "regex">
       <param name = "expression"><![CDATA[(\w{6})]]></param>
       <message>${getText("user.password.regex")}</message>
    </field-validator> 
  </field>
  
    <field name = "user.tel">
    <field-validator type = "requiredstring">
       <param name = "doTrim">true</param>
       <message>${getText("user.tel.requiredstring")}</message>
    </field-validator> 
    <field-validator type = "regex">
       <param name = "expression"><![CDATA[(^1[3578]\d{9}$)]]></param>
       <message>${getText("user.tel.regex")}</message>
    </field-validator> 
  </field>
  
   <field name = "user.age">
    <field-validator type = "int">
       <param name = "min">1</param>
       <param name = "max">100</param>
       <message>${getText("user.age.int")}</message>
    </field-validator> 
  </field>
  
   <field name = "user.birthday">
    <field-validator type = "date">
       <param name = "min">1990-01-01</param>
       <param name = "max">2020-01-01</param>
       <message>${getText("user.birthday.date")}</message>
     </field-validator> 
  </field>
</validators>