Struts2通过过滤器对每个jsp页面国际化
2011-06-11 16:40
369 查看
最近做个项目要求国际化,网上搜了很多文章都没什么营养,只能自己摸索着写了
备份一下自己的实现方式,肯定有更好的望指点
1、首先在web.xml中配struts2和一个过滤器(用来过滤所有jsp页面的请求)
2、在struts.xml中配置i18n的资源文件,i18n的国际化拦截器和负责处理jsp国际化的动态结果action(很拗口啊)
3、编写International过滤器过滤jsp请求统统转发到totalFilter.action
因为struts只能对走action的请求实现自动国际化所以要转发一下
4、编写totalFilter.action实现动态跳转结果,实现jsp自动国际化
5、编写测试页面
setLang.action?request_locale=语言_国家(大写字母)
参数名必须是request_locale,i18n过滤器会自动获取参数更改session从而实现用户自己选择语言的功能
6、setLang.action其实很简单
7、MyJsp.jsp中使用struts标签库的s:text标签
完
备份一下自己的实现方式,肯定有更好的望指点
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>
完
相关文章推荐
- struts2中jsp页面国际化
- 不通过struts2,直接将Json对象转为字符串返回到jsp页面
- struts2下无法用直接通过路径访问.jsp的页面
- struts2通过ajax动态改变系统后台的语言来达到国际化页面切换的功能
- JAVA_WEB Struts2学习:使用验证框架进行校验JSP页面元素 反编译工具的配置 Strtuts2 国际化学习
- struts2项目jsp页面通过js调取action中的封装的json
- 实现Struts2中对未登录的jsp页面进行拦截功能(采用的是Struts2中过滤器进行过滤拦截)
- struts2中的jsp页面通过iterator调取后台action中的list集合
- jquery通过struts2请求,在jsp页面形成二级联动下拉列表
- javaweb国际化,用struts标签解决,注意每个页面都是action跳jsp,并且拦截器的配置使得每次将国际化配置保存在session
- Struts2中Action获取JSP页面传参的方法,及Action获取request的每个方法的区别
- 关于jsp页面通过el表达式访问struts2值栈的问题
- jsp页面通过 request对象直接获取 struts2 Action的变量的值。
- jquery通过struts2请求,返回json数据,在jsp页面形成二级联动下拉列表
- 通过jsp页面向mysql数据库插入中文数据时乱码问题
- struts2的jsp页面如何传值
- jsp页面通过js将时间格式化并赋初始值
- struts2的jsp页面下载文件
- JSP学习笔记(八十四):struts2的页面中调用函数
- struts2使用ajax实现页面和action之间通过json数据交互