spring mvc 国际化

一、配置xxx-servlet.xml(xxx是项目名称)

<!-- 资源国际化相关配置 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
  <property name="useCodeAsDefaultMessage" value="true" />
  <!-- 默认编码格式为 utf-8 -->
  <property name="defaultEncoding" value="UTF-8" />
  <!-- 国际化信息所在的文件名 -->
  <property name="basenames">
    <list>
      <value>messages123</value>
    </list>
  </property>
</bean>

<!-- 存储区域设置信息SessionLocaleResolver类通过一个预定义会话名将区域化信息存储在会话中从session判断用户语言defaultLocale :默认语言-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
  <!-- <bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver"> -->
  <property name="defaultLocale" value="zh_CN" />
</bean>

<!--该拦截器通过名为”lang”的参数来拦截HTTP请求,使其重新设置页面的区域化信息-->
<mvc:interceptors>
  <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
  </bean>
</mvc:interceptors>

 

二、jsp页面的引用方式

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
  <body>
    <a href="/xxx/index"><spring:message code="login"/></a>
  </body>
</html>

 

三、默认的初始页面需要经过spring controller渲染返回,结合tiles的配置

	
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

<tiles-definitions>

  <definition name="baseLayout" template="/star-view/tiles/main_t.jsp">
    <put-attribute name="menu" value="/star-view/tiles/menu_t.jsp"/>
  </definition>

  <definition name="index" template="/star-view/index.jsp">
  </definition>

  <definition name="/" extends="index">
  </definition>

  <definition name="*" extends="baseLayout">
    <put-attribute name="body" value="/star-view/{1}.jsp"/>
  </definition>

</tiles-definitions>

 

posted @ 2018-02-05 21:59  一沙世界  阅读(288)  评论(0编辑  收藏  举报