Struts2拦截器解决乱码问题

 之前使用struts1的时候是通过写filter来处理乱码,把写的filter搬到struts2,配置了WEB.XML发生没有效果,请求根本就没有通过filter。原因Struts2在web.html配置了处理action请求的filter:

[html]  view plain copy
  1. <filter>  
  2.   <filter-name>struts2</filter-name>  
  3.   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  4.  </filter>  
  5.   
  6.  <filter-mapping>  
  7.   <filter-name>struts2</filter-name>  
  8.   <url-pattern>/*</url-pattern>  
  9.  </filter-mapping>  

通过这个sturts filter后,在这个struts filter之前或之后配置都是发现处理乱码的filter不起作用,所以编写拦截器还是个不错的解决乱码的方式。

1、编写自定义 EncodingIntereptor拦截器

[java]  view plain copy
  1. import java.io.UnsupportedEncodingException;  
  2. import java.util.Iterator;  
  3.   
  4. import javax.servlet.http.HttpServletRequest;  
  5.   
  6. import org.apache.struts2.StrutsStatics;  
  7.   
  8. import com.opensymphony.xwork2.ActionContext;  
  9. import com.opensymphony.xwork2.ActionInvocation;  
  10. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  11.   
  12. public class EncodingInterceptor extends AbstractInterceptor {  
  13.   
  14.  /** 
  15.   * Struts2编码拦截器 
  16.   */  
  17.    
  18.  @Override  
  19.  public String intercept(ActionInvocation arg0) throws Exception {  
  20.   // TODO Auto-generated method stub  
  21.     
  22.    ActionContext actionContext = arg0.getInvocationContext();     
  23.    HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);   
  24.   System.out.println("Encoding Intercept...");  
  25.   /** 
  26.    * 此方法体对GET 和 POST方法均可 
  27.    */  
  28.   if( request.getMethod().compareToIgnoreCase("post")>=0){  
  29.       try {  
  30.        request.setCharacterEncoding("GBK");  
  31.       } catch (UnsupportedEncodingException e) {  
  32.        // TODO Auto-generated catch block  
  33.        e.printStackTrace();  
  34.       }  
  35.      }else{  
  36.               
  37.       Iterator iter=request.getParameterMap().values().iterator();  
  38.       while(iter.hasNext())  
  39.       {  
  40.        String[] parames=(String[])iter.next();  
  41.        for (int i = 0; i < parames.length; i++) {  
  42.         try {  
  43.          parames[i]=new String(parames[i].getBytes("iso8859-1"),"GBK");//此处GBK与页面编码一样  
  44.         } catch (UnsupportedEncodingException e) {  
  45.          e.printStackTrace();  
  46.         }  
  47.        }     
  48.       }     
  49.        }  
  50.          return arg0.invoke();  
  51.  }  
  52.   
  53. }  


2、Struts.xml配置

下注册拦截器:

[html]  view plain copy
  1. <package>  
  2.      <interceptors>  
  3.         <interceptor name="Encoding" class="com.disaster.util.EncodingInterceptor"></interceptor>  
  4.         <interceptor-stack name="Encode">  
  5.            <interceptor-ref name="Encoding"></interceptor-ref>  
  6.            <interceptor-ref name="defaultStack"></interceptor-ref><!-- 必须引入这个,否则request不会再往下传-->  
  7.         </interceptor-stack>  
  8.      </interceptors>  

3、使用拦截器,可将其设为默认的拦截器

[html]  view plain copy
  1. <default-interceptor-ref name="Encode"></default-interceptor-ref>   

4、页面编码和页面字符编码跟设为"UTF-8"。如果页面是其它编码,将拦截器中重编码部分改一下即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值