struts2 jsp提交到action出现中文乱码问题

时间:2021-09-26 13:25:04

       a.首先在struts.xml中加固定值(和jsp页面的编码要一致)

<constantname="struts.i18n.encoding"value="UTF-8"></constant>

      b.若不行,再写一个filter过滤器,处理中文字符编码转换问题:

packagefilters;

importjava.io.IOException;

importjavax.servlet.Filter;

importjavax.servlet.FilterChain;

importjavax.servlet.FilterConfig;

importjavax.servlet.ServletException;

importjavax.servlet.ServletRequest;

importjavax.servlet.ServletResponse;

public classTomcatFilter implements Filter {    

    public void destroy()

    { }  

    public void doFilter(ServletRequest req,ServletResponse res, 

            FilterChain chain) throws IOException,ServletException {                

       req.setCharacterEncoding("UTF-8"); 

        chain.doFilter(req, res); 

    }   

    public void init(FilterConfig arg0) throwsServletException {   }

      public boolean accept(Object arg0) throwsIOException {

             // TODO Auto-generated method stub

             return false;

      }  

  注:实现Filter接口,要引入importjavax.servlet.Filter这个包,否则会报错。

struts2 jsp提交到action出现中文乱码问题

    然后,在web.xml中进行配置,记住要配在<filter-name>struts2</filter-name>之前。

  <filter>

  <display-name>TomcatFilter</display-name>

   <filter-name>TomcatFilter</filter-name>

   <filter-class>filters.TomcatFilter</filter-class>

  </filter>

  <filter-mapping>

        c.要保证数据库的编码与刚才的编码都一致