有一个项目,用struts提交中文数据时候总是显示乱码,有什么方法可以解决呢?

解决方案 »

  1.   

    页面:
        encodeURI
    action里这样处理:
        java.net.URLDecoder.decode(bankMsg, "UTF-8");
      

  2.   

    做个过滤器,在web.xml中配置一下
      

  3.   


    URL中处理不了的中文用String 改下编码
      

  4.   

    从3个方面:
    1,数据库
    有些乱码问题是数据库的编码格式和画面显示格式不一样产生的。
    你可以确认一下数据库是不是utf-8,如果不是可以用utf-8建立数据库
    connection连接的时候也可以指定格式。2,xml文件,jsp,html文件
    这个在文件的开头都可以指定编码格式3,java程序
    response.setContentType("text/html;charset=GB2312");
      

  5.   

    web.xml配置如下: 
        <filter>
            <filter-name>characterEncoding</filter-name>
            <filter-class>com.test.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>     //自己根据情况改成你需要的编码如GBK     
            </init-param>        
        </filter>    <filter-mapping>
            <filter-name>characterEncoding</filter-name>
            <url-pattern>/*</url-pattern>     
        </filter-mapping>
    以下为java代码package com.test.filter;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;import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    public class CharacterEncodingFilter implements Filter {
    private static Log log = LogFactory.getLog(CharacterEncodingFilter.class); protected String encoding = null;
    protected FilterConfig filterConfig = null;
    protected boolean ignore = true; public void destroy() { this.encoding = null;
    this.filterConfig = null; } public void doFilter(
    ServletRequest request,
    ServletResponse response,
    FilterChain chain)
    throws IOException, ServletException { if (ignore || (request.getCharacterEncoding() == null)) {
    String encoding = selectEncoding(request);
    if (encoding != null)
    request.setCharacterEncoding(encoding);
    }
    chain.doFilter(request, response);
    } 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 if (value.equalsIgnoreCase("yes"))
    this.ignore = true;
    else
    this.ignore = false; } protected String selectEncoding(ServletRequest request) {
    return (this.encoding);
    }
    }
      

  6.   

    使用时注意编码保持统一,从jsp->java->数据库,三者编码注意一致。
      

  7.   

    很简单啊,做个过滤器,在web.xml中配置一下