Posted on 2005-09-20 20:56
qiyadeng 阅读(379)
评论(0) 编辑 收藏 所属分类:
J2EE
对Struts实行国际化
(1)设置字符编码
为了保证在同一JSP页面能支持多种语言,可以将所有JSP页面的字符编码统一设置为"UTF-8"
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
(2)对文本,按钮,图片国际化
(3)创建英文,中文资源文件
(4)对中文资源文件进行编码转换
用JDK中的native2ascii工具对中文资源进行转换
native2ascii -encoding gb2312 applicaion_CN.properties application_CN.properties
(5)采用Servlet过滤器设置请求数据的字符编码
添加如下类SetCharacterEncodingFilter:
package com.yourcompany.struts;import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SetCharacterEncodingFilter implements Filter {
public void init(FilterConfig arg0) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
chain.doFilter(request,response);
}
public void destroy() {
}
}
并在web.xml中添加如下设置:
<filter>
<filter-name>Character Encoding</filter-name>
<filter-class>com.yourcompany.struts.SetCharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>