[size=large][size=x-large][b]以前搞了很长时间一直没解决jsp pager-tablib查询带中文参数出现乱码的错误。今天花了相对较少的时间解决了很开心,原因和解决方法如下:
问题:[size=large]
使用<pg:param name="key" />标签传递中文参数时,会有乱码。
原因:
因为它默认是用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中加中文过滤器,即可解决!
<!-- 解决中文问题 -->
<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);
}
}
[b][/b][color=darkred][/color][/size][/size]
[/size][/b]
分享到:
相关推荐
使用pager-taglib分页,含有多种分页样式,jar包都有,只要导入就可以运行,内附有说明,java 语言的
pager-taglib 是个很好的jsp分页标签,使用它结合jstl可以实现灵活的分页导航功能。在实际的开发中post方式的提交比较常见,本人做了一个比较通用的基于post方式的一个应用。主要实现一下功能: 1.添加输入跳转、每...
pager-taglib分页组建 pager-taglib分页组建 pager-taglib分页组建 pager-taglib分页组建
jsp 分页标签。 用于列表自动分页功能
用pager-taglib进行分页时,如果出现中文参数,由于pager-taglib默认是用ISO8859-1编码后进行参数传递,故会出现乱码现象。本人下载源码后,将编码的语句注释掉,即不进行编码,解决了中文乱码问题。改写后的代码已...
用于在jsp中进行分页的 pager-taglib包
这是pager-taglib的官方源码,但在采用官方的pager-taglib分页传输中文参数会出现乱码,所以我采用maven新建一个pager-taglib项目对pager-taglib的源码进行小小的改动,是他支持中文传输,更重要的是把源代码提供给...
这是关于paper-taglib分页框架的小小项目,导入myeclipse中部署到服务器可直接运行,包含了两种分页风格:普通样式和谷歌样式,可以在servlet中通过注释切换。
已解决中文乱码问题的 pager-taglib-2.0.jar包。
该资源系本人工作之余的一个小小案例,希望对大家有所帮助
pager-taglib-2.0及使用方法,一个很好用的页面分页工具.
displaytag,pager-taglib 分页包 和源文件
pager-taglib-2.0,pager-taglib-2.0,pager-taglib-2.0,java分页标签。
jsp pager-taglib分页资料jsp pager taglib 分页 ppt,很好的分页模版
pager-taglib分页要点,第三方分页工具 的要点!
利用pager-taglib进行查询分页时需要添加该jar包:pager-taglib.jar
很详细的Pager-taglib标签库分页的使用。
pager-taglib.jar包下载.很方便的分页框架。这是jar包,希望能帮到大家