springmvc國際化 基於請求的國際化配置

基於請求的國際化配置是指,在當前請求內,國際化配置生效,不然自動以瀏覽器爲主。html

項目結構圖:
java

說明:properties文件中爲國際化資源文件.格式相關見文章http://www.cnblogs.com/dennisit/p/3359008.html

web

這裏不一樣點是,在國際化資源文件中增長參數位.例如:messages_ja.properties以下
spring

main.target=愛してる main.title=こんにちは {0},{1}

 

web.xml文件中聲明spring監聽與上下文資源、spring-mvc應用文件.
瀏覽器

 

<?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-context.xml文件中聲明國家化
spring-mvc

 

<?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:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.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" />    
    
   <!-- 定義國際化消息 說明:dispatcherServlet.xml 只會在servlet作出響應,因此加載信息應該放置在servlet中. -->   
   <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="defaultEncoding" value="UTF-8"/>
     <property name="useCodeAsDefaultMessage" value="true" />
     
   </bean>   
   
</beans>

 

servlet-context.xml文件中聲明springmv相關,並定義國際化請求處理攔截器,處理用戶請求式國際化
mvc

 

<?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:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    
    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

    <context:component-scan base-package="com.pudp" />
    
    
    <!-- 配置基於Session的處理,將提交上來的locale參數進行處理 -->  
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <!-- 該屬性能夠不用配置 -->
        <property name="defaultLocale" value="ja"></property>
    </bean>  
    
    <!-- 國際化請求攔截器處理 -->
    <mvc:interceptors>  
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />  
    </mvc:interceptors>  
    
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    
</beans>

 

視圖層實現國際化請求處理app

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>spring國際化</title>
  </head>
  
  <body style="font-size: 13px;">
  
      <span>
          
          <a href="index.html?locale=zh">中文版</a> | <!-- 對應 messages_zh.properties文件-->
          <a href="index.html?locale=ja">日文版</a> |    <!-- 對應 messages_ja.properties文件-->
          <a href="index.html?locale=ko">韓文版</a> |    <!-- 對應 messages_ko.properties文件-->
          <a href="index.html?locale=en">英文版</a>     <!-- 對應 messages_en.properties文件-->
      </span>
      
      <!-- 使用message 標籤配置須要顯示的國際化文本, code對應國際化文件中對應的鍵的名稱,arguments 對應國際化屬性文件中的參數。 -->
    <p>
           <spring:message code="main.title" arguments="蘇若年,林允熙"/> , <spring:message code="main.target"/>
    </p>
  </body>
</html>

 

springmvc控制器類實現
jsp

 

package com.pudp.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; /** * description: * * @author <a href='mailto:dennisit@163.com'> Cn.蘇若年 (En.dennisit)</a> Copy Right since 2013-10-9 * * com.pudp.controller.ComponentController.java * */ @Controller @RequestMapping(value="/") public class ComponentController { /** * 跳轉到首頁視圖層 * * @author <a href='mailto:dennisit@163.com'>Cn.pudp(En.dennisit)</a> Copy Right since 2013-10-9 下午12:54:54 * * @param request * @param response * @return
     */ @RequestMapping(value={"index","index.html"},method={RequestMethod.GET,RequestMethod.POST}) public ModelAndView windexPage(HttpServletRequest request, HttpServletResponse response){ return new ModelAndView("main/index"); } }

 

程序首次運行結果:
this


由於咱們默認的資源文件爲日文,因此展現日語版.

 

當咱們點擊韓語版本的話,便可將系統的國際化資源設定爲韓語,效果圖以下



轉載請註明出處:[http://www.cnblogs.com/dennisit/p/3366368.html]

在線交談

相關文章
相關標籤/搜索