欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-03 19:40:50
JSP与struts2中的乱码问题,解决方法很多,今天我又尝试了一个新方法感觉不错,超简单   首先写一个filter [java]  publ...

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>