struts2国际化

在struts.xml里面加入: http://jinlixiang.iteye.com/blog/1494975
http://www.cnblogs.com/langlang/archive/2010/01/14/1647627.html
<constant name="struts.custom.i18n.resources" value="globalMessages" />
如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.custom.i18n.resources" value="globalMessages" />  多个配置文件以逗号分割,资源文件不在根目录时,要添加相对路径,value中的值不能带.properties。
<package name="test" namespace="/test" extends="struts-default">
<!-- 国际化支持 -->
<action name="i18n" class="com.test.web.HelloAction">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>

myeclipse在src下新建

globalMessages_zh_CN.properties
hello=\u4F60\u597D\u4E16\u754C

globalMessages_en_US.properties

hello=Hello World\!

hello.jsp中可以输出
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %> 
<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head> 
<title>test</title>
</head>

<body>
${param.username}
${param.votedate}
<br/>
<s:text name="hello"/>
<br/> 
通过request获取:${requestScope.hello}
</body>
</html>

HelloAction.java里面execute方法可以将hello获取到,并加入大request对象中
public String execute() throws Exception {
this.message="成功登陆";v
  ActionContext.getContext().put("hello", this.getText("hello");
System.out.println(this.getText("hello"));
return "success";
}
 
 

最近做个项目要求国际化,网上搜了很多文章都没什么营养,只能自己摸索着写了

备份一下自己的实现方式,肯定有更好的望指点

 http://blog.csdn.net/del1214/article/details/6538264

1、首先在web.xml中配struts2和一个过滤器(用来过滤所有jsp页面的请求)

  1. <filter>  
  2.     <filter-name>struts2</filter-name>  
  3.     <filter-class>  
  4.         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  5.     </filter-class>     
  6. </filter>  
  7. <filter-mapping>  
  8.     <filter-name>struts2</filter-name>  
  9.     <url-pattern>*</url-pattern>  
  10.     <dispatcher>REQUEST</dispatcher> <!--这两行很重要不配的话无法转发到action-->  
  11.     <dispatcher>FORWARD</dispatcher>  
  12. </filter-mapping>  
  13.   
  14.   
  15.  <filter>  
  16.     <filter-name>international</filter-name>  
  17.     <filter-class>  
  18.         com.i8ntest.filter.International <!--这个是我自己的包名和类名-->  
  19.     </filter-class>  
  20. </filter>  
  21. <filter-mapping>  
  22.     <filter-name>international</filter-name>  
  23.     <url-pattern>*.jsp</url-pattern>  
  24. </filter-mapping>  

 

 

 

2、在struts.xml中配置i18n的资源文件,i18n的国际化拦截器和负责处理jsp国际化的动态结果action(很拗口啊)

  1. <constant name="struts.custom.i18n.resources" value="find,whx,zxx"></constant>  
  2. <!--配置properties文件,value中表示3个文件的开头名字-->  
  3.       
  4. <package name="com.i8ntest.action" extends="struts-default">  
  5.         <!--配置i18n的拦截器实现自动国际化-->  
  6.     <interceptors>  
  7.         <interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>              
  8.     </interceptors>  
  9.     <!--专用的jsp国际化action-->  
  10.     <action name="totalfilter" class="com.i8ntest.action.totalFilter">  
  11.         <result name="url" >${url}</result>           
  12.     </action>  
  13.         <!--测试用的设置语言国际化action-->  
  14.     <action name="setlang" class="com.i8ntest.action.SetLang">              
  15.         <result name="MyJsp">/MyJsp.jsp</result>  
  16.     </action>  
  17. </package>  

 

 

 

3、编写International过滤器过滤jsp请求统统转发到totalFilter.action

     因为struts只能对走action的请求实现自动国际化所以要转发一下

  1. public class International implements Filter {  
  2.     public void destroy() {  
  3.         // TODO Auto-generated method stub   
  4.           
  5.     }  
  6.     public void doFilter(ServletRequest request, ServletResponse response,  
  7.             FilterChain chain) throws IOException, ServletException {  
  8.         HttpServletRequest httpRequest=(HttpServletRequest)request;       
  9.         RequestDispatcher   ds   =   request.getRequestDispatcher( "totalfilter.action");   
  10.         request.setAttribute("jsp", httpRequest.getServletPath());  
  11.         request.setAttribute("param", httpRequest.getQueryString());  
  12.         ds.forward(request,   response);  
  13.     }  
  14.     public void init(FilterConfig filterConfig) throws ServletException {  
  15.         // TODO Auto-generated method stub   
  16.           
  17.     }  
  18. }  

 

 

4、编写totalFilter.action实现动态跳转结果,实现jsp自动国际化

 

  

  1. public class TotalFilter extends ActionSupport {  
  2.   
  3.     /** 
  4.      *  
  5.      */  
  6.     private static final long serialVersionUID = 1L;  
  7.       
  8.     private String url;  
  9.       
  10.     public String getUrl() {  
  11.         return url;  
  12.     }  
  13.       
  14.     public String  execute() throws Exception{  
  15.           
  16.            
  17.         HttpServletRequest request = ServletActionContext.getRequest();  
  18.         Locale locale = (Locale)request.getSession().getAttribute("SessionLocale");  
  19.         if(locale != null){  
  20.             ActionContext.getContext().setLocale(locale);  
  21.         }  
  22.         String result = (String)request.getAttribute("jsp");          
  23.         url = result;  
  24.         if(request.getQueryString() != null){  
  25.             url = url + "?" + request.getQueryString();  
  26.         }  
  27.         return "url";  
  28.     }  
  29. }  

 

 

5、编写测试页面

     setLang.action?request_locale=语言_国家(大写字母)

     参数名必须是request_locale,i18n过滤器会自动获取参数更改session从而实现用户自己选择语言的功能

 

  1. <body>  
  2.     <a href="${pageContext.request.contextPath}/setlang.action?request_locale=zh_CN" >点我中文</a>  
  3.     <a href="${pageContext.request.contextPath}/setlang.action?request_locale=en_US" >clickme English</a>  
  4.   </body>  

 

 

6、setLang.action其实很简单

  1. public class SetLang extends ActionSupport {  
  2.     public String  execute() throws Exception{  
  3.         return "MyJsp";  
  4.     }  
  5. }  

 

 

 

7、MyJsp.jsp中使用struts标签库的s:text标签

  1. <body>    
  2.     <s:text name="find.language"></s:text><br>  
  3.     <s:text name="find.chargename"></s:text><br>  
  4.     <s:text name="navigation.location"></s:text><br>  
  5.     <s:text name="navigation.routesearch"></s:text><br>  
  6.     <s:text name="navigation.destinationselect"></s:text><br>  
  7.     <a href="${pageContext.request.contextPath}/1.jsp" >1</a>  
  8.     <a href="${pageContext.request.contextPath}/2.jsp" >2</a>  
  9. </body>  

访问资源文件的方式:

页面上可以: <s:text name=”key”/> 也可以 <s:textfield name=”username” key=”key”/>

Action中访问: getText(“key”)

 
 
 
 
 
posted @ 2013-01-24 23:07  Brook Legend  阅读(323)  评论(0编辑  收藏  举报