乐乐好好吧 关注:565贴子:4,484
  • 1回复贴,共1

struts2+spring+hibernate+json+jquery+flexigrid开发笔记-中文乱

只看楼主收藏回复

解决中文乱码的方法有很多种,但个人认为最有效最直接的方法就是过虑器,一次配置,多次使用,而且可以直接解决中文乱码问题,在struts2下怎样配置.
     过虑器代码:
package com.popedom.uitl;
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;
/**
*
* @author Administrator
*/
public class SetCharacterEncodingFilter implements Filter {
     protected String encoding = null;
     protected FilterConfig filterConfig = null;
     protected boolean ignore = true;
     public void init(FilterConfig filterConfig) throws ServletException {
     
         this.filterConfig = filterConfig;
         this.encoding = filterConfig.getInitParameter("encoding");
         String value = filterConfig.getInitParameter("ignore");
         if (value == null) {
             this.ignore = true;
         } else if (value.equalsIgnoreCase("true")) {
             this.ignore = true;
         } else {
             this.ignore = false;
         }
     }
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
       
      System.out.println("o1111111111111111111111111ooo1ooooooooooo");
      if (ignore || (request.getCharacterEncoding() == null)) {
             String encoding = selectEncoding(request);
             if (encoding != null) {
                 request.setCharacterEncoding(encoding);
             }
         }
         chain.doFilter(request, response);
     }
     public void destroy() {
         this.encoding = null;
         this.filterConfig = null;
     }
     protected String selectEncoding(ServletRequest request) {
         return (this.encoding);
     }
}
配置文件web.xml
<filter>
         <filter-name>SetCharacterEncodingFilter</filter-name>
         <filter-class>com.popedom.uitl.SetCharacterEncodingFilter</filter-class>
         <init-param>
             <param-name>encoding</param-name>
             <param-value>UTF-8</param-value>
         </init-param>
     </filter>
     <filter-mapping>
         <filter-name>SetCharacterEncodingFilter</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
<!-- --> <filter>
   <filter-name>struts2</filter-name>
   <filter-class>
    org.apache.struts2.dispatcher.FilterDispatcher
   </filter-class>
</filter>
<filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
这里一定要注意不能把顺序,先配置过虑器,在配置struts2,要不然会出现有时不出去乱码,有时出现乱码,具体原因尚未研究,等过一久忙玩这个项目,在好好总结一下解决乱码问题的所有方法.



IP属地:上海1楼2010-04-13 13:50回复
    拦截器 代码2:
    /*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
    package gov.util.ajax.filter;
    import java.io.IOException;
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class AjaxGBKFilter
         implements Filter
    {
         public AjaxGBKFilter()
         {
         }
         public void destroy()
         {
         }
         public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
             throws IOException, ServletException
         {
             HttpServletRequest httpRequest = (HttpServletRequest)request;
             HttpServletResponse httpResponse = (HttpServletResponse)response;
             String requestedWith = httpRequest.getHeader("x-requested-with");
             String type = request.getContentType();
             if(requestedWith != null && "XMLHttpRequest".equals(requestedWith) && null != type && "application/x-www-form-urlencoded".equals(type))
             {
                 httpRequest.setCharacterEncoding("UTF-8");
                 httpResponse.setCharacterEncoding("UTF-8");
                 httpRequest.getParameterMap();
             } else
             {
                 request.setCharacterEncoding("gbk");
                 response.setContentType("text/html;charset=gbk");
             }
             chain.doFilter(request, response);
         }
         public void init(FilterConfig filterconfig)
             throws ServletException
         {
         }
    }
    /*
         DECOMPILATION REPORT
         Decompiled from: E:\workspace\pdloan\web\WEB-INF\lib\govutil_1.0.jar
         Total time: 15 ms
         Jad reported messages/errors:
         Exit status: 0
         Caught exceptions:
    */


    IP属地:上海2楼2010-04-16 12:55
    回复