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属性进行提交的话,会出现乱码