springmvc国际化 基于请求的国际化配置
springmvc国际化 基于请求的国际化配置
基于请求的国际化配置是指,在当前请求内,国际化配置生效,否则自动以浏览器为主。
项目结构图:
说明: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.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相关,并定义国际化请求处理拦截器,处理用户请求式国际化

<?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>

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

<%@ 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]
springmvc国际化 基于请求的国际化配置的更多相关文章
- springmvc国际化 基于浏览器语言的国际化配置
当前标签: springmvc springmvc国际化 基于浏览器语言的国际化配置 苏若年 2013-10-09 13:03 阅读:305 评论:0 SpringMVC中应用Ajax异步通讯 ...
- SpringMVC数据校验并通过国际化显示错误信息
目录 SpringMVC数据校验并通过国际化显示错误信息 SpringMVC数据校验 在页面中显示错误信息 通过国际化显示错误信息 SpringMVC数据校验并通过国际化显示错误信息 SpringMV ...
- 缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例
之前为大家介绍了如何使用spring注解来进行缓存配置 (EHCache 和 OSCache)的简单的例子,详见 Spring基于注解的缓存配置--EHCache AND OSCache 现在介绍一下 ...
- SpringMVC系列(八)国际化
一.页面国际化 1.在pom.xml引入国际化需要的依赖 <!--国际化相关依赖 begin --> <dependency> <groupId>jstl</ ...
- Spring之SpringMVC(源码)初始化DispatcherServlet策略配置
1.从上一篇文章中可以SpringMVC初始化的过程中完成的其中一件事就是DispatcherServlet的相关策略的配置,如下所示 protected void initStrategies(Ap ...
- spring-mvc.xml 和 application-context.xml的配置与深入理解
在java框架这个话题,前几篇文章是基于搭建ssm项目框架,以及web.xml的配置讲解,本篇主要就ssm框架的其他配置文件进行深入讲解,他们分别是:1.application-context.xml ...
- SpringMVC 入门、请求、响应
目录 SpringMVC 概述 SSM 简介 MVC 简介 SpringMVC 简介 入门案例 Spring 技术架构 SpringMVC 基础配置 常规配置 Controller 加载控制 静态资源 ...
- 学习SpringMVC——如何获取请求参数
@RequestParam,你一定见过:@PathVariable,你肯定也知道:@QueryParam,你怎么会不晓得?!还有你熟悉的他(@CookieValue)!她(@ModelAndView) ...
- Struts2基于注解的Action配置
使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...
随机推荐
- Controller与Action
Controller与Action 我们知道在MVC5和之前的版本,两个框架的生命周期是不一样的,在新版MVC6中,MVC Controller/Web API Controller已经合二为一了,本 ...
- nodejs爬虫系统
其中express是服务端框架 request相当于前端的ajax请求 cheerio相当于jq 开始 首先我们先新建一个 crawler目录 执行 npm install express -g 命令 ...
- C++拷贝构造函数具体解释
一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是非常easy的,比如: int a = 100; int b = a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各 ...
- ajax提交与上传文件同步
我们经常担心文件上传,最烦比,可以推断,我们上传的文件大小,格风格等等一系列的推理验证.所以,我们只能ajax提交验证.ajax异步提交太麻烦,我想太多的变化代码,事实上,我们使用JQuery当插件, ...
- Mac OS X于Android Kernel下载方法
于上一篇日志中,我总结了大家提供的下载Android源代码的方法.这里再简单总结一下内核的下载方法. 參考这里的介绍:http://source.android.com/source/building ...
- SSH—Struts(三)—跑步者(Action)
如已经描述Struts其基本原理和控制器ActionServlet,根据一个请求通过流ActionServlet之后就要到达Action类中做详细的处理了.ActionServlet通过ActionM ...
- MongoDB学习笔记<两>
继续有shell学问,他们继续研究的例子,下面的知识: --文档数据插入 --文档数据删除 --文档数据更新 如下面的详细信息: 1.插入文档 db.person.insert({"name ...
- 財智V6.0(完美破解序列号特别版)
財智V6.0(完美破解序列号特别版) 財智V6.0(完美破解序列号特别版) 財智6是眼下唯一在中央台报道的.比較成熟的国产理財软件.能全面管理家庭的日常收入.消费.储蓄 ...
- pyqt学习总结
一.学习来由: 近期一段时间,应朋友的须要,完毕一款抓取软件.一般而言,python是我比較熟悉的语言,又有丰富的抓取和解析模块,所以果断选择之. 而这远远不是重点,后台程序在工作做常常写,所以比較熟 ...
- linux网络编程学习笔记之四 -----多-threaded服务器
对于使用过程中并发.通过实现更轻量级线程. 每个线程都是一个独立的逻辑流. 主题是CPU在执行调度的最小独立单位,这个过程是资源分配单元.当然,这是在微内核操作系统说.总之,这是唯一的一个操作系统内核 ...