pager-taglib分页控件查询参数乱码解决方法

以前搞了很长时间一直没解决jsp pager-tablib查询带中文参数出现乱码的错误。今天花了相对较少的时间解决了很开心,原因和解决方法如下:

 

问题:

使用标签传递中文参数时,会有乱码。

 

原因:

因为它默认是用gb2312来对添加的参数进行编码,如果你的过滤器、jsp页面都是采用的gb2312就没有什么问题,如果你采用的是utf-8来编码,那么中文参数传递过程中就会出现乱码导致无法解析。

 

网上有很多解决办法,重新修改编译源代码是一种方法;

在com/jsptags/navigation/pager下面的PagerTag的addParam(String name, String value)中,它原本是采用的URLEncoding.encode(value)方式来对传递的参数进行编码的,修改成为 URLEncoding.encode(value, "UTF-8")后,替换掉原来jar包的这个class文件再重新打包并引入到项目中就可以了。

也以前也有人建议,加过滤器,修改服务器编码/jsp编码为一致,加URLEncoding都未解决,当时页面都是设置为utf-8 的,一直没搞定。

 

解决办法:

将JSP页面编码pageEncoding设置为GB2312,在web.xml中加中文过滤器,即可解决!


   
        SetCharacterEncodingFilter
       
            com.jxtit.util.SetCharacterEncodingFilter
         

       
            encoding
            GB2312
       

   

   
        SetCharacterEncodingFilter
        /*
   

 

过滤器:

SetCharacterEncodingFilter:

public class SetCharacterEncodingFilter implements Filter {

    // ----------------------------------------------------- Instance Variables

    /**
     * The default character encoding to set for requests that pass through this
     * filter.
     */
    protected String encoding = null;

    /**
     * The filter configuration object we are associated with. If this value is
     * null, this filter instance is not currently configured.
     */
    protected FilterConfig filterConfig = null;

    /**
     * Should a character encoding specified by the client be ignored?
     */
    protected boolean ignore = true;

    // --------------------------------------------------------- Public Methods

    /**
     * Take this filter out of service.
     */
    public void destroy() {

        this.encoding = null;
        this.filterConfig = null;

    }

    /**
     * Select and set (if specified) the character encoding to be used to
     * interpret request parameters for this request.
     *
     * @param request
     *            The servlet request we are processing
     * @param result
     *            The servlet response we are creating
     * @param chain
     *            The filter chain we are processing
     *
     * @exception IOException
     *                if an input/output error occurs
     * @exception ServletException
     *                if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        // Conditionally select and set the character encoding to be used
        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);
            if (encoding != null) {
                request.setCharacterEncoding(encoding);
            }
        }
        // Pass control on to the next filter
        chain.doFilter(request, response);
        //BufferedWriter bw = new BufferedWriter(new FileWriter("/temp/insertlog.log"));
    }

    /**
     * Place this filter into service.将这个filter放在服务器中
     *
     * @param filterConfig
     *            The filter configuration object
     */
    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 Methods
    /**
     * Select an appropriate character encoding to be used, based on the
     * characteristics of the current request and/or filter initialization
     * parameters. If no character encoding should be set, return
     * null.
     *


     * The default implementation unconditionally returns the value configured
     * by the encoding initialization parameter for this
     * filter.
     *
     * @param request
     *            The servlet request we are processing
     */
    protected String selectEncoding(ServletRequest request) {
        return (this.encoding);
    }

}

 

jsp pager-taglib2.0参考

http://jsptags.com/tags/navigation/pager/index.jsp

 

你可能感兴趣的:(Java开发,encoding,character,filter,initialization,servlet,processing)