With the development of the network, in Web development, international requirements of the system has become very common. This article explains SpringMVC framework support for multiple languages, only to learn to share, if any inadequacies, please correct me.
What is international?
Internationalization (internationalization), also known as i18n (read law i 18 n, reportedly because of internationalization (international) the word from i to 18 letters between n, i18n is hence the name), which can At the same time deal with access to different regions and countries of the world, and for access to different regions and countries, provide the appropriate page in line with the reading habits of visitors or data.
SpringMVC international support steps
1. Create an international resource files
As shown below, the internationalization resource files * .properties file format, the file name must conform to [base_ language _ region .properties] naming convention, the default is [base.properties] That is, if there is no corresponding language resource file , from the default file search.
1 // default file 2 / ThirdSpringMvc / src / i18n.properties 3 // English - United States 4 / ThirdSpringMvc / src / i18n_en_US.properties 5 // Chinese - China 6 /ThirdSpringMvc/src/i18n_zh_CN.properties
2. The content of international resource files
Global resources stored content files based on the [key] of the key = value form, and if Chinese, corresponding Ascii character is displayed as follows:
hi=\u4F60\u597D me=\u6211 love=\u7231 study=\u5B66\u4E60 mvc=SpringMVC
3. The default configuration file in dispatcher-servlet.xml SpringMVC [file] increase international support
As follows: group name i18n default naming common.
Note: bean's id must be messageSource, otherwise do not correspond
1 <!-- 加载国际化资源文件 --> 2 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 3 <!--<property name="basename" value="i18n"></property>--> 4 <property name="basenames"> 5 <list> 6 <value>i18n</value> 7 </list> 8 </property> 9 </bean>
4. Increase jstl support in the JSP file
As follows: The <% @ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt"%> tag supports the introduction of
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <!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> <body> <fmt:message key="hi"></fmt:message> <fmt:message key="me"></fmt:message> <fmt:message key="love"></fmt:message> <fmt:message key="study"></fmt:message> <fmt:message key="mvc"></fmt:message> </body> </html>
5. Test, open the Settings Language Options
In the IE browser set as follows: Settings -> Internet Options -> Language -> Language Options open, you can move down to set the current language, if not, you can add a language. As follows: The current English - United States (en-US)
The browser displays the contents as follows:
As provided Chinese - Chinese (zh-CN), is shown below:
These are the steps to set SpringMVC multi-language support.
Remark
Magic Cup grape wine, To drink immediately pipa reminder.
Zuiwo battlefield Jun Mo laugh, ancient expedition a few people back?