Internationalization of sessions:
Other links: http://blog.csdn.net/jinwufeiyang/article/details/52338268
How can we make other content (such as menus and titles) of our website internationalized? This is what we will discuss in this article-> internationalization.
Add the following content to the spring. xml file of the project:
[Html]View plain copy
- <Mvc: interceptors>
- <Span style = "white-space: pre"> </span> <! -- If the international operation interceptor is based on (Request/Session/Cookie), it must be configured. -->
- <Bean class = "org. springframework. web. servlet. i18n. LocaleChangeInterceptor"/>
- </Mvc: interceptors>
Add the myproperties. properties, myproperties_zh _. properties, and myproperties_en _. properties files to the source folder resources of the project.
The following is some simple information about the jsp page, which is not considered in the Demonstration:
[Html]View plain copy
- <% @ Page language = "java" contentType = "text/html; charset = UTF-8"
- PageEncoding = "UTF-8" %>
- <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
- <Html>
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
- <Title> Insert title here </title>
- </Head>
- <%
- Locale name = (Locale) session. getAttribute ("i18nlanguage ");
- ResourceBundle myResourcesBundle = ResourceBundle. getBundle ("myproperties", name );
- %>
- <Body>
- <A href = "$ {pageContext. request. contextPath}/index/findex. do? LangType = en & page = Home "> ENG </a> |
- <A href = "$ {pageContext. request. contextPath}/index/findex. do? LangType = zh & page = Home "> <% = myResourcesBundle. getString (" simplified ") %> </a>
- </Body>
- </Html>
The code for the background Action layer is as follows:
[Html]View plain copy print?
- Package com. zhidao. oms. index;
- Import java. util. Locale;
- Import javax. servlet. http. HttpServletRequest;
- Import org. springframework. stereotype. Controller;
- Import org. springframework. web. bind. annotation. RequestMapping;
- Import org. springframework. web. bind. annotation. RequestParam;
- @ Controller
- @ RequestMapping ("/index ")
- Public class IndexAction {
- @ RequestMapping ("/findex ")
- Public String Findex (HttpServletRequest request, @ RequestParam String langType, String page ){
- If (langType. equals ("zh ")){
- Locale locale = new Locale ("zh", "CN ");
- Request. getSession (). setAttribute ("i18nlanguage", locale );
- }
- Else if (langType. equals ("en ")){
- Locale locale = new Locale ("en", "US ");
- Request. getSession (). setAttribute ("i18nlanguage", locale );
- } Else {
- Request. getSession (). setAttribute ("i18nlanguage", Locale. getDefault ());
- }
- Return "/front/" + page + ". jsp ";
- }
- }
Let's test the presentation! I hope everyone will criticize and correct the poor writing.