displaytag分页中文处理

displaytag参考:
http://blog.sina.com.cn/s/blog_6b184a840100lavn.html
http://www.knowsky.com/1045905.html

直接修改class方法参考:
http://jingyan.baidu.com/article/4d58d541017fd39dd4e9c005.html

网上处理方案大部分都是改tomcat

个人还是喜欢改源码的方式:
更改DefaultRequestHelper
更改后:

/**
 * Licensed under the Artistic License; you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://displaytag.sourceforge.net/license.html
 *
 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */
package org.displaytag.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.UnhandledException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.displaytag.Messages;


/**
 * Default RequestHelper implementation.
 * @author Fabrizio Giustina
 * @version $Revision: 1104 $ ($Author: fgiust $)
 * @see org.displaytag.util.Href
 * @see org.displaytag.util.RequestHelper
 */
public class DefaultRequestHelper implements RequestHelper
{

    /**
     * logger.
     */
    private static Log log = LogFactory.getLog(DefaultRequestHelper.class);

    /**
     * original HttpServletRequest.
     */
    private HttpServletRequest request;

    /**
     * original HttpServletResponse.
     */
    private HttpServletResponse response;

    /**
     * Construct a new RequestHelper for the given request.
     * @param servletRequest HttpServletRequest needed to generate the base href
     * @param servletResponse HttpServletResponse needed to encode generated urls
     */
    public DefaultRequestHelper(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
    {
        this.request = servletRequest;
        this.response = servletResponse;
    }

    /**
     * @see org.displaytag.util.RequestHelper#getHref()
     */
    public Href getHref()
    {
        String requestURI = this.request.getRequestURI();
        // call encodeURL to preserve session id when cookies are disabled
        Href href = new DefaultHref(this.response.encodeURL(requestURI));
        href.setParameterMap(getParameterMap());
        return href;
    }

    /**
     * @see org.displaytag.util.RequestHelper#getParameter(java.lang.String)
     */
    public String getParameter(String key)
    {
        // actually simply return the parameter, this behaviour could be changed
        return this.request.getParameter(key);
    }

    /**
     * @see org.displaytag.util.RequestHelper#getIntParameter(java.lang.String)
     */
    public Integer getIntParameter(String key)
    {
        String value = this.request.getParameter(key);

        if (value != null)
        {
            try
            {
                return new Integer(value);
            }
            catch (NumberFormatException e)
            {
                // It's ok to ignore, simply return null
                log.debug(Messages.getString("RequestHelper.invalidparameter", //$NON-NLS-1$
                    new Object[]{key, value}));
            }
        }

        return null;
    }

    /**
     * @see org.displaytag.util.RequestHelper#getParameterMap()
     */
  /*  public Map getParameterMap()
    {

        Map map = new HashMap();

        // get the parameters names
        Enumeration parametersName = this.request.getParameterNames();

        while (parametersName.hasMoreElements())
        {
            // ... get the value
            String paramName = (String) parametersName.nextElement();

            request.getParameter(paramName);
            // put key/value in the map
            String[] originalValues = (String[]) ObjectUtils.defaultIfNull(
                this.request.getParameterValues(paramName),
                new String[0]);
            String[] values = new String[originalValues.length];

            for (int i = 0; i < values.length; i++)
            {
                try
                {
                    values[i] = URLEncoder.encode(StringUtils.defaultString(originalValues[i]), StringUtils
                        .defaultString(response.getCharacterEncoding(), "UTF8")); //$NON-NLS-1$
                }
                catch (UnsupportedEncodingException e)
                {
                    throw new UnhandledException(e);
                }
            }
            map.put(paramName, values);

        }

        // return the Map
        return map;
    }*/

    //解决中文支持问题
    public Map getParameterMap() {
        Map map = new HashMap(); 
        // get the parameters names 
        Enumeration parametersName = this.request.getParameterNames(); 
        while (parametersName.hasMoreElements()) { 
            // ... get the value 
            String paramName = (String) parametersName.nextElement(); 
            request.getParameter(paramName); 
            // put key/value in the map 
            String pName=""; 
            String[] originalValues = (String[]) ObjectUtils.defaultIfNull( this.request.getParameterValues(paramName), new String[0]); 
            try { 
                if(originalValues[0]!=null){ 
                    if(isContainChinese(originalValues[0])){ 
                        pName=originalValues[0]; 
                    }else{ 
                        pName=new String(originalValues[0].getBytes("ISO-8859-1"),"UTF-8"); 
                    } 
                } 
            } catch (UnsupportedEncodingException e1) { 
                // TODO Auto-generated catch block 
                e1.printStackTrace(); 
            } 
            try { 
                pName = URLEncoder.encode(StringUtils.defaultString(pName), StringUtils .defaultString(response.getCharacterEncoding(), "UTF8")); //$NON-NLS-1$ 
            }catch (UnsupportedEncodingException e) { 
                throw new UnhandledException(e); 
            } 
            map.put(paramName, pName); 
        } 
        //return the Map 
        return map; 
    }
    public boolean isContainChinese(String str) { 
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]"); 
        Matcher m = p.matcher(str); 
        if (m.find()) { 
            return true; 
        } 
        return false; 
    }

}

完美解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值