struts2有没有提供分页的标签???
struts2有没有动态分页的标签提供啊?
如果要自己编的话,有没有用struts2实现的动态分页的例子?谢谢
------解决方案--------------------
加入pager-taglib.jar
页面上下文:
package com.shwl.cmshop;
public class SystemContext {
private static ThreadLocal<Integer> offset = new ThreadLocal<Integer>();
private static ThreadLocal<Integer> pagesize = new ThreadLocal<Integer>();
public static int getOffset(){
Integer os = (Integer)offset.get();
if(os == null){
return 0;
}
return os;
}
public static void setOffset(int offsetvalue){
offset.set(offsetvalue);
}
public static void removeOffset(){
offset.remove();
}
public static int getPagesize(){
Integer ps = (Integer)pagesize.get();
if(ps == null){
return Integer.MAX_VALUE;
}
return ps;
}
public static void setPagesize(int pagesizevalue){
pagesize.set(pagesizevalue);
}
public static void removePagesize(){
pagesize.remove();
}
}
写一个页面过滤器:
package com.shwl.cmshop.web;
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 javax.servlet.http.HttpServletRequest;
import com.shwl.cmshop.SystemContext;
public class PagerFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws
IOException,
ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request;
SystemContext.setOffset(getOffset(httpRequest));
SystemContext.setPagesize(getPagesize(httpRequest));
try{
chain.doFilter(request, response);
}finally{
//清空ThreadLocal中的值
SystemContext.removeOffset();
SystemContext.removePagesize();
}
}
protected int getOffset(HttpServletRequest request){
int offset = 0;
try {
offset = Integer.parseInt(request.getParameter("pager.offset"));
} catch (
NumberFormatException ignore) {
}
return offset;
}
protected int getPagesize(HttpServletRequest request){
return 10;
}
public void init(FilterConfig arg0) throws ServletException {
}
}
下面是hibernate对分页的实现
package com.shwl.cmshop;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
public class AbstractDAO {
public PagerModel searchPaginated(String hql) throws Exception {
return searchPaginated(hql, null, 0, Integer.MAX_VALUE);
}
public PagerModel searchPaginated(String hql, Object param)
throws Exception {
return searchPaginated(hql, new Object[] { param }, 0,
Integer.MAX_VALUE);
}
public PagerModel searchPaginated(String hql, Object[] params)
throws Exception {
return searchPaginated(hql, params, 0, Integer.MAX_VALUE);
}
public PagerModel searchPaginated(String hql, int offset, int pagesize)
throws Exception {
return searchPaginated(hql, null, offset, pagesize);
}
public PagerModel searchPaginated(String hql, Object obj, int offset,
int pagesize) throws Exception {
return searchPaginated(hql, new Object[] { obj }, offset, pagesize);
}
/**
* 根据HQL语句进行分页查询
*
* @param hql
* HQL语句
* @param params
* HQL语句带的多个参数值
* @param offset
* 从第几条记录开始查询
* @param pagesize