随笔-57  评论-170  文章-17  trackbacks-0
  2005年5月13日

      在网上有很多有关Struts的中文解决方案,但是很多都说得很零碎,不够完整。下面是我的一个完整解决方案。
      要使网页能够真正实现多语言,有三个地方都需要修改:
      1.在页面部分,一定要把页面的编码设成UTF-8,就是在开头加上这一句:<%@ page contentType="text/html; charset=UTF-8" %>。并且把所有的文字信息都放到resource文件中。
      以前我在写网页的时候,没有指定页面的编码,总是在获取表单的内容后,要人工的用new String(s.getBytes("ISO8859-1"))转换一下,这样是很繁琐的,而且很容易出错。网页中如果没有指定编码,那么默认的就是用ISO8859-1编码的。
      2.相应的资源文件需要用native2ascii转换一下。
      对于简体中文的资源文件:native2ascii -encoding gbk ApplicationResources_zh.properties convert\ApplicationResources_zh.properties
      对于繁体中文的资源文件:native2ascii -encoding big5 ApplicationResources_zh_tw.properties convert\ApplicationResources_zh_tw.properties

      3.需要用一个filter设置一下request的编码,我的代码如下:

 1import java.io.*;
 2import java.util.*;
 3import javax.servlet.*;
 4import javax.servlet.http.*;
 5
 6/**
 7 * <p>Title: </p>
 8 * <p>Description: </p>
 9 * <p>Copyright: Copyright (c) 2003</p>
10 * <p>Company: </p>
11 * @author George Hill
12 * @version 1.0
13 */

14
15public class CharsetFilter implements Filter {
16
17  private FilterConfig filterConfig;
18
19  /**
20   * Request设置的Charset encoding
21   */

22  private String encoding;
23
24  /**
25   * 是否忽略设置Request的Charset encoding
26   */

27  private boolean ignore;
28
29  //Handle the passed-in FilterConfig
30  public void init(FilterConfig filterConfig) {
31    this.filterConfig = filterConfig;
32
33    encoding = filterConfig.getInitParameter("encoding");
34    String value = filterConfig.getInitParameter("ignore");
35    if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value)
36        || "on".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value)) {
37      ignore = true;
38    }

39  }

40
41  //Process the request/response pair
42  public void doFilter(ServletRequest request, ServletResponse response,
43                       FilterChain chain) throws IOException, ServletException {
44    if (!ignore) {
45      request.setCharacterEncoding(encoding);
46    }

47
48    chain.doFilter(request, response);
49  }

50
51  //Clean up resources
52  public void destroy() {
53    this.filterConfig = null;
54  }

55}

56

      web.xml的相关片断如下:

 1  <filter>
 2    <filter-name>charsetfilter</filter-name>
 3    <filter-class>xxx.CharsetFilter</filter-class>
 4    <init-param>
 5      <param-name>encoding</param-name>
 6      <param-value>UTF-8</param-value>
 7    </init-param>
 8    <init-param>
 9      <param-name>ignore</param-name>
10      <param-value>false</param-value>
11    </init-param>
12  </filter>
13  <filter-mapping>
14    <filter-name>charsetfilter</filter-name>
15    <url-pattern>/*</url-pattern>
16  </filter-mapping>

      这样,在Action中处理表达的内容时,就不需要再做转换;而且在Action中处理数据给页面显示时,也不需要做转换。在页面中可以同时显示简体和繁体的内容,不需要去设置IE的编码。
      另外需要说明的就是如果数据库也支持编码的话,最好也是设成UTF-8编码,这样才能够完整的解决多语言的问题。例如MySQL 4.1以上的版本可以设置编码成utf8,在JDBC的URL中可以指定编码为UTF-8。
posted @ 2005-05-13 14:26 小米 阅读(1763) | 评论 (11)编辑 收藏
     摘要:       由于篇幅较长,所以分开两篇来写。下面是我的一个实际例子,从数据库中分页获取管理员的数据,然后在JSP页面上表示出来。      我的Action的代码:  1import java.util.List; 2import j...  阅读全文
posted @ 2005-05-13 11:33 小米 阅读(3039) | 评论 (24)编辑 收藏
     摘要:       在网页编程中,经常需要利用分页显示数据,下面是我的分页显示的例子。      先说我的设计思路:      1.首先用一个PageData类保存页面相关的数据,有三个域:   &...  阅读全文
posted @ 2005-05-13 11:18 小米 阅读(998) | 评论 (2)编辑 收藏