您的位置:首页 > 编程语言 > Java开发

Struts2通过过滤器对每个jsp页面国际化

2011-06-11 16:40 369 查看
最近做个项目要求国际化,网上搜了很多文章都没什么营养,只能自己摸索着写了

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

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

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*</url-pattern>
<dispatcher>REQUEST</dispatcher> <!--这两行很重要不配的话无法转发到action-->
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

<filter>
<filter-name>international</filter-name>
<filter-class>
com.i8ntest.filter.International <!--这个是我自己的包名和类名-->
</filter-class>
</filter>
<filter-mapping>
<filter-name>international</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>


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

<constant name="struts.custom.i18n.resources" value="find,whx,zxx"></constant>
<!--配置properties文件,value中表示3个文件的开头名字-->

<package name="com.i8ntest.action" extends="struts-default">
<!--配置i18n的拦截器实现自动国际化-->
<interceptors>
<interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>
</interceptors>
<!--专用的jsp国际化action-->
<action name="totalfilter" class="com.i8ntest.action.totalFilter">
<result name="url" >${url}</result>
</action>
<!--测试用的设置语言国际化action-->
<action name="setlang" class="com.i8ntest.action.SetLang">
<result name="MyJsp">/MyJsp.jsp</result>
</action>
</package>


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

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

public class International implements Filter {
public void destroy() {
// TODO Auto-generated method stub

}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest=(HttpServletRequest)request;
RequestDispatcher   ds   =   request.getRequestDispatcher( "totalfilter.action");
request.setAttribute("jsp", httpRequest.getServletPath());
request.setAttribute("param", httpRequest.getQueryString());
ds.forward(request,   response);
}
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub

}
}


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

public class TotalFilter extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;

private String url;

public String getUrl() {
return url;
}

public String  execute() throws Exception{

HttpServletRequest request = ServletActionContext.getRequest();
Locale locale = (Locale)request.getSession().getAttribute("SessionLocale");
if(locale != null){
ActionContext.getContext().setLocale(locale);
}
String result = (String)request.getAttribute("jsp");
url = result;
if(request.getQueryString() != null){
url = url + "?" + request.getQueryString();
}
return "url";
}
}


5、编写测试页面

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

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

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


6、setLang.action其实很简单

public class SetLang extends ActionSupport {
public String  execute() throws Exception{
return "MyJsp";
}
}


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

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: