spring mvc 国际化的基本使用
基本使用步骤
1.在spring的配置文件里加入
- <!-- Register the welcome.properties -->
- <bean id="messageSource"
- class="org.springframework.context.support.ResourceBundleMessageSource">
- <!-- welcome为文件的前缀 ,message为文件夹 -->
- <property name="basenames" value="message/welcome"/>
- <property name="defaultEncoding" value="utf8" />
- </bean>
2.放入资源文件
message/welcome_en.properties
- welcome.springmvc = english title
message/ welcome_zh.properties
- welcome.springmvc = chongwen title
3.在jsp里使用
- <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
- <h3>
- welcome.springmvc : <spring:message code="welcome.springmvc" text="default text" />
- </h3>
4.结束
上面3步就实现了spring的项目的国际化,默认使用的是头信息里的
- Accept-Language:zh-CN,zh;q=0.8
来判断语言,(决定权在浏览器)
使用cookie或session来判断用户的语言
1.使用cookie
在spring配置文件中加入:
- <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
- <property name="defaultLocale" value="en"/>
- </bean>
这是决定使用cookie来存储用户的语言,里面还可以配置用户的默认语言(也可以不配置)。
2.使用session
- <bean id="localeResolver"
- class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
- <property name="defaultLocale" value="en"/>
- </bean>
3.使用用户浏览器的语言(默认的采用的方法)
- <!--这个就是使用的http头信息里面的接收语言来判断用户的语言 -->
- <bean id="localeResolver"
- class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
- </bean>
修改用户语言
1.使用
spring mvc的配置文件中插入
- <mvc:interceptors>
- <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
- <property name="paramName" value="changeparam"/>
- </bean>
- </mvc:interceptors>
这样就访问 http://localhost:8080?changeparam=en 就可以修改了
2.注意事项
使用浏览器来判断用户语言的方法不能修改,只有在使用Cookie和session的时候才可以修改用户语言。
代码中获取用户语言
- request.getLocale()
相关推荐
NULL 博文链接:https://20142014.iteye.com/blog/1999130
Java EE 框架整合开发⼊⻔到实战——Spring+Spring MVC+MyBatis(微课版)课后习题答案.pdf
Spring MVC+Mybatis整合详解——资源自Linux公社
NULL 博文链接:https://ylxy3058.iteye.com/blog/2230763
Spring MVC Ibatis Bean 根据mysql数据表——代码生成工具
手把手教你如何快速搭建spring mvc环境.zip
通过maven创建spring+spring mvc+mybatis框架 的web项目,适合初学者学习。(使用eclipse、MYSQL和tomcat 7开发)
Spring MVC和Spring Web Flow是Spring平台上两个极为灵活而且功能强大的Web框架。前者是构建在Spring框架上的Web应用程序框架,可以同许多其他视图技术无缝集成;后者是控制业务处理流程的有效解决方案,提供了一种...
...
...
NULL 博文链接:https://ylxy3058.iteye.com/blog/2230071
刘冬编写的Spring.NET,NHibernate,ASP.NET MVC例子 原文:http://www.cnblogs.com/GoodHelper/archive/2009/11/19/SpringNet_Nhibernate_AspNetMvc.html
Spring MVC 学习总结(五)——校验与文件上传 目录 2.2.7、范围 2.2.8、其它注解 2.3、注解控制器参数 1.4、在UI中添加错误标签 1.5、测试运行 三、使用jQuery扩展插件Validate实现前端校验 3.1、jQuery扩展插件...
spring——mvc下的简单程序,适合新手使用
Spring系列——MVC框架整合.md
该演示项目是在“Spring MVC使用篇”系列文章之前的项目工程的基础上搭建的,因此依赖的基础jar包于第二篇文章中已经介绍过了(点击链接即可查看:Spring MVC使用篇(二)—— 环境搭建),这里便不再赘述。
使用Spring MVC3基于注解的形式实现的REST小例子 部署运行: 1. 将工程导入eclipse 2. 添加tomcat属性 工程(右键)——> tomcat project——> update context 3. 启动tomcat 4. 在浏览器中输入...
该毕业设计模仿了微博的功能,使用技术:spring mvc,spring,mybatis,websocket,页面较为清爽。 系统功能包括:分享新鲜事,点赞,收藏,回复等。因为使用了websocket,所以当别人点赞或者回复的时候,服务器端会...
Spring Boot mvc web拦截器实现权限访问控制等功能(csdn)————程序
1. spring杂谈[原创] 1.1 Spring事务处理时自我调用的解决方案及一些实现方式的风险 ...1.32 Spring3 Web MVC下的数据类型转换(第一篇)——《跟我学Spring3 Web MVC》抢先看 1.33 Spring 注入集合类型