当前标签: springmvc
苏若年 2013-04-13 11:37 阅读:976 评论:0
springmvc国际化 基于浏览器语言的国际化配置
项目结构图如下:
说明:lib下存放的是Spring相关包,项目应用包为Spring3.2,message_*.properties中存放的是国际化的资源文件
资源文件
英语的资源文件message_en.properties
main.title=Hello World
main.target=I love you
韩语的资源文件messages_ko.properties
main.title=uC548uB155uD558uC2EDuB2C8uAE4C
main.target=uC0ACuB791uD574
对应的韩语为:
main.title=안녕하십니까
main.target=사랑해
Spring与spring mvc的相关配置
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context/spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring-mvc的配置文件servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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.xsd">
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.pudp" />
</beans:beans>
spring配置文件spring-context.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.pudp" />
<!-- 定义国际化消息-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 其中basename用来指定properties文件的通用名
如实例中的messages_en.properties,messages_ja.properties通用名都是messages
-->
<property name="basename" value="messages"/>
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<!-- 获取本地 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
</beans>
说明:servlet-context.xml 只会在servlet做出响应,所以加载信息应该放置在spring配置文件中.
视图层应用
视图层应用前,需要先声明spring标签
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
国际化标签引用
<body>
<!-- 使用message 标签配置需要显示的国际化文本,
code 对应国际化文件中对应的键的名称 -->
<span style="color: #2D2D2D;">
<spring:message code="main.title" />
</span>
<br>
<input type="text" value="<spring:message code="main.target"/>">
</body>
程序运行结果:
因为我浏览器默认语言是韩语,所以运行结果会展示韩语相关.
更改浏览器默认使用语言.实例中更改为日语,如下图
这时运行程序,系统根据浏览器默认的语言选择日语展示
转载请注明出处:[http://www.cnblogs.com/dennisit/p/3359008.html]
热爱生活,热爱Coding,敢于挑战,用于探索 ...