struts1.x中文乱码解决
使用过滤器Filter过滤struts1的中文乱码
第一步:先设定jsp页面或html页面的编码
jsp:<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>html:<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>hr.util.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>enable</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <servlet-name>action</servlet-name> </filter-mapping>
package hr.util; import javax.servlet.Filter; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.FilterChain; import java.io.IOException; public class CharacterEncodingFilter implements Filter { private FilterConfig filterConfig; private String encoding; private boolean enable; public CharacterEncodingFilter() { encoding = "UTF-8"; enable = false; } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; loadConfigParams(); } private void loadConfigParams() { encoding = filterConfig.getInitParameter("encoding"); String strIgnoreFlag = filterConfig.getInitParameter("enable"); if (strIgnoreFlag.equalsIgnoreCase("true")) { this.enable = true; } else { this.enable = false; } } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (enable) { request.setCharacterEncoding(encoding); } chain.doFilter(request, response); } public void destroy() { filterConfig = null; encoding = null; } }