spring mvc 乱码问题

tomacat对GET和POST请求处理方式是不同的

POST: 加过滤器, 即可解决

GET: 加过滤器, 还需要改tomcat的server.xml配置文件(特别注意: eclipse下的 Servers下的server.xml也要改

 

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

 改为:

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"  useBodyEncodingForURI="true"/>

 

 

过滤器的几种加法: 

 

方法一: 自定义过滤器,  

	<!-- 自定义编码过滤器 -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>com.lw.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>characterEncoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <init-param>
            <param-name>enabled</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 实现方法:

package com.lw.filter;

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 CharacterEncodingFilter implements Filter {

    private String characterEncoding; //编码方式配置在web.xml文件中
    private boolean enabled;   //是否启用此Filter,配置在web.xml中

    @Override
    public void destroy() {
        characterEncoding = null;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        if (enabled || characterEncoding != null) {
            request.setCharacterEncoding(characterEncoding);
            response.setCharacterEncoding(characterEncoding);
        }
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
        characterEncoding = config.getInitParameter("characterEncoding");
        enabled = "true".equalsIgnoreCase(config.getInitParameter("enabled").trim());
    }
}

 

方法二: spring mvc 自带过滤器

	<!-- spring mvc 自带编码过滤器  -->
	<filter>  
	    <filter-name>CharacterEncodingFilter</filter-name>  
	    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
	    <init-param>  
	        <param-name>encoding</param-name>  
	        <param-value>utf-8</param-value>  
	    </init-param>  
	</filter>  
	<filter-mapping>  
	    <filter-name>CharacterEncodingFilter</filter-name>  
	    <url-pattern>/*</url-pattern>  
	</filter-mapping>

 

方法三: 集成到spring mvc 核心过滤器,   实现org.springframework.web.servlet.DispatcherServlet

    <!-- 集成过滤到核心拦截器中。  自定义过滤, EncodingDispatcherServlet实现核心拦截器DispatcherServlet, -->
	<servlet>  
	 <servlet-name>springMVC</servlet-name>  
	 <servlet-class>com.lw.filter.EncodingDispatcherServlet</servlet-class>  
	 <load-on-startup>1</load-on-startup>
		 <init-param>  
		  <param-name>encoding</param-name>  
		  <param-value>UTF-8</param-value>  
		 </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
	</servlet>

 实现方法:

package com.lw.filter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.DispatcherServlet;

public class EncodingDispatcherServlet extends DispatcherServlet {

	private String encoding;

	public void init(ServletConfig config) throws ServletException {
		encoding = config.getInitParameter("encoding");
		super.init(config);
	}

	protected void doService(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setCharacterEncoding(encoding);
		response.setCharacterEncoding(encoding);
		super.doService(request, response);
	}

}

 

附:

很多人说get请求乱码处理办法如下, 但本人测试好像不太奏效

<Connector port="8080" maxHttpHeaderSize="8192"    
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"    
               enableLookups="false" redirectPort="8443" acceptCount="100"    
               connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" /> 

 

 

你可能感兴趣的:(spring mvc,编码过滤器 filter)