專案結構,如下圖片顯示:
global.properties
#Global messages global.username = Username global.password = Password global.submit = Submit
global_zh_CN.properties
#Global messages global.username = \u7528\u6237\u540d global.password = \u5bc6\u7801 global.submit=\u63d0\u4ea4
global_fr.properties
#Global messages global.username = Nom d'utilisateur global.password = Mot de passe global.submit = Soumettre
global_de.properties
#Global messages global.username = Benutzername global.password = Kennwort global.submit = Einreichen
兩個動作類,LocaleAction基本上是什麼都不做, 而 LoginAction 會做一個簡單的驗證和通過gettext()顯示來自資源包錯誤資訊。
LocaleAction.java
package com.tw511.common.action; import com.opensymphony.xwork2.ActionSupport; public class LocaleAction extends ActionSupport{ //business logic public String execute() { return "SUCCESS"; } }
LoginAction.java
package com.yiibai.user.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ private String username; private String password; //...getter and setter methods //business logic public String execute() { return "SUCCESS"; } //simple validation public void validate(){ if("".equals(getUsername())){ addFieldError("username", getText("username.required")); } if("".equals(getPassword())){ addFieldError("password", getText("password.required")); } } }
login.jsp
<%@ page contentType="text/html;charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 localization example</h1> <s:form action="validateUser" namespace="/user"> <s:textfield key="global.username" name="username" /> <s:password key="global.password" name="password"/> <s:submit key="global.submit" name="submit" /> </s:form> <s:url id="localeEN" namespace="/" action="locale" > <s:param name="request_locale" >en</s:param> </s:url> <s:url id="localezhCN" namespace="/" action="locale" > <s:param name="request_locale" >zh_CN</s:param> </s:url> <s:url id="localeDE" namespace="/" action="locale" > <s:param name="request_locale" >de</s:param> </s:url> <s:url id="localeFR" namespace="/" action="locale" > <s:param name="request_locale" >fr</s:param> </s:url> <s:a href="%{localeEN}" >English</s:a> <s:a href="%{localezhCN}" >Chinese</s:a> <s:a href="%{localeDE}" >German</s:a> <s:a href="%{localeFR}" >France</s:a> </body> </html>
<s:textfield key="global.username" name="username" /> <s:text name="global.username" /> <s:property value="getText('global.username')" /> <s:text name="global.password" />
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="global" /> <constant name="struts.devMode" value="true" /> <package name="user" namespace="/user" extends="struts-default"> <action name="login"> <result>/pages/login.jsp</result> </action> <action name="validateUser" class="com.yiibai.user.action.LoginAction"> <result name="SUCCESS">/pages/welcome.jsp</result> <result name="input">/pages/login.jsp</result> </action> </package> <package name="default" namespace="/" extends="struts-default"> <action name="locale" class="com.tw511.common.action.LocaleAction"> <result name="SUCCESS">/user/pages/login.jsp</result> </action> </package> </struts>
http://localhost:8080/i18nlocalization/user/login.action
http://localhost:8080/i18nlocalization/locale.action?request_locale=en
http://localhost:8080/i18nlocalization/locale.action?request_locale=zh_CN
http://localhost:8080/i18nlocalization/locale.action?request_locale=de
http://localhost:8080/i18nlocalization/locale.action?request_locale=fr
程式碼下載 - http://pan.baidu.com/s/1jGCUaJ8