SSH设置字符集过滤器解决中文乱码问题

文章原文:https://www.cnblogs.com/vincentExclusiveBlog/p/5351610.html

web.xml配置必须放在struts2配置之前):




CharEncodingFilter
com.crm.tools.CharEncodingFilter

encoding 
UTF-8 



CharEncodingFilter
/*





struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter


struts2
/*


自定义CharEncodingFilter类实现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() {

    }

}

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

 

你可能感兴趣的:(学习笔记,SSH)