struts2国际化语言切换

1.首先把Struts2的环境搭建起来,

 

2.建立一个action.测试i18n的。

 

3.下面这个是struts.xml的简单配置,里有2中properties文件的配置,一种是全局的,一种是局部的,

Xml代码

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5.  
  6. <struts>
  7. <constant name="struts.devMode" value="true" />
  8. <!-- 局部的配置 -->
  9. <!--
  10. <constant name="struts.custom.i18n.resources" value="com/test/action/I18n"></constant>
  11. -->
  12. <!-- 全局的配置 -->
  13. <!-- -->
  14. <constant name="struts.custom.i18n.resources" value="test"></constant>
  15. <package name="default" namespace="/" extends="struts-default">
  16. <action name="" class="com.test.action.I18nAction">
  17. <result >/index.jsp</result>
  18. </action>
  19. </package>
  20. </struts>

 

4.根据struts2的配置,插件一个名字为test_en_US.properties和test_zh_CN.properties的配置文件,

test_en_US.properties里面的内容为:hello=hi,hello

test_zh_CN.properties里面的内容为:hello=\u4F60\u597D(注意了:这个是通过编码编译过来的,也可以试用MyEclipse的properties自动编辑转换实现)。

 

5.下面是jsp的展现页面:本人整理了以下几种实现方法,

 

 

Html代码

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7.  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12.  
  13. <title>My JSP 'index.jsp' starting page</title>
  14. <meta http-equiv="pragma" content="no-cache">
  15. <meta http-equiv="cache-control" content="no-cache">
  16. <meta http-equiv="expires" content="0">
  17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18. <meta http-equiv="description" content="This is my page">
  19. </head>
  20.  
  21. <body>
  22. <a href="<%=basePath%>?local=zh_CN">中文</a>
  23. <a href="<%=basePath%>?local=en_US">英文</a>
  24. This is my JSP page. <br>
  25. <s:debug></s:debug>
  26. property:<s:property value="getText('hello')"/><br>
  27. text:<s:text name="hello"></s:text><br>
  28. i18n:<s:i18n name="test">
  29. <s:text name="hello"></s:text>
  30. </s:i18n>
  31. </body>
  32. </html>

 

 

6.想要实现中英文切换,还要在action中加入这一一句话

Java代码

  1. Locale locale=new Locale("zh","CN");//(这个能根据你传来的值动态改变)
  2. ServletActionContext.getRequest().getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale);

 

7.基本上可以实现动态链接切换中英文了,不过,还有个小问题,需要解决,那就是,需要点击2下中文才能切换到中文,

英文同样也是,这个问题怎么解决呢?

 

8.想解决那个问题其实很简单,配置一个fitler拦截器就行了,这个拦截器最好配置在struts2的拦截器前面,

拦截器的内容大概是:

Java代码

  1. String local=arg0.getParameter("local");
  2. if(local!=null){
  3. String loc[]=local.split("_");
  4. Locale locale=new Locale(loc[0],loc[1]);
  5. ((HttpServletRequest)arg0).getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale);
  6. }
  7. arg2.doFilter(arg0, arg1);

这样就能实现动态切换中英文了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值