[Background knowledge for solving the problem] The Struts2 framework provides the i18n interceptor, which functions as follows:
(1) The i18n interceptor automatically finds a parameter named request_locale in the request before executing the Action method. If this parameter exists, the interceptor converts it into a Locale object and sets it as the user's default Locale.
(2) The i18n interceptor will also save the Locale object generated above in the attribute named "WW_TRANS_I18N_LOCALE" of the user Session.
(3) Once there is an attribute named "WW_TRANS_I18N_LOCALE" in the user's Session, the Locale specified by this attribute will be used as the browser's default Locale.
[Specific steps]
1. Add a key-value pair to the resource file and add it in
English: language=Select Language
and add it in Chinese (requires transcoding): language=Select language
2. Create the JavaBean class Locales.java, the key code is as follows:
public class Locales { private Locale current; private Map<String,Locale> locales; public Locale getCurrent() { return current; } public void setCurrent(Locale current) { this.current = current; } public Map<String, Locale> getLocales() { Map<String, Locale> locales=new Hashtable<String, Locale>(); locales.put("Simplified Chinese", Locale.CHINA); locales.put("American English", Locale.US); return locales; }3. Add the following code to login.jsp
(1) Add between <head> and </head> of the web page:
<script type="text/javascript"> function langSelecter_onChanged() { document.langForm.submit(); } </script>
Analysis: Define a JavaScript function whose function is to submit the form (set the onchange event handler in the drop-down list box, which is called when the value of the list box changes)
(2) Add the following code to the <body> of login.jsp
<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"> <s:bean name="com.edu.entity.Locales" id="locales"> <s:param name="current" value="SESSION_LOCALE==null?locale:SESSION_LOCALE" /> </s:bean> </s:set> <s:form action="language" name="langForm"> <s:select list="#locales.locales" key="language" listKey="value" listValue="key" value="#locales.current" name="request_locale" onchange="langSelecter_onChanged()"></s:select> </s:form>
4. Realize display