基於SpringMVC國際化資源配置方式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下載:點擊下載

發佈了46 篇原創文章 · 獲贊 40 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章