首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts1.x汉语言乱码解决

2013-11-09 
struts1.x中文乱码解决使用过滤器Filter过滤struts1的中文乱码第一步:先设定jsp页面或html页面的编码jsp:

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">

第二步:我们来看看web文件的配置
  <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>     


第三步:生成Filter文件
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;      }    }    

通过以上的方法,在struts中就再也不用担心乱码问题了,也不用再在每个页面中写  request.setCharacterEncoding("UTF-8")了


热点排行