首页 / 更多教程 / 基于Session的国际化实现
基于Session的国际化实现
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了基于Session的国际化实现,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2349字,纯文字阅读大概需要4分钟。
内容图文
![基于Session的国际化实现](/upload/InfoBanner/zyjiaocheng/1296/ecd8a28c261d47f1a2870087e8405828.jpg)
如何将我们网站的其它内容(如菜单、标题等)做国际化处理呢?这就是本篇要将的内容—>国际化。
在项目的spring.xml文件添加的内容如下
1 <mvc:interceptors> 2 <span style="white-space:pre"> </span><!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 --> 3 <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> 4 </mvc:interceptors>
在项目中的源文件夹resources中添加myproperties.properties、myproperties_zh_.properties、myproperties_en_.properties三个文件
下面是jsp页面的一些简单信息如下,仅仅是演示没考虑其他的:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <% 11 Locale name = (Locale) session.getAttribute("i18nlanguage"); 12 ResourceBundle myResourcesBundle = ResourceBundle.getBundle("myproperties",name); 13 %> 14 <body> 15 <a href="${pageContext.request.contextPath}/index/findex.do?langType=en&page=Home">ENG</a> | 16 <a href="${pageContext.request.contextPath}/index/findex.do?langType=zh&page=Home"><%=myResourcesBundle.getString("simplified")%></a> 17 </body> 18 </html>
后台Action层代码如下:
1 package com.zhidao.oms.index; 2 3 import java.util.Locale; 4 5 import javax.servlet.http.HttpServletRequest; 6 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestParam; 10 11 @Controller 12 @RequestMapping("/index") 13publicclass IndexAction { 141516 @RequestMapping("/findex") 17public String Findex(HttpServletRequest request,@RequestParam String langType,String page){ 1819if(langType.equals("zh")){ 20 Locale locale = new Locale("zh", "CN"); 21 request.getSession().setAttribute("i18nlanguage",locale); 22 } 23elseif(langType.equals("en")){ 24 Locale locale = new Locale("en", "US"); 25 request.getSession().setAttribute("i18nlanguage",locale); 26 }else{ 27 request.getSession().setAttribute("i18nlanguage",Locale.getDefault()); 28 } 29return "/front/"+page+".jsp"; 30 } 3132 }
有关的效果图展示大家测试一下就好了!写的不好的地方希望大家批评指正。
原文:http://www.cnblogs.com/jinwufeiyang/p/5823296.html
内容总结
以上是互联网集市为您收集整理的基于Session的国际化实现全部内容,希望文章能够帮你解决基于Session的国际化实现所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。