Struts1 中提交中文表单到ActionForm后出现乱码问题的原因及处理方法
一、乱码出现原因
由于Struts框架直接把表单数据发送给了ActionForm,所以这里面没有对HttpRequestServlet进行SetCharacterEncoding,所以默认是按照ISO-8859-1
(参见Tomcat 源代码中的org.apache.catalina.connector.HttpRequestBase中的protected void parseParameters()方法)
二、解决办法
方法 <1>: 就是在表单提交到ActionForm之前对request进行编码,写一个过滤器,对所有请求进行过滤
(1)过滤器代码如下:
View Code
1 package com.first.struts.filters; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 12 public class SetCharacterEncodingFilter implements Filter { 13 14 private String encoding; 15 private FilterConfig filterConfig = null; 16 17 public void destroy() { 18 this.encoding = null; 19 this.filterConfig = null; 20 21 } 22 23 public void doFilter(ServletRequest request, ServletResponse response, 24 FilterChain chain) throws IOException, ServletException { 25 request.setCharacterEncoding(this.encoding); 26 chain.doFilter(request, response); 27 } 28 29 public void init(FilterConfig filterConfig) throws ServletException { 30 this.encoding = filterConfig.getInitParameter("encoding"); 31 } 32 33 }
(2)web.xml文件配置如下:
View Code
1 <filter> 2 <filter-name>SetCharacterEncoding</filter-name> 3 <filter-class>com.first.struts.filters.SetCharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>GBK</param-value> 7 </init-param> 8 </filter> 9 10 <filter-mapping> 11 <filter-name>SetCharacterEncoding</filter-name> 12 <url-pattern>/*</url-pattern> 13 </filter-mapping> 14 </filter-mapping>
方法 <2>:替换默认的控制器org.apache.struts.action.ActionServlet
(1)子类代码:
View Code
1 package jp.co.ricoh.gtis.others.profile.controllers; 2 3 4 import java.io.IOException; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import org.apache.struts.action.ActionServlet; 11 12 public class SetEncodingActionServlet extends ActionServlet { 13 14 protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 15 // TODO Auto-generated method stub 16 String encoding = getInitParameter("encoding"); 17 request.setCharacterEncoding(encoding); 18 super.process(request, response); 19 } 20 21 }
(2)配置文件web.xml
View Code
1 <servlet> 2 <servlet-name>testAction</servlet-name> 3 <servlet-class>jp.co.ricoh.gtis.others.profile.controllers.SetEncodingActionServlet</servlet-class> 4 <init-param> 5 <param-name>config</param-name> 6 <param-value>/WEB-INF/struts-config.xml</param-value> 7 </init-param> 8 <init-param> 9 <param-name>encoding</param-name> 10 <param-value>Shift_JIS</param-value> 11 </init-param> 12 <load-on-startup>2</load-on-startup> 13 </servlet> 14 15 <servlet-mapping> 16 <servlet-name>testAction</servlet-name> 17 <url-pattern>*.testdo</url-pattern> 18 </servlet-mapping>
http://www.cnblogs.com/gywbg --------------GYW