pager-taglib分页控件查询参数乱码解决方法
来源:互联网 发布:两台mac之间共享屏幕 编辑:程序博客网 时间:2024/05/28 15:35
以前搞了很长时间一直没解决jsp pager-tablib查询带中文参数出现乱码的错误。今天花了相对较少的时间解决了很开心,原因和解决方法如下:
问题:
使用<pg:param name="key"/>标签传递中文参数时,会有乱码。
原因:
因为它默认是用gb2312来对添加的参数进行编码,如果你的过滤器、jsp页面都是采用的gb2312就没有什么问题,如果你采用的是utf-8来编码,那么中文参数传递过程中就会出现乱码导致无法解析。
网上有很多解决办法,重新修改编译源代码是一种方法;
在com/jsptags/navigation/pager下面的PagerTag的addParam(String name, Stringvalue)中,它原本是采用的URLEncoding.encode(value)方式来对传递的参数进行编码的,修改成为URLEncoding.encode(value, "UTF-8")后,替换掉原来jar包的这个class文件再重新打包并引入到项目中就可以了。
也以前也有人建议,加过滤器,修改服务器编码/jsp编码为一致,加URLEncoding都未解决,当时页面都是设置为utf-8 的,一直没搞定。
解决办法:
将JSP页面编码pageEncoding设置为GB2312,在web.xml中加中文过滤器,即可解决!
<!-- 解决中文问题 -->
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>
com.jxtit.util.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 解决中文问题 -->
过滤器:
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
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> 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
- pager-taglib分页控件查询参数乱码解决方法
- pager-taglib 分页乱码。解决方法。
- pager-taglib分页中文乱码
- pager-taglib分页查询中文乱码(用的tomcat)
- pager-taglib分页中文乱码原理解析
- Linux下pager-taglib分页中文搜索乱码
- pager-taglib分页控件的使用总结
- 分页pager-taglib使用指南
- pager-taglib分页实现
- pager-taglib分页
- java分页 pager-taglib
- pager-taglib实现分页
- Pager-taglib分页
- Pager-taglib分页标签
- Pager-taglib分页
- pager-taglib分页优化
- pager-taglib分页工具
- 分页pager-taglib使用指南
- Windows CE下访问物理内存的一些方法
- Android教程之如何安装(卸载)apk文件到模拟器
- 用Repeater做的一个可以收缩展开的列表项目
- eclipse 启动报堆内存溢出解决办法
- 2006年9月 bind的用法
- pager-taglib分页控件查询参数乱码解决方法
- linux下C语言编程1-gdb调试程序简介
- 2006年10月 金钱的思考!
- 和最牛叉的软件公司的职员的对话
- Android build system note
- 2007年1月 讨论:人为什么活着(人活着就是为了能够光荣的死去)
- 又看描述符
- Organizer截的Screenshots放在哪了?
- 亲身经历oracle 10g默认的归档路径(在闪回区)的2g大小限制