当前位置:首页 > 开发 > 编程语言 > Spring > 正文

spring mvc 乱码问题

发表于: 2015-04-29   作者:davidforit   来源:转载   浏览次数:
摘要: tomacat对GET和POST请求处理方式是不同的 POST: 加过滤器, 即可解决 GET: 加过滤器, 还需要改tomcat的server.xml配置文件(特别注意: eclipse下的 Servers下的server.xml也要改)   <Connector connectionTimeout="20000" port="8080&qu

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 乱码问题

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Spring MVC3返回JSON数据中文乱码问题解决 查了下网上的一些资料,感觉比较复杂,这里,我这几使用
Spring MVC3返回JSON数据中文乱码问题解决 查了下网上的一些资料,感觉比较复杂,这里,我这几使用
今天发现spring mvc把一个view参数匹配异常(org.springframework.web.bind.MissingServletRequestP
错误信息如下: HTTP Status 500 - org.springframework.beans.NotReadablePropertyException: Inval
错误信息如下: HTTP Status 500 - org.springframework.beans.NotReadablePropertyException: Inval
错误信息如下: HTTP Status 500 - org.springframework.beans.NotReadablePropertyException: Inval
环境: jasperreports 5.0.0 ireport 5.1.0 1.IReport使用自带的Adobe中文字体会出现所有字会堆叠在
错误信息如下: HTTP Status 500 - org.springframework.beans.NotReadablePropertyException: Inval
错误信息如下: HTTP Status 500 - org.springframework.beans.NotReadablePropertyException: Inval
程序代码 代码如下 复制代码 <script type="text/ javascript"> $(document).ready(function
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号