使用struts2 做国际化
ChangeLanguageAction.java
public class ChangeLanguageAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 744732104102747185L;
@Override
public String execute() throws Exception {
ActionContext context = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
String request_locale = request.getParameter("request_locale");
Locale LOCALE = (Locale)context.getSession().get("WW_TRANS_I18N_LOCALE");
System.out.println(LOCALE.getCountry());
System.out.println(request_locale);
ActionContext.getContext().setLocale(LOCALE);
return "success";
}
}
loginAction.java
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 8837849417087243469L;
private String userName;
private String eamil;
@Override
public String execute() throws Exception {
String usrname = getText("username"); // 获取
String password1 = getText("password1");
System.out.println(usrname);
System.out.println(password1);
ActionContext context = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
System.out.println(getText("success"));
// String result = getText("success",new String[]{username});
context.put("message", this.getText("success",new String[]{userName}));
return SUCCESS;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEamil() {
return eamil;
}
public void setEamil(String eamil) {
this.eamil = eamil;
}
}
package tutorial;
Locales .java
public class Locales {
public Map<String, Locale> getLocales() {
Map<String, Locale> locales =new Hashtable<String, Locale>(2);
locales.put("Simplified Chinese", Locale.CHINA);
locales.put("American English", Locale.US);
return locales;
}
}
globalMessages_en_US.properties
HelloWorld=Hello World!
username=User Name
password1=Password1
passwordagain=Password Again
submit=Submit
doregist=do regist
email=email
success={0}login success
globalMessages_zh_CN.properties
username=/u7528/u6237/u540d
email=/u90ae/u7bb1
success={0} /u767b/u9646/u6210/u529f
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml" />
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.custom.i18n.resources" value="globalMessages"></constant>
<package name="" extends="struts-default">
<action name="loginAction" class="com.liuc.web.LoginAction">
<result name="success">/success.jsp</result>
<result name="fail">/error.jsp</result>
</action>
<action name="changeLanguage" class="com.liuc.web.ChangeLanguageAction">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
web.xml
<!-- 监听器采用观察者模式 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
index.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
<s:include value="/LangSelector.jsp"></s:include>
<form action="loginAction" method="post">
<table>
<tr>
<td><s:text name="username"></s:text></td>
<td><input type="text" name="userName"> </td>
</tr>
<tr>
<td>
<s:text name="email"></s:text>
</td>
<td><input type="text" name="email"> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="" value="<s:text name='submit'/>" > </td>
</tr>
</table>
</form>
LangSelector.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
<script type="text/javascript">
<!--
function langSelecter_onChanged() {
document.langForm.submit();
}
//-->
// <s:url includeParams="get" encode="true"/> -->
</script>
<body>
<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/>
<s:bean id="locales" name="tutorial.Locales" />
<form action="changeLanguage.action" name="langForm"
style="background-color: powderblue; padding-top: 4px; padding-bottom: 4px;">
Language: <s:select label="Language"
list="#locales.locales" listKey="value" listValue="key"
value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"
name="request_locale" id="langSelecter"
onchange="langSelecter_onChanged()" theme="simple"/>
</form>
</body>
success.jsp
<body>
${message }
</body>
效果图如下: