当前标签: 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. Tair LDB基于Prefixkey中期范围内查找性能优化项目总结

    "Tair LDB基于Prefixkey该范围内查找性能优化"该项目是仅一个月.这个月主要是熟悉项目..以下从几个方面总结下个人在该项目上所做的工作及自己的个人所得所感. 项目工作 ...

  2. Hard Parse&amp;Soft Parse

    DDL每次运行将必须是难以解决. SQL 解析过程 Oracle对此SQL将进行几个步骤的处理过程: 1.语法检查(syntax check): 检查此sql的拼写是否语法. 2.语义检查(seman ...

  3. php_中替换换行符

    //php 有三种方法来解决 //1.使用str_replace 来替换换行 $str = str_replace(array("\r\n", "\r", &q ...

  4. WIN2003+IIS6+FastCGI+PHP5.4.30的安装配置

    原文:WIN2003+IIS6+FastCGI+PHP5.4.30的安装配置 说明:PHP5.5已不支持win2003了,Win2003最高能安装PHP5.4.30. 安装好系统:并且安装好IIS6. ...

  5. Docker 管理工具 Shipyard

    Docker 管理工具 Shipyard Shipyard 是一个基于 Web 的 Docker 管理工具,支持多 host,可以把多个 Docker host 上的 containers 统一管理: ...

  6. MongoDB学习笔记&lt;两&gt;

    继续有shell学问,他们继续研究的例子,下面的知识: --文档数据插入 --文档数据删除 --文档数据更新 如下面的详细信息: 1.插入文档 db.person.insert({"name ...

  7. Web API 2:Action的返回类型

    Web API 2:Action的返回类型 Web API控制器中的Action方法有如下几种返回类型: void HttpResponseMessage IHttpActionResult 其它类型 ...

  8. MongoDB集群

    高可用的MongoDB集群   1.序言 MongoDB 是一个可扩展的高性能,开源,模式自由,面向文档的数据库. 它使用 C++编写.MongoDB 包含一下特点: l  面向集合的存储:适合存储 ...

  9. cocos2d-x-lua基础系列教程三(lua面向对象)

    lua 类 Lua 事实上不是面向对象语言 我们能够用table 模拟仿照面向对象编程 lua 中的this 类似的是self  table 也具有生命周期 2,使用table  创建类 projed ...

  10. 正则提取文本中的颜色值 #xxxx,不严谨版本

    不严谨但兼容性最好: #[a-fA-F0-9]+ 增加严谨性: #[a-fA-F0-9]{1,6}