日期:2014-05-20  浏览次数:20853 次

struts2.0中个get请求乱码,使用过滤器仍然乱码,各位高手帮帮了
监听类EncodeFilter.java
Java code

package com.qian.tools;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.struts2.dispatcher.FilterDispatcher;
import org.omg.CORBA.Request;

import com.qian.mian.MainListener;

public class EncodeFilter implements Filter{    
    private String edcoding=null;    
    private boolean ignore=true;    
   
    public void destroy() {    
         edcoding=null;    
    }    
   
    public void doFilter(ServletRequest request, ServletResponse response,    
            FilterChain filterChain) throws IOException, ServletException {    
        if(ignore==true||request.getCharacterEncoding()==null){    
            String encoding=setCharacterEncoding(request);   
            System.out.println("请求"+request.getParameter("sms.username"));
            
              if(encoding!=null){    
                //request.setCharacterEncoding(encoding);    
                response.setCharacterEncoding(encoding);
                System.out.println("en:"+encoding);
                }    
        }    
       System.out.println( request.getAttribute("username"));
          filterChain.doFilter(request, response);    
    }    
   
    public void init(FilterConfig filterConfig) throws ServletException {    
        this.edcoding=filterConfig.getInitParameter("encoding");    
        String value=filterConfig.getInitParameter("ignore");   
        System.out.println(value);
       
        if(value==null){    
            this.ignore=true;    
        }else if(value.equalsIgnoreCase("true")){    
            this.ignore=true;    
                
        }else{    
            this.ignore=false;    
        }    
            
   
    }    
    public String setCharacterEncoding(ServletRequest request){    
        return this.edcoding;    
    }




}    



web.xml配置文件
XML code

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <filter>
  <filter-name>encode</filter-name>
  <filter-class>com.qian.tools.EncodeFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
  <filter-name>encode</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>



struts.xml配置文件
XML code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <package name="struts2" namespace="/" extends="struts-default">
        <action name="ltdx" class="com.qian.actions.LtDxTSAction">
            <result name="success">/result.jsp</result>
        </action>
    </package>
</struts>