在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";
}
<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页面的请求)
- <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>
访问资源文件的方式:
页面上可以: <s:text name=”key”/> 也可以 <s:textfield name=”username” key=”key”/>
Action中访问: getText(“key”)