Session-based springMvc internationalization and sessionspringmvc

Source: Internet
Author: User

Session-based springMvc internationalization and sessionspringmvc

The springMvc framework is used in the project. We need to dynamically switch the language, find some materials, and finally decide to use session-based Dynamic switching to achieve dynamic switching between Chinese, English, and Korean, it is actually to translate Chinese into other languages for display.

SpringMvc internationalization involves two aspects. One is the internationalization of front-end pages. spring has its own labels for implementation, which is very convenient, and the other is the internationalization of the background java code that involves Chinese characters.

1. Overall project structure

 

The main configuration of bean is configured in the spring-mvc.xml, and the format of the content to be translated in the messages folder is as follows: key = value format

English:

Chinese:

Korean:

Note that the Chinese characters involved in the configuration file must be converted to unicode encoding. Otherwise, garbled characters may occur after translation.

2. Add the following configuration in spring-mvc:
1 <! -- International resource configuration, resource file binder --> 2 <bean id = "messageSource" class = "org. springframework. context. support. ReloadableResourceBundleMessageSource"> 3 <! -- Configure the international resource file and specify the storage location of the properties file --> 4 <property name = "basename" value = "classpath: messages/message"/> 5 <! -- If the corresponding Code cannot be found in the international resource file, use this code as the name --> 6 <property name = "useCodeAsDefaultMessage" value = "true"/> 7 </bean> 8 <! -- Dynamically switch to the international market and put the international market in the session --> 9 <bean id = "localeResolver" class = "org. springframework. web. servlet. i18n. sessionLocaleResolver "> </bean> 10 <mvc: interceptors> 11 <! -- If the international operation interceptor is based on (Request/Session/Cookie), you must configure --> 12 <bean class = "org. springframework. web. servlet. i18n. localeChangeInterceptor "> 13 <! -- Use this parameter to obtain the configuration file --> 14 <property name = "paramName" value = "language"/> 15 </bean> 16 </mvc: interceptors>

Note that the basename value must be configured to the messages/message level. That is, the location where international files are stored until the first underline.

3. Foreground page implementation

 

1 <% @ page language = "java" contentType = "text/html; charset = UTF-8 "2 pageEncoding =" UTF-8 "%> 3 <% @ taglib prefix =" spring "uri =" http://www.springframework.org/tags "%> 4 <% @ taglib prefix =" mvc "uri = "http://www.springframework.org/tags/form" %> 5 <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" http://www.w3.org/TR/html4/loose.dtd "> 6 

Note that the spring label is introduced to the page. The code is written in Chinese, corresponding to the keys in the three translation files. If the code does not find the corresponding key in the translation file, use the content in the code. This configuration is specified in spring '-mvc.

 

4. Background code implementation
1 package com. ccg. controller; 2 3 4 5 import javax. servlet. http. httpServletRequest; 6 import javax. servlet. http. httpServletResponse; 7 8 import org. springframework. stereotype. controller; 9 import org. springframework. web. bind. annotation. requestMapping; 10 import org. springframework. web. servlet. modelAndView; 11 import org. springframework. web. servlet. support. requestContext; 12 13 14 @ Controller15 @ RequestMapping ("test") 16 public class TestController {17 18 @ RequestMapping ("view") 19 public ModelAndView view (HttpServletRequest request, HttpServletResponse response) {20 // use req for spring translation. getmessage () method 21 RequestContext req = new RequestContext (request); 22 ModelAndView model = new ModelAndView ("test"); 23 model. addObject ("welcome", req. getMessage ("welcome"); 24 return model; 25} 26}
5. effect display

Summarize the key points of springMvc internationalization:

1) Specify the file location to be translated for spring internationalization. Note that the path must be completely written.

2) Specify the parameter for internationalization of spring. I used language in the previous article. After configuration, spring will find the corresponding configuration file based on the value of this parameter in the request.

3) Front-end page using spring labels, code corresponds to the key in the configuration file, it is recommended to use Chinese, in case the corresponding does not display the content in the code by default (need to specify the configuration in the spring-mvc.xml)

4) if the background code is internationalized, use the getMessage method of the RequestContext object and return a String.

 

The above is my international implementation of springMvc. If you have any questions, please leave a message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.