35、(知识篇)SpringMVC12 SpringMVC 超链接国际化

/**
* SpringMVC 超链接国际化

* 1、在SpringMVC中配置 SessionLocaleResolver 的Bean
* <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
*  需要写上id,不写则会报错,可能SpringMVC源码用到,有空再研究SpringMVC
* 2、在SpringMVC中配置 LocaleChangeInterceptor 的拦截器Interceptor
* org.springframework.web.servlet.i18n.LocaleChangeInterceptor
* 3、url通过带参locale=zh_CN/locale=en_US
* 则会读取properties中的翻译文件


*/

测试类:

package com.spring.test;

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 TestController {

	
	/**
	 * SpringMVC 超链接国际化
	 * 
	 * 1、在SpringMVC中配置 SessionLocaleResolver 的Bean
	 * 	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
	 *  需要写上id,不写则会报错,可能SpringMVC源码用到,有空再研究SpringMVC
	 * 2、在SpringMVC中配置 LocaleChangeInterceptor 的拦截器Interceptor
	 * 	org.springframework.web.servlet.i18n.LocaleChangeInterceptor
	 * 3、url通过带参locale=zh_CN/locale=en_US
	 * 	则会读取properties中的翻译文件
	 * 
	 * 
	 */
	
	@Autowired
	private ResourceBundleMessageSource messageSource;
	
	@RequestMapping("i18N")
	public String i18N(Locale locale){
		String result = messageSource.getMessage("i18N.age", null, locale);
		System.out.println(result);
		return "index";
	}
	
}

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"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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-4.3.xsd">
	<context:component-scan base-package="com.spring.test"></context:component-scan>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename" value="i18N"></property>
	</bean>
	
	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
	<mvc:interceptors>
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
	</mvc:interceptors>
	
	<mvc:view-controller path="/index" view-name="index"/>
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven></mvc:annotation-driven>
	
	
</beans>

i18N.properties

i18N.age=the age must more than 20

i18N_zh_CN.properties

i18N.age=\u5E74\u9F84\u4E0D\u80FD\u5C0F\u4E8E20\u5C81\u5462


index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/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>Test i18N</title>
</head>
<body>
	<fmt:message key="i18N.age"></fmt:message>
	<br>
	<a href="i18N?locale=zh_CN">中文</a>
	<br>
	<a href="i18N?locale=en_US">英文</a>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值