码迷,mamicode.com
首页 > 其他好文 > 详细

SSH项目中,解决中文请求参数乱码,Filter过滤器不起作用的问题

时间:2016-04-04 11:49:42      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

1.web.xml配置如下(必须放在Struts2配置之前)

<!-- 字符编码过滤器配置-->

<filter>
<filter-name>CharEncodingFilter</filter-name>
<filter-class>com.crm.tools.CharEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<!-- Struts2过滤器配置-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 其它配置-->

2.Filter类

package com.crm.tools;

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;
import javax.servlet.annotation.WebFilter;

/**
 * 字符过滤器
 */
@WebFilter("/CharEncodingFilter")
public class CharEncodingFilter implements Filter {
    //设置为UTF-8
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		chain.doFilter(request, response);
	}
    //初始化
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}
    //销毁
	public void destroy() {
	}

}

 3.表单请求(注意:必须制定method为post

<form action="此处是请求的地址" method="post">

</form>

Notes:

字符编码过滤器必须放在其它过滤器之前,还有请求的表单必须为post,反正我试过,不指定method属性进行提交的话,会出现乱码

SSH项目中,解决中文请求参数乱码,Filter过滤器不起作用的问题

标签:

原文地址:http://www.cnblogs.com/vincentExclusiveBlog/p/5351610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!