struts2国际化设置

1.全局信息配置

在struts.xml增加如下内容:

<constant name="struts.custom.i18n.resources" value="global" />

或struts.properties文件中指定如下一行

struts.custom.i18n.resources=global

在class路径下增加如下文件:

(1)global_en_US.properties

username=(G)username
password=(G)password
submit=(G)submit
chinese=(G)Chinese
english=(G)English
language=(G)Language
login=(G)Login Page
firstpage=This is the sucess page.

 

 

(2)global_zh_CN.properties

username=(G)\u7528\u6237\u540D
password=(G)\u5BC6\u7801
submit=(G)\u786E\u5B9A
chinese=(G)\u4E2D\u6587
english=(G)\u82F1\u6587
language=(G)\u8BED\u8A00
login=(G)\u767B\u5F55\u9875\u9762
firstpage=\u8FD9\u662F\u6210\u529F\u9875\u9762

 global可以换成其他的文件名

 

 

2.在Action类中配置

 

如下图所示:


struts2国际化设置_第1张图片
 LoginValidate.java

@SuppressWarnings("serial")
public class LoginValidate extends ActionSupport {
	private String username;
	private String password;

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}

 LoginValidate-validation.xml

 

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

<validators>
	<field name="username">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>${getText("name.null")}</message>
		</field-validator>
	</field>
	<field name="password">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>${getText("password.null")}</message>
		</field-validator>
	</field>
</validators>

 LoginValidate_en_US.properties

 

name.null=(A)Username is empty\!
password.null=(A)Password is empty\!

 LoginValidate_zh_CN.properties

 

name.null=(A)\u7528\u6237\u540D\u4E3A\u7A7A\uFF01
password.null=(A)\u5BC6\u7801\u4E3A\u7A7A\uFF01

 3.国际化的使用

 

 

(1)jsp的使用

       <s:text name="inputpsw"/>

      <s:textfield name="username" key="username"/>

       <s:submit value="%{getText('submit')}" />
      <s:property value="%{getText('login')}" />

 (2)Action调用

       addActionError(getText("invalid"));

       addFieldError("password", "password.error");

  (3) validateors调用

       <message>${getText("error.name.null")}</message>

你可能感兴趣的:(struts2国际化设置)