你的位置:首页 > Java教程

[Java教程]Struts1 中提交中文表单到ActionForm后出现乱码问题的原因及处理方法


问题:

在你所有页面设置的编码都是utf-8的情况下仍然会出现中文乱码现象。

一、乱码出现原因

由于Struts框架直接把表单数据发送给了ActionForm,所以这里面没有对HttpRequestServlet进行SetCharacterEncoding,所以默认是按照ISO-8859-1。

二、解决办法

方法 : 就是在表单提交到ActionForm之前对request进行编码,写一个过滤器,对所有请求进行过滤

方式一:直接在过滤器中指定编码格式

MyFilter.java

 1 public class MyFilter extends HttpServlet implements Filter { 2   private static final long serialVersionUID = 1L; 3     4  5   public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws java.io.IOException, javax.servlet.ServletException {  6     // TODO Auto-generated method stub 7      arg0.setCharacterEncoding("utf-8"); //设置接收编码 8     arg1.setContentType("text/html;charset=utf-8"); 9     arg2.doFilter(arg0, arg1);  //必须10   11   }12 13   14   public void init(FilterConfig arg0) throws javax.servlet.ServletException { 15     // TODO Auto-generated method stub16   }17 18 }

View Code

在web.

1 <filter>2   <filter-name>MyFilter</filter-name>3   <filter-class>com.xidian.filter.MyFilter</filter-class>4  </filter>5  <filter-mapping>6   <filter-name>MyFilter</filter-name>7   <url-pattern>/*</url-pattern>8  </filter-mapping>

View Code

 

方式二:在web.

MyFilter.java

 1 package com.xidian.filter; 2  3 import javax.servlet.Filter; 4 import javax.servlet.FilterChain; 5 import javax.servlet.FilterConfig; 6 import javax.servlet.ServletRequest; 7 import javax.servlet.ServletResponse; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet;10 11 public class MyFilter extends HttpServlet implements Filter {12   private static final long serialVersionUID = 1L;13   private String encoding; 14 15   public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws java.io.IOException, javax.servlet.ServletException { 16     // TODO Auto-generated method stub17       arg0.setCharacterEncoding(encoding);18       arg2.doFilter(arg0, arg1);19   }20 21   public void init(FilterConfig arg0) throws javax.servlet.ServletException { 22     // TODO Auto-generated method stub23     encoding=arg0.getInitParameter("encoding");24   }25 26 }

View Code

在web.

 1 <filter> 2   <filter-name>MyFilter</filter-name> 3   <filter-class>com.xidian.filter.MyFilter</filter-class> 4   <init-param> 5   <param-name>encoding</param-name> 6   <param-value>utf-8</param-value> 7   </init-param>  8  </filter> 9  <filter-mapping>10   <filter-name>MyFilter</filter-name>11   <url-pattern>/*</url-pattern>12  </filter-mapping>

View Code

 

下面是struts2中中文乱码的问题:

关于struts2中表单提交时,中文乱码问题的解决

 

顺带再说下加上对输入数据内容的过滤的情况:

再添加一个过滤程序:

SafeFilter.java

 1 package com.xidian.filter; 2  3 import java.io.IOException; 4 import javax.servlet.Filter; 5 import javax.servlet.FilterChain; 6 import javax.servlet.FilterConfig; 7 import javax.servlet.ServletException; 8 import javax.servlet.ServletRequest; 9 import javax.servlet.ServletResponse;10 11 public class SafeFilter implements Filter {  12   private String keywords[];13 14   public void doFilter(ServletRequest request, ServletResponse response,15       FilterChain chain) throws IOException, ServletException {16     //这里我们接收到用户名17     String name=request.getParameter("name");18     request.setAttribute("err", "您不允许登录<font color='red'>"+name+"</font>");19     System.out.println("第二个过滤器:"+name);20     if(name!=null){21       //判断22       for(String key:keywords){23         if(key.equals(name)){24           request.getRequestDispatcher("WEB-INF/ifo.jsp").forward(request, response);25           return;26         }27       }28       29     }30     chain.doFilter(request, response);31   }32 33 34   public void init(FilterConfig filterConfig) throws ServletException {35     keywords=filterConfig.getInitParameter("keywords").split(";");36 37   }38 39 }

View Code

web.

 1 <!-- 部署过滤器的顺序就是执行的顺序 --> 2  <filter> 3   <filter-name>MyFilter</filter-name> 4   <filter-class>com.xidian.filter.MyFilter</filter-class> 5   <init-param> 6   <param-name>encoding</param-name> 7   <param-value>utf-8</param-value> 8   </init-param>  9  </filter>10 11  <filter>12   <filter-name>SafeFilter</filter-name>13   <filter-class>com.xidian.filter.SafeFilter</filter-class> 14   <init-param>15   <param-name>keywords</param-name>16   <param-value>拉登;小明;凤姐</param-value>17   </init-param>18  </filter>

View Code