posts - 42, comments - 7, trackbacks - 0, articles - 1

          struts框架中文問題解決(轉(zhuǎn))

          Posted on 2007-10-11 15:31 TuringJava 閱讀(317) 評(píng)論(0)  編輯  收藏 所屬分類: Structs

          在使用struts框架的J2EE應(yīng)用的Web層面解決中文問題有四處關(guān)鍵:
          1、Web頁面
          2、Response
          3、Request
          4、struts的properties文件
          以下是四處的具體說明。

          1、Web頁面
          在每個(gè)jsp頁開頭加上
          <%@ page
          language="java"
          contentType="text/html; charset=GBK"
          pageEncoding="GBK"
          %>
          <META http-equiv="Content-Type" content="text/html; charset=GBK">
          其中,GBK也可以替換成utf-8。

          2、Response
          1的解決辦法中已經(jīng)覆蓋解決了2。

          3、Request
          最好的解決辦法是寫一個(gè)servlet過濾器,代碼如下:


          package cn.com.extech.tornado;

          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;

          /**

          * @author
          *
          * @version
          */
          public class SetCharacterEncodingFilter implements Filter
          {

           // ------------- Instance Variables
           /**
           * The default character encoding to set for requests that pass through
           * this filter.
           */
           protected String encoding = null;
           /**
           * The filter configuration object we are associated with. If this value
           * is null, this filter instance is not currently configured.
           */
           protected FilterConfig filterConfig = null;
           /**
           * Should a character encoding specified by the client be ignored?
           */
           protected boolean ignore = true; /**
           
           
           /**
           * Take this filter out of service.
           */
           public void destroy() {
           
           this.encoding = null;
           this.filterConfig = null;
           
           }
           
           /**
           * Select and set (if specified) the character encoding to be used to
           * interpret request parameters for this request.
           *
           * @param request The servlet request we are processing
           * @param result The servlet response we are creating
           * @param chain The filter chain we are processing
           *
           * @exception IOException if an input/output error occurs
           * @exception ServletException if a servlet error occurs
           */
           public void doFilter(ServletRequest request, ServletResponse response,
                 FilterChain chain)throws IOException, ServletException {
           
            // Conditionally select and set the character encoding to be used
            if (ignore || (request.getCharacterEncoding() == null)) {
             String encoding = selectEncoding(request);
            if (encoding != null)
             request.setCharacterEncoding(encoding);
            }
            
            // Pass control on to the next filter
            chain.doFilter(request, response);
           
           }
           
           /**
           * Place this filter into service.
           *
           * @param filterConfig The filter configuration object
           */
           public void init(FilterConfig filterConfig) throws ServletException {
           
            this.filterConfig = filterConfig;
            this.encoding = filterConfig.getInitParameter("encoding");
            String value = filterConfig.getInitParameter("ignore");
            if (value == null)
             this.ignore = true;
            else if (value.equalsIgnoreCase("true"))
             this.ignore = true;
            else if (value.equalsIgnoreCase("yes"))
             this.ignore = true;
            else
             this.ignore = false;
           
           }
           
           /**
           * Select an appropriate character encoding to be used, based on the
           * characteristics of the current request and/or filter initialization
           * parameters. If no character encoding should be set, return
           * <code>null</code>.
           * <p>
           * The default implementation unconditionally returns the value configured
           * by the <strong>encoding</strong> initialization parameter for this
           * filter.
           *
           * @param request The servlet request we are processing
           */
           protected String selectEncoding(ServletRequest request) {
           
            return (this.encoding);

           }

          }

          然后在web.xml中增加配置:
           <filter>
            <filter-name>SetCharacterEncodingFilter</filter-name>
            <display-name>SetCharacterEncodingFilter</display-name>
            <filter-class>cn.com.extech.tornado.SetCharacterEncodingFilter</filter-class>
            <init-param>
             <param-name>encoding</param-name>
             <param-value>GBK</param-value>
            </init-param>
            <init-param>
             <param-name>ignore</param-name>
             <param-value>true</param-value>
            </init-param>
           </filter>
           <filter-mapping>
            <filter-name>SetCharacterEncodingFilter</filter-name>
            <servlet-name>action</servlet-name>
           </filter-mapping>
          此方法只有在form里面用提交的方式將客戶端的輸入提交到后臺(tái)才有用,因?yàn)椋@時(shí)候的request的默認(rèn)編碼方式是iso-8859-1,
          如果設(shè)置了request.setCharacterEncoding("GB2312"),就是設(shè)置newString(***.getByte("ISO-8859-1"),"GB2312");而如果
          是用forward方式,再用request.getParameter時(shí),由于用forward提交的param的默認(rèn)編碼方式不一定ISO-8859-1,所以
          request.setCharacterEncoding("GB2312"),可能得不到正確的中文。如何解決forward的param的中文問題呢?

          4、struts的properties文件
          方法一:
          建立ApplicationResources_zh_CN.properties文件并寫入中文,然后在jdk的bin目錄下運(yùn)行
          native2ascii -encoding gb2312 ApplicationResources_zh_CN.properties ApplicationResources_zh_CN_new.properties。
          再將ApplicationResources_zh_CN_new.properties改名成ApplicationResources_zh_CN.properties,覆蓋WEB-INF\class目錄
          下的同名文件即可。

          方法二:

          下載該插件可以非常方便的實(shí)現(xiàn)各種編碼間的轉(zhuǎn)換

          插件下載址:http://propedit.sourceforge.jp/eclipse/updates

          參考:http://blog.csdn.net/finepeace/archive/2004/06/23/23155.aspx

          主站蜘蛛池模板: 金华市| 滦平县| 咸丰县| 衡阳市| 桦甸市| 无为县| 广元市| 蓬安县| 驻马店市| 彭泽县| 海盐县| 章丘市| 长宁区| 星座| 蒙自县| 武宁县| 土默特左旗| 南通市| 黄陵县| 武平县| 来宾市| 通化县| 正阳县| 特克斯县| 保定市| 河南省| 德令哈市| 扶余县| 连平县| 中宁县| 阜宁县| 讷河市| 洪湖市| 东阿县| 延津县| 黎城县| 买车| 合作市| 攀枝花市| 临桂县| 南昌县|