Struts国际化问题解决

来源:互联网 发布:基本公共卫生网络直报 编辑:程序博客网 时间:2024/05/07 19:19

第一步:在每个jsp头都加上<%@ page contentType="text/html;charset=GBK" language="java" %>,有需要的话也可以再加上<html:html locale="true"></html:html>表示使用本地编码。 

第二步:解决资源包中的问题,

1) 先将资源包的信息写成自己需要的中文信息,保存成ApplicationResources_zh.properties      
2) 运行cmd到资源包所在目录,运行jdk命令:native2ascii -encoding gb2312 ApplicationResources_zh.properties temp.txt
   运行后会将编码转换成ascii,储存在temp.txt。复制temp.txt中的内容,用它覆盖ApplicationResources_zh.properties原来的内容。      
 3) 不需要更改struts-config.xml中的资源包设置,它会根据浏览器的设置判断,选择相应的资源包。比如在IE的工具-INTERNET选项
  -    语言,选择你需要的语言。你可以选择多种语言,但系统会根据先后关系选择资源包。如果把中文放在前面,系统就会使用ApplicationResources_zh.properties的资源包。

第三步:设置配置文件

解决页面数据传输的中文问题jsp中将中文传给bean,再由bean传给jsp,在页面显示出来是乱码。在WEB.XML中加入字体类,内容如下:
  <filter>
        <filter-name>encodingFilter</filter-name>
     <filter-class>com.hz.jp.util.encodingFilter</filter-class>
    </filter>
    <filter-mapping>
       <filter-name>encodingFilter</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>  这里使用到一个字体类,encodingFilter,源代码在下面,以下是字符转换的方法:

public class encodingFilter implements Filter {

 public void destroy() {
  // TODO 自动生成方法存根

 }

 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
   request.setCharacterEncoding("UTF-8");
   chain.doFilter(request, response);

 }

 public void init(FilterConfig arg0) throws ServletException {
  // TODO 自动生成方法存根

 }

}

原创粉丝点击