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

来源:互联网 发布:算法分析的主要方面是 编辑:程序博客网 时间:2024/05/15 11:06
/**
* 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;@Controllerpublic 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中的翻译文件 *  *  */@Autowiredprivate 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.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://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>


0 0
原创粉丝点击