程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Struts中提交中文表單到ActionForm的亂碼問題解決辦法

Struts中提交中文表單到ActionForm的亂碼問題解決辦法

編輯:關於JAVA

由於Struts框架直接把表單數據發送給了ActionForm,所以這裡面沒有對HttpRequestServlet進行SetCharacterEncoding,所以默認是按照ISO-8859-1(參見Tomcat 源代碼中的org.apache.catalina.connector.HttpRequestBase中的protected void parseParameters()方法),解決的方法,就是在表單提交到ActionForm之前對request進行編碼。

第一種方法,就是寫一個過濾器,對所有請求進行過濾

過濾器代碼:

package jp.co.ricoh.gtis.others.profile.filters;
   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 {
   private String encoding;
   public void init(FilterConfig filterConfig) throws ServletException {
   // TODO Auto-generated method stub
   this.encoding=filterConfig.getInitParameter("encoding");
   }
   public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
   // TODO Auto-generated method stub
   request.setCharacterEncoding(this.encoding);
   chain.doFilter(request,response);
   }
   public void destroy() {
   // TODO Auto-generated method stub
   }
  }

配置文件web.xml

<filter>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <filter-class>jp.co.ricoh.gtis.others.profile.filters.SetCharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
    </init-param>
   </filter>
   <filter-mapping>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
   </filter-mapping>

第二種方法是替換默認的控制器org.apache.struts.action.ActionServlet子類代碼:

package jp.co.ricoh.gtis.others.profile.controllers;
  import java.io.IOException;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.struts.action.ActionServlet;
  public class SetEncodingActionServlet extends ActionServlet {
   protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
   // TODO Auto-generated method stub
   String encoding = getInitParameter("encoding");
   request.setCharacterEncoding(encoding);
   super.process(request, response);
   }
  }

配置文件web.xml

<servlet>
    <servlet-name>testAction</servlet-name>
    <servlet-class>jp.co.ricoh.gtis.others.profile.controllers.SetEncodingActionServlet</servlet-class>
    <init-param>
     <param-name>config</param-name>
     <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
     <param-name>encoding</param-name>
     <param-value>utf-8</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
   </servlet>
  <servlet-mapping>
    <servlet-name>testAction</servlet-name>
    <url-pattern>*.testdo</url-pattern>
   </servlet-mapping>

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved