If the user can choose to customize the language according to their preferences will be a relatively friendly thing, Struts2 can easily realize the user-defined language.
In Struts2, the user's default language can be set by Actioncontext.getcontext (). SetLocale (locale arg). To simplify setup
User default locale. STRUTS2 provides an interceptor called i18n (Interceptor) and registers it in the default interceptor (Defaultstack).
The idea of the program is that the I18N interceptor executes the action method money, and automatically finds a parameter named Request_locale in the request. If the parameter exists, the interceptor converts it as an argument to the locale object and sets it as the user's default locale.
The program will change the page language symbol by passing different parameters (ZH_CN or en_US) to the parameters of the Request_locale. Look at the face:
Create the action package under SRC and create an action named Loginaction.java in the action. Create a resource file in the SRC directory at the same time
Loginresource_en_us.properties and loginresource_zh_cn.properties files, configuration files Struts.properties and Struts.xml.
Loginaction.java
Package action;
Import Com.opensymphony.xwork2.ActionSupport;
public class Loginaction extends Actionsupport {
Private static final long serialversionuid = 1L;
Private String UserName;
private String password;
Public String GetUserName () {
return userName;
}
public void Setusername (String userName) {
This.username = UserName;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
Public String succ () {
Return "Success";
}
public String Lang () {
return "input";
}
}
The two methods behind the Code SECC () and Lang (), succ () correspond to the form data submission, and Lang () essentially dies to implement the refresh of the view page. It's not inside.
What logic, these two methods is to implement page jump. The turn is based on the different values of the Request_locale obtained by the i18n interceptor and the actual different pages
Prompt information.
Struts.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd ">
<struts>
<package name= "Struts2language" extends= "Struts-default" >
<action name= "SUCC" class= "action. Loginaction "method=" SUCC ">
<result name= "Success" >/success.jsp</result>
</action>
<action name= "lang" class= "action. Loginaction "method=" lang ">
<result name= "Input" >/index.jsp</result>
</action>
</package>
</struts>
Struts.properties
Struts.custom.i18n.resources=loginresource
Struts.i18n.encoding =GBK
The first parameter specifies the name of a global resource file. The second parameter is used for the internationalization of the information inside the code, otherwise the process of delivery will appear garbled.
Loginresource_en_us.properties
Login.title=english Page
Login.welcome=welcome to login!
Login.username=username
Login.password=password
Login.success=login success
Login.submit=submit
Loginresource_zh_cn.properties
login.title=\u4e2d\u6587\u9875\u9762
login.welcome=\u6b22\u8fce\u767b\u9646
Login.username=\u767b\u5f55\u8d26\u53f7
login.password=\u767b\u5f55\u5bc6\u7801
login.success=\u767b\u5f55\u6210\u529f
Login.submit=\u786e\u8ba4\u767b\u5f55
Here are two page views
index.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%>
<%@ taglib prefix= "s" uri= "/struts-tags"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title><s:text name= "Login.title" ></s:text></title>
<body>
<a href= "LANG.ACTION?REQUEST_LOCALE=ZH_CN" > Chinese </a>
<a href= "Lang.action?request_locale=en_us" >English</a>
<s:form action= "Succ.action" method= "POST" >
<s:textfield name= "UserName" key= "Login.username" >
</s:textfield>
<s:textfield name= "Password" key= "Login.password" >
</s:textfield>
<s:submit key= "Login.submit" >
</s:submit>
</s:form>
</body>
Success.jps
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%>
<%@ taglib prefix= "s" uri= "/struts-tags"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title><s:text name= "Login.success"/></title>
<body>
<%=request.getattribute ("UserName")%>:<s:text name= "Login.welcome"/>
</body>
The language changes link in the JSP page, and when the user chooses a language link, the hyperlink passes a Request_locale value to the action. And this value will
is automatically read by the i18n Interceptor, i18n sets a new locale based on this value.
Struts2 enable users to choose their own language