struts2中文乱码终极解决方案

Struts乱码终极解决方案

1、  JSP页面设置:

<%@ page language="java" contentType="text/html;

charset=utf-8"  pageEncoding="utf-8"%>

2、  struts.xml常量配置:

<constant name="struts.i18n.encoding" value="UTF-8"/>

3、  定义EncodingFilter:

public class EncodingFilter implementsFilter{

 

FilterConfig config = null;

    private String encoding="UTF-8";

public void destroy() {

           config = null;

           encoding = null;

}

 

public voiddoFilter(ServletRequest sRequest, ServletResponse sResponse,

                    FilterChain filterChain) throwsIOException, ServletException {

           sRequest.setCharacterEncoding("UTF-8"); 

           sResponse.setCharacterEncoding("UTF-8");

        filterChain.doFilter(sRequest,sResponse);  

}

 

public void init(FilterConfigconfig) throws ServletException {

            this.config = config;

     this.encoding= config.getInitParameter("encoding");

}

 

}

Web.xml中作如下配置(注意此配置(filter-mapping)要写在struts核心filter之前):

<filter> 

    <filter-name>encodingFilter</filter-name>             <filter-class>com.action.interceptor.EncodingFilter</filter-class>   

    <init-param> 

        <param-name>encoding</param-name> 

        <param-value>UTF-8</param-value>       

    </init-param>    

</filter> 

<filter-mapping> 

        <filter-name>encodingFilter</filter-name> 

        <url-pattern>/*</url-pattern> 

</filter-mapping>

4、  Tomcat

Server.xml文件下的配置

<Connectorport="8080" protocol="HTTP/1.1"

               maxThreads="150"connectionTimeout="20000"

              redirectPort="8443"URIEncoding="UTF-8"/>

5、  Mysql的my.ini文件:

default-character-set=utf8 

6、  资源文件

你可能感兴趣的:(struts2中文乱码终极解决方案)