struts表单乱码的解决方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BabyGoodMorning/article/details/52331979

表单中文乱码的处理


struts中Action执行execute方法时,request.getCharacterEncoding()得到的值为空,在有中文时,会出现乱码现象。以下有三种方法,虽然都是参考网上的,不过还是手撸并测试了一下,这三种方式都还可以。

  1. 使用过滤器重新设置request的编码,个人比较喜欢这种方法
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;
public class CharacterEncodingFilter implements Filter {

    private String encoding;
    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // TODO Auto-generated method stub
        if(request.getCharacterEncoding() == null) {
            System.out.println("Request原编码为:" + request.getCharacterEncoding());
            if(this.encoding != null) {
                request.setCharacterEncoding(this.encoding);
                System.out.println("Filter设置Encoding为:" + encoding);
            }
        }
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
        this.encoding = filterConfig.getInitParameter("encoding");
    }

}

web.xml文件配置, 这里当表单保存或者修改时,才会被过滤器拦截,设置requset的编码为utf-8

<web-app>
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>com.struts.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/EmployeeAction_Save.do</url-pattern>
    <url-pattern>/EmployeeAction_Update.do</url-pattern>
  </filter-mapping>
 </web-app>

2.在ActionForm里的set方法时,对字符串重新使用utf-8编码,不过这种方法可能有点不方便

public void setName(String name){
    try {
            name = new String(name.getBytes("iso-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.name = name;
}

3.第三种方法是改写ActionServlet,重写process()方法

public class MyActionServlet extends ActionServlet {

    @Override
    protected void process(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        System.out.println("设置编码为utf-8");
        super.process(request, response);
    }

}

web.xml配置文件

  <servlet>
    <servlet-name>ActionServlet</servlet-name>
    <servlet-class>com.struts.web.MyActionServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ActionServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
阅读更多
换一批

没有更多推荐了,返回首页