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

项目结构图:

说明:properties文件中为国际化资源文件.格式相关见文章: http://www.cnblogs.com/dennisit/p/3359008.html

这里不同点是,在国际化资源文件中增加参数位.例如: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>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文件中声明国家化

<?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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://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相关,并定义国际化请求处理拦截器,处理用户请求式国际化

<?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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://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>

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

<%@ 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控制器类实现

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");}
}

程序首次运行结果:

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

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

转载请注明出处:[http://www.cnblogs.com/dennisit/p/3366368.html]

转载于:https://www.cnblogs.com/dennisit/p/3366368.html

springmvc国际化 基于请求的国际化配置相关推荐

  1. springmvc国际化 基于浏览器语言的国际化配置

    项目结构图如下: 说明:lib下存放的是Spring相关包,项目应用包为Spring3.2,message_*.properties中存放的是国际化的资源文件 资源文件 英语的资源文件message_ ...

  2. 基于浏览器请求的国际化实现

    代码: loginForm.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&q ...

  3. Struts2的国际化(一)-国际化资源文件的配置及国际化信息的访问

    一.概述: 1)国际化是一种技术:在程序设计领域,把在无需改写源代码即可让开发出来的应用程序能够支持多种语言和数据格式的技术称为国际化. 2)本地化是一个动作:与国际化对应的是本地化,指让一个具备国际 ...

  4. Vue项目如何实现国际化?分享一下基于vue-i18n实现国际化的经验

    vue项目如何实现国际化?分享一下基于vue-i18n实现国际化的经验 demo源码链接:https://github.com/XieTongXue/how-to/tree/master/vue-in ...

  5. 缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例

    之前为大家介绍了如何使用spring注解来进行缓存配置 (EHCache 和 OSCache)的简单的例子,详见 Spring基于注解的缓存配置--EHCache AND OSCache 现在介绍一下 ...

  6. SpringMvc 04 基于注解的映射器与适配器配置

    SpringMvc的两种基于注解的映射器与适配器配置: 1.通过显式的配置映射器与适配器,并通过自动扫描标签去加载Controller类. <?xml version="1.0&quo ...

  7. 基于ng-alain做国际化

    基于ng-alain做国际化 1.下载需要下载的包 2.加载支持i18n模块 2-1. 修改app.module.ts 使用和修改默认语言 在header.component.html中引用Heade ...

  8. 【SpringMVC 笔记】SpringMVC 原理 + 入门项目(xml 配置版 vs 注解版)

    SpringMVC 入门项目 什么是 SpringMVC? 中心控制器 SpringMVC 执行原理 执行流程 xml 配置版 1.创建一个 Web 项目 2.pom.xml 中导入 SpringMV ...

  9. spring 基于注解的控制器配置

    http://ttaale.iteye.com/blog/787586 spring 基于注解的控制器配置 博客分类: spring SpringBeanServletMVCWeb 13.12. 基于 ...

最新文章

  1. google的gn构建系统
  2. android studio字符串转整型,Android Studio 中的FindBugs插件使用,轻松帮你发现Bug (转)...
  3. pandas 知识点补充:绘图plot
  4. 改变NumericStepper控件上下箭头的外观.
  5. java 图片刷新页面_js修改img的src属性刷新图片时的图片缓存问题
  6. VTK:Utilities之SortDataArray
  7. Lattice Diamond 和 ispLEVER 的不同之处
  8. shiro前后端分离_为什么要前后端分离?前后端分离的优点是什么?
  9. 大学计算机科学不会编码,华中科技大学人员编号编码管理办法
  10. Nginx Learning (1)
  11. C++ STL string字符串内容修改和替换
  12. nsis如何设置运行安装包传参_使用NSIS制作安装包
  13. 创建第一个Android app项目
  14. java。用类描述计算机中CPU的速度和硬盘的容量。要求Java应用程序有4个类,名字分别是PC、CPU、HardDisk和Test,其中Test是主类。
  15. HDU 2121 Ice_cream’s world II (最小树形图+虚根)
  16. 关于梯度消失,梯度爆炸的问题
  17. spring整合mongoDB 和 Redis 极简入门
  18. 浅析Go中三个点(...)用法
  19. 两步开发.NET Core剪裁器,并且开源它
  20. Linux下安装Redis详细步骤具体教程

热门文章

  1. SQL注入漏洞(原理;网页注入)
  2. Codeforces Round #223 (Div. 2): E. Sereja and Brackets(线段树)
  3. bzoj 1685: [Usaco2005 Oct]Allowance 津贴(贪心)
  4. k8s nodeSelector和affinity
  5. js系列教程13-原型、原型链、作用链、闭包全解
  6. matlab2c使用c++实现matlab函数系列教程-deconv函数
  7. MySQL命令窗口下中文显示乱码的解决过程
  8. 零基础学Python--------第3章 流程控制语句
  9. 前端技术分享和发展网站总结
  10. 探寻 JavaScript 逻辑运算符(与、或)的真谛