基于SpringMVC国际化资源配置方式Demo
2016-11-12 15:30
525 查看
一.项目结构:
图
二.操作步骤:
在springmvc.xml配置如下内容
1.配置拦截器
2.配置解析方式
如果是需要在界面上进行切换的话选用Session方式,如果要是浏览器自适应语言的话可以选用cookies方式。
4.将message_en_US.properties和message_zh_CN.properties放到项目的类路径下。
三.示例演示:
springmvc.xml
web.xml
Index.java
message_en_US.properties
message_zh_CN.properties
index.jsp
success.jsp
Demo下载:点击下载
图
二.操作步骤:
在springmvc.xml配置如下内容
1.配置拦截器
<!-- 本地化变更拦截器 --> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<mvc:interceptors> <!-- 国际化资源切换时,根据请求参数中的locale参数自动切换 --> <mvc:interceptor> <mvc:mapping path="/**" /> <ref bean="localeChangeInterceptor" /> </mvc:interceptor> </mvc:interceptors>
2.配置解析方式
如果是需要在界面上进行切换的话选用Session方式,如果要是浏览器自适应语言的话可以选用cookies方式。
<!-- 浏览器自适应语言方式(cookie方式) --> <!-- <bean id="cookieLocaleResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" /> --> <!-- 需要在界面上进行切换(session方式) --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> <!-- 会话属性不存在,默认属性设置 --> <property name="defaultLocale" value="zh_CN" /> </bean>3.配置资源文件读取
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="message" /> <property name="useCodeAsDefaultMessage" value="true" /> </bean>
4.将message_en_US.properties和message_zh_CN.properties放到项目的类路径下。
三.示例演示:
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="com.dqiang"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp"></bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="message" /> <property name="useCodeAsDefaultMessage" value="true" /> </bean>
<!-- 本地化变更拦截器 --> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<!-- 浏览器自适应语言方式(cookie方式) --> <!-- <bean id="cookieLocaleResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" /> --> <!-- 需要在界面上进行切换(session方式) --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> <!-- 会话属性不存在,默认属性设置 --> <property name="defaultLocale" value="zh_CN" /> </bean>
<mvc:interceptors> <!-- 国际化资源切换时,根据请求参数中的locale参数自动切换 --> <mvc:interceptor> <mvc:mapping path="/**" /> <ref bean="localeChangeInterceptor" /> </mvc:interceptor> </mvc:interceptors></beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>springmvc4</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
Index.java
package com.dqiang; import java.util.Locale; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class Index { @Autowired private ResourceBundleMessageSource messagesource; @RequestMapping("/index") public String goIndex(){ return "index"; } @RequestMapping("/lang") public String goSuccess(Locale locale){ String val=messagesource.getMessage("username", null,locale); System.out.println("==="+val+"====>"+locale.getLanguage()); return "success"; } }
message_en_US.properties
username=username1 password=password
message_zh_CN.properties
username=\u7528\u6237\u540D password=\u5BC6\u7801
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here2</title> </head> <body> <a href="lang?locale=en_US">en</a><br/> <a href="lang?locale=zh_CN">zh</a><br/> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> success.jsp </body> </html>
Demo下载:点击下载
相关文章推荐
- 基于SpringMVC国际化资源配置方式
- 基于浏览器首选语言的springmvc和freemarker国际化配置的实现
- 01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器
- Springmvc 基于浏览器国际化配置及使用
- SpringMVC基于代码的配置方式(零配置,无web.xml)
- SpringMvc中配置国际化资源文件
- SpringMVC经典系列-03基于Spring2.5采用XMl配置的方式进行项目开发---【LinusZhu】
- spring 配置国际化资源文件的两种方式
- SpringMVC 国际化消息配置 i18n的正确打开方式
- 企业常用框架Springmvc_基于注解+xml配置方式实现
- springmvc国际化 基于浏览器语言的国际化配置
- springmvc基于xml文件的配置方式
- SpringMVC基于代码的配置方式(零配置,无web.xml)
- spring 配置国际化资源文件的两种方式(转)
- springmvc国际化 基于浏览器语言的国际化配置
- 01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器
- SpringMVC基于代码的配置方式(零配置,无web.xml)
- 当配置 DispatcherServlet拦截“/”,SpringMVC访问静态资源的三种方式
- 基于XMLSchema的简化配置方式也就是Spring中的命名空间以及国际化
- SpringMVC静态资源的配置问题------三种解决方式