Struts 2 – i18n or localization example
2015-09-15 17:27
573 查看
A Struts 2 internationalizing(i18n), localization(i10n) or multi-lingua example to show the use of resource bundle to display the message from different languages. In this example, you will create a simple login screen, display the message from resource bundle via the Struts 2 UI components, and change the locale base on the selected language option.
In some “non-Europe” or “non-English” like characters, you should always encode the content with
To support Struts 2 localization, you HAVE TO declared the
To change the default locale, you just need to declared a “
In Struts 1, there are one standard
6.
Struts 2 configuration file, link it all together.
http://localhost:8080/Struts2Example/locale.action?request_locale=en
http://localhost:8080/Struts2Example/locale.action?request_locale=zh_CN
http://localhost:8080/Struts2Example/locale.action?request_locale=de
http://localhost:8080/Struts2Example/locale.action?request_locale=fr
1. Project Structure
The project structure for this example2. Properties file
Make sure the properties file are named as country specified code.In some “non-Europe” or “non-English” like characters, you should always encode the content with
native2asciitool.
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
3. Action
Two action classes, theLocaleActionis basically do nothing, and the
LoginActionwill do a simple validation and display the error message from resource bundle via
getText().
LocaleAction.java
package com.mkyong.common.action; import com.opensymphony.xwork2.ActionSupport; public class LocaleAction extends ActionSupport{ //business logic public String execute() { return "SUCCESS"; } }
LoginAction.java
package com.mkyong.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")); } } }
4. View page
A login page with a textbox, password and submit UI components.To support Struts 2 localization, you HAVE TO declared the
<%@ page contentType=”text/html;charset=UTF-8″ %>in your view page, else you will have problem to display the “UTF-8 data” correctly, especially the Chinese characters. Read this article about Struts 2 Chinese localization issue.
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>
To change the default locale, you just need to declared a “
request_locale” parameter, set your prefer language code and pass to an Action class. In Struts 2 the
com.opensymphony.xwork2.interceptor.I18nInterceptorinterceptor, which declared in the
struts-default.xml, will hijack your Action class and handle the locale stuff accordingly.
5. Display the resource bundle message?
In Struts 2 , there are many ways to display the resource bundle message base on the selected language or locale. For examples,<s:textfield key="global.username" name="username" /> <s:text name="global.username" /> <s:property value="getText('global.username')" /> <s:text name="global.password" />
In Struts 1, there are one standard
bean:messageto display the resource bundle message, which is more prefer. But in Struts 2, there are so many equivalent ways to display the resource bundle message (even internal work is different), it’s quite confuse at the first glance. Basically, no matter what you choose, Struts 2 also will display the resource bundle message correctly.
6. struts.xml
Struts 2 configuration file, link it all together.<?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.mkyong.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.mkyong.common.action.LocaleAction"> <result name="SUCCESS">user/pages/login.jsp</result> </action> </package> </struts>
7. Demo
http://localhost:8080/Struts2Example/user/login.actionhttp://localhost:8080/Struts2Example/locale.action?request_locale=en
http://localhost:8080/Struts2Example/locale.action?request_locale=zh_CN
http://localhost:8080/Struts2Example/locale.action?request_locale=de
http://localhost:8080/Struts2Example/locale.action?request_locale=fr
相关文章推荐
- JQuery+Strusts1.x无刷新登录
- java struts常见错误以及原因分析
- Struts之logic标签库详解
- struts2的select标签用法实例分析
- SSH框架网上商城项目第20战之在线支付平台
- java中struts 框架的实现
- sprng和struts有什么区别?
- ios多种语言的本地化思路
- SpringMVC+MyBatis项目总结(三)
- MyEclipse7.5+JDK1.6+Tomcat6+Struts1.3+MySQL4.1开发演示
- struts使用拦截器注解
- 懒到极致:对mybatis的进一步精简
- 浅谈struts1和struts2框架(转载+整理)
- Maven搭建struts2+spring+hibernate环境(二)
- Maven搭建struts2+spring+hibernate环境(三)
- 国内首创Java开发者的技术体系
- Struts知识
- struts 获取客户端真实ip
- 转载--Struts1.1 validation framework 使用中的若干经验