当前标签: springmvc

 
苏若年 2013-10-09 13:03 阅读:305 评论:0
 
苏若年 2013-04-25 18:58 阅读:1253 评论:2
 
苏若年 2013-04-13 22:21 阅读:1020 评论:1
 
苏若年 2013-04-13 11:37 阅读:976 评论:0

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

项目结构图如下:

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

资源文件

英语的资源文件message_en.properties

main.title=Hello World
main.target=I love you

韩语的资源文件messages_ko.properties

main.title=\uC548\uB155\uD558\uC2ED\uB2C8\uAE4C
main.target=\uC0AC\uB791\uD574

对应的韩语为:

main.title=안녕하십니까
main.target=사랑해


Spring与spring mvc的相关配置

web.xml文件

<?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-mvc的配置文件servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven /> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean> <context:component-scan base-package="com.pudp" />
</beans:beans>

spring配置文件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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx http://www.springframework.org/schema/tx/spring-tx.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"> <!-- 其中basename用来指定properties文件的通用名
如实例中的messages_en.properties,messages_ja.properties通用名都是messages
-->
<property name="basename" value="messages"/>
<property name="useCodeAsDefaultMessage" value="true" /> </bean> <!-- 获取本地 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/> </beans>

说明:servlet-context.xml 只会在servlet做出响应,所以加载信息应该放置在spring配置文件中.

视图层应用

视图层应用前,需要先声明spring标签

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

国际化标签引用

  <body>
<!-- 使用message 标签配置需要显示的国际化文本,
code 对应国际化文件中对应的键的名称 -->
<span style="color: #2D2D2D;">
<spring:message code="main.title" />
</span>
<br>
<input type="text" value="<spring:message code="main.target"/>">
</body>

程序运行结果:

因为我浏览器默认语言是韩语,所以运行结果会展示韩语相关.

更改浏览器默认使用语言.实例中更改为日语,如下图

这时运行程序,系统根据浏览器默认的语言选择日语展示

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

热爱生活,热爱Coding,敢于挑战,用于探索 ...
 
分类: Javaeespring
标签: springmvc

springmvc国际化 基于浏览器语言的国际化配置的更多相关文章

  1. SpringMVC学习(8):国际化

    在系列(7)中我们讲了数据的格式化显示,Spring在做格式化展示的时候已经做了国际化处理,那么如何将我们网站的其它内容(如菜单.标题等)做国际化处理呢?这就是本篇要将的内容->国际化. 一.基 ...

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

    springmvc国际化 基于请求的国际化配置 基于请求的国际化配置是指,在当前请求内,国际化配置生效,否则自动以浏览器为主. 项目结构图: 说明:properties文件中为国际化资源文件.格式相关 ...

  3. 基于Typescript的Vue项目配置国际化

    基于Typescript的Vue项目配置国际化 简介 使用vue-i18n插件对基于Typescript的vue项目配置国际化,切换多种语言, 配合element-ui或者其他UI库 本文以配置中英文 ...

  4. 菜鸟学SSH(三)——Struts2国际化自动检测浏览器语言版

    前几天发了一篇Struts国际化的博客——<菜鸟学习SSH(二)——Struts2国际化手动切换版>,有网友提了一个意见,见下图: 于是就有了下面修改的版本: web.xml <?x ...

  5. js判断浏览器语言实现网站国际化

    一般国际化的网站至少是有中.英文两种语言的,然后就是在不同的语言环境下使用不同的语言页面. 1.实现原理 一般实现这种功能的方法,无非就是两种, 第一种,判断浏览器语言类型: 第二种,判断ip所属国家 ...

  6. 在SpringMVC框架下实现数据的国际化(即数据实现多国文字之间的转换)

    在eclipse中javaEE环境下:导入必要的架包 web.xml配置文件: <?xml version="1.0" encoding="UTF-8"? ...

  7. android国际化(多语言)

    2013-03-18 23:45             13390人阅读             评论(0)             收藏              举报 1.  很大程度上,为什么 ...

  8. DWZ (JUI) 教程 国际化问题(多语言/语言切换)

    DWZ 国际化也是比较简单的,网站的内容国际化和常规的项目国际化是一样的,不要做出特殊的调整. DWZ 自身框架的国际化,比如 翻页的上一页下一页等信息.这些信息都是在dwz.frag.xml 文件当 ...

  9. iOS开发——iOS国际化 APP内语言切换

    最近一个一直在迭代的老项目收到一份新的开发需求,项目需要做国际化适配,简体中文+英文.由于项目中采用了storyboard和纯代码两种布局方式,所以国际化也要同时实现.上网查了些资料,实现了更改系统语 ...

随机推荐

  1. SQL字符串转换为数组

    /*一.按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果. -----rtrim(@str)去掉 @str右边的字符 ltrim(@ ...

  2. B二分法

    <span style="color:#330099;">/* B - 二分法 基金会 Time Limit:1000MS Memory Limit:65536KB 6 ...

  3. java程序连接MongoDB副本集测试

    三个节点有一个节点挂掉也不会影响应用程序客户端对整个副本集的读写! public class TestMongoDBReplSet { public static void main(String[] ...

  4. Appium Server 源码分析之启动运行Express http服务器

    通过上一个系列Appium Android Bootstrap源码分析我们了解到了appium在安卓目标机器上是如何通过bootstrap这个服务来接收appium从pc端发送过来的命令,并最终使用u ...

  5. CSS3实战开发:使用CSS3实现photoshop的过滤效果

    原文:CSS3实战开发:使用CSS3实现photoshop的过滤效果 我们知道,使用Photoshop来调整图像的亮度和对比度,或者将图片转化为灰度等等是很常见的功能.今天我将给大家介绍几个新特性,我 ...

  6. POJ 1028 Web Navigation 题解

    考查代码能力的题目.也能够说是算法水题,呵呵. 推荐新手练习代码能力. 要添加难度就使用纯C实现一下stack,那么就有点难度了,能够使用数组模拟环形栈.做多了,我就直接使用STL了. #includ ...

  7. 编程算法 - 二叉树的深度 代码(C)

    二叉树的深度 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一棵二叉树的根节点, 求该树的深度. 依次选择最深的左右子树, 然后递归加1. ...

  8. Scala Sublime text 3 Build 编译

    使用Sublime text 3编译Scala 在sublime工具栏中[tools]->[Build System]->[new Build System]添加如下文本 { " ...

  9. leetcode[159] Longest Substring with At Most Two Distinct Characters

    找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...

  10. web开发与设计--js数据类型,js运营商

    1. js数据类型划分:号码值类型,布尔,串 由typeof能够看到什么类型的数据被详述. 举例: <span style="font-family:Microsoft YaHei;f ...