解决JSP与struts2 Action中文乱码问题

来源:岁月联盟 编辑:exp 时间:2012-10-07

JSP与struts2中的乱码问题,解决方法很多,今天我又尝试了一个新方法感觉不错,超简单

 


首先写一个filter


[java] 
public class SetCodeFilter implements Filter { 
 
    @Override 
    public void destroy() { } 
 
    @Override 
    public void doFilter(ServletRequest req, ServletResponse res, 
            FilterChain chain) throws IOException, ServletException { 
             
        req.setCharacterEncoding("UTF-8"); 
        chain.doFilter(req, res); 
    } 
 
    @Override 
    public void init(FilterConfig arg0) throws ServletException {   } 
 

public class SetCodeFilter implements Filter {

 @Override
 public void destroy() { }

 @Override
 public void doFilter(ServletRequest req, ServletResponse res,
   FilterChain chain) throws IOException, ServletException {
   
  req.setCharacterEncoding("UTF-8");
  chain.doFilter(req, res);
 }

 @Override
 public void init(FilterConfig arg0) throws ServletException { }

}
在web.xml配置filter


[html] 
<filter> 
      <filter-name>SetCodeFilter</filter-name> 
      <filter-class>com.shop.web.filter.SetCodeFilter</filter-class> 
  </filter> 
  <filter-mapping> 
      <filter-name>SetCodeFilter</filter-name> 
      <url-pattern>*.do</url-pattern> 
      <url-pattern>*.jsp</url-pattern> 
  </filter-mapping> 

<filter>
      <filter-name>SetCodeFilter</filter-name>
      <filter-class>com.shop.web.filter.SetCodeFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>SetCodeFilter</filter-name>
      <url-pattern>*.do</url-pattern>
      <url-pattern>*.jsp</url-pattern>
  </filter-mapping>