springmvc国际化 基于请求的国际化配置

阅读更多

基于请求的国际化配置是指,在当前请求内,国际化配置生效,否则自动以浏览器为主。

 

项目结构图:
springmvc国际化 基于请求的国际化配置_第1张图片

 



 

这里不同点是,在国际化资源文件中增加参数位.例如:messages_ja.properties如下

 

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>contextConfigLocationparam-name>
        <param-value>/WEB-INF/context/spring-context.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>/WEB-INF/context/servlet-context.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jspwelcome-file>
    welcome-file-list>
web-app>
复制代码

 

 

 

spring-context.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: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" />    
    
      
   <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">   
     
     
     <property name="basename" value="messages" />
     <property name="defaultEncoding" value="UTF-8"/>
     <property name="useCodeAsDefaultMessage" value="true" />
     
   bean>   
   
beans>
复制代码

 

 

 

servlet-context.xml文件中声明springmv相关,并定义国际化请求处理拦截器,处理用户请求式国际化

 

 

 

复制代码
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">

    
    
    
    <mvc:annotation-driven />

    <context:component-scan base-package="com.pudp" />
    
    
      
    <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>  
    
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    bean>
    
    
beans>
复制代码

 

 

 

视图层实现国际化请求处理

 

 

 

复制代码
<%@ 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> | 
          <a href="index.html?locale=ja">日文版a> |    
          <a href="index.html?locale=ko">韩文版a> |    
          <a href="index.html?locale=en">英文版a>     
      span>
      
      
    <p>
           <spring:message code="main.title" arguments="苏若年,林允熙"/> ,
           <spring:message code="main.target"/>
    p>
  body>
html>
复制代码

 

 

 

springmvc控制器类实现

 

 

 

复制代码
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  Cn.苏若年 (En.dennisit) Copy Right since 2013-10-9 
 *
 * com.pudp.controller.ComponentController.java
 *
 */

@Controller
@RequestMapping(value="/")
public class ComponentController {

    /**
     * 跳转到首页视图层
     *
     * @author Cn.pudp(En.dennisit) 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");
    }
}
复制代码

 

 

 

程序首次运行结果:

 


 

因为我们默认的资源文件为日文,所以展示日语版.

 

 

 

当我们点击韩语版本的话,即可将系统的国际化资源设定为韩语,效果图如下

你可能感兴趣的:(Spring,MVC,i18n)