熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts裡提交中文表單到ActionForm的亂碼問題

2013-11-23 20:14:04  來源: Java開源技術 

  由於Struts框架直接把表單數據發送給了ActionForm所以這裡面沒有對HttpRequestServlet進行SetCharacterEncoding所以默認是按照ISO(參見Tomcat 源代碼中的orgnnectorHttpRequestBase中的protected void parseParameters()方法)

  解決的方法就是在表單提交到ActionForm之前對request進行編碼
    第一種方法就是寫一個過濾器對所有請求進行過濾

    過濾器代碼

     package ricohgtisothersprofilefilters;

  import javaioIOException;

  import javaxservletFilter;
    import javaxservletFilterChain;
    import javaxservletFilterConfig;
    import javaxservletServletException;
    import javaxservletServletRequest;
    import javaxservletServletResponse;

  public class SetCharacterEncodingFilter implements Filter {
     private String encoding;
     public void init(FilterConfig filterConfig) throws ServletException {
      // TODO Autogenerated method stub
      thisencoding=filterConfiggetInitParameter(encoding);
     }

  public void doFilter(ServletRequest request ServletResponse response
       FilterChain chain) throws IOException ServletException {
      // TODO Autogenerated method stub
      requestsetCharacterEncoding(thisencoding);
      chaindoFilter(requestresponse);
     }

  public void destroy() {
      // TODO Autogenerated method stub

  }

  }


    配置文件webxml

       <filter>
       <filtername>SetCharacterEncodingFilter</filtername>
       <filterclass>ricohgtisothersprofilefiltersSetCharacterEncodingFilter</filterclass>
       <initparam>
        <paramname>encoding</paramname>
        <paramvalue>utf</paramvalue>
       </initparam>
      </filter>

  <filtermapping>
       <filtername>SetCharacterEncodingFilter</filtername>
       <urlpattern>/*</urlpattern>
      </filtermapping>


    第二種方法是替換默認的控制器orgapachestrutsactionActionServlet

  子類代碼

     package ricohgtintrollers;

    import javaioIOException;

  import javaxservletServletException;
    import javaxservlethttpHttpServletRequest;
    import javaxservlethttpHttpServletResponse;

  import orgapachestrutsactionActionServlet;

  public class SetEncodingActionServlet extends ActionServlet {

  protected void process(HttpServletRequest request HttpServletResponse response) throws IOException ServletException {
      // TODO Autogenerated method stub
      String encoding = getInitParameter(encoding);
      requestsetCharacterEncoding(encoding);
      superprocess(request response);
     }

  }

  配置文件webxml

       <servlet>
        <servletname>testAction</servletname>
        <servletclass>ricohgtintrollersSetEncodingActionServlet</servletclass>
        <initparam>
          <paramname>config</paramname>
          <paramvalue>/WEBINF/strutsconfigxml</paramvalue>
        </initparam>
        <initparam>
          <paramname>encoding</paramname>
          <paramvalue>utf</paramvalue>
        </initparam>
        <loadonstartup></loadonstartup>
      </servlet>

  <servletmapping>
        <servletname>testAction</servletname>
        <urlpattern>*testdo</urlpattern>
      </servletmapping>

  此例凡是通過*testdo來請求的數據都會經過參數encoding設定的值來編碼


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28167.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.