java + struts2 分页技术
2015-07-14 15:01
399 查看
在面试的时候遇到一大神把我彻底问傻,回来后恶补。自己写个了分页,现在贴出来供大家分享、指正;废话不多说,代码走起。
test.jsp页面,程序从该页面进入,点击提交找action
action类
Dao层,数据库链接及处理语句
SplitPage类
jsp页面
每页条第
页
共页
test.jsp页面,程序从该页面进入,点击提交找action
" /> My JSP 'test.jsp' starting page
action类
public class SplitAction extends ActionSupport implements ServletRequestAware{ private SplitPage splitPage = new SplitPage(); public SplitPage getSplitPage() { return splitPage; } public void setSplitPage(SplitPage splitPage) { this.splitPage = splitPage; } HttpServletRequest request; public String execute()throws Exception{ HttpSession session = request.getSession(); ArrayList arrayList = new ArrayList(); SplitDao sd = new SplitDao(); ArrayList aList = new ArrayList(); arrayList = sd.execute(); aList = sd.getAll(); int maxpage = aList.size(); int page = maxpage/4 + ((maxpage % 4 == 0)?0 : 1); splitPage.getNowpage(); splitPage.getRadpage(); session.setAttribute("list",arrayList); session.setAttribute("page", page); session.setAttribute("maxpage", maxpage); session.setAttribute("currpage", 1); session.setAttribute("splitPage", splitPage); return "success"; } public String select (){ HttpSession session = request.getSession(); SplitDao sd = new SplitDao(); ArrayList aList = new ArrayList(); ArrayList arrayList = new ArrayList(); int total = splitPage.getNowpage(); int currpage = splitPage.getRadpage(); aList = sd.getAll(); int maxpage = aList.size(); int page = maxpage/total + ((maxpage % total == 0)?0 : 1);//总页面数 if(currpage > page){ currpage = page; splitPage.setRadpage(page); }else if(currpage < 1){ currpage = 1; splitPage.setRadpage(1); } arrayList = sd.query(currpage,total); session.setAttribute("list", arrayList); session.setAttribute("maxpage", maxpage); session.setAttribute("page", page); session.setAttribute("currpage", currpage); return "success"; } public ArrayList getAll(){ HttpSession session = request.getSession(); ArrayList arrayList = new ArrayList(); SplitDao sd = new SplitDao(); arrayList = sd.getAll(); return arrayList; } @Override public void setServletRequest(HttpServletRequest request) { this.request = request; } }
Dao层,数据库链接及处理语句
public class SplitDao { DbUtil dbUtil = new DbUtil(); GuaHao gh = null; public ArrayList execute(){ String sql = "select * from t_ghinfo limit 0,4" ; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList arrayList = new ArrayList(); try { conn = dbUtil.getConn(); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { gh = new GuaHao(); gh.setGhDesc(rs.getString("ghDesc")); gh.setGhNo(rs.getString("ghNo")); gh.setOfficeName(rs.getString("officeName")); arrayList.add(gh); } } catch (Exception e) { e.printStackTrace(); }finally{ dbUtil.Close(conn, ps, rs); } return arrayList; } public ArrayList query(int page,int total){ page = (page -1) * total; String sql = "select * from t_ghinfo limit " ; sql += page +"," + total; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList arrayList = new ArrayList(); try { conn = dbUtil.getConn(); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { gh = new GuaHao(); gh.setGhDesc(rs.getString("ghDesc")); gh.setGhNo(rs.getString("ghNo")); gh.setOfficeName(rs.getString("officeName")); arrayList.add(gh); } } catch (Exception e) { e.printStackTrace(); }finally{ dbUtil.Close(conn, ps, rs); } return arrayList; } public ArrayList getAll(){ String sql = "select * from t_ghinfo" ; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList arrayList = new ArrayList(); try { conn = dbUtil.getConn(); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { gh = new GuaHao(); gh.setGhDesc(rs.getString("ghDesc")); gh.setGhNo(rs.getString("ghNo")); gh.setOfficeName(rs.getString("officeName")); arrayList.add(gh); } } catch (Exception e) { e.printStackTrace(); }finally{ dbUtil.Close(conn, ps, rs); } return arrayList; } }
SplitPage类
public class SplitPage { private int nowpage = 4;//每页显示的条数 private int currentpage = 1;//当前页,默认是第一页 private int first = 1;//首页 private int totalpage = 0;//总页数 private int radpage = 1;//用户选择的页数,默认是第一页 public int getNowpage() { return nowpage; } public void setNowpage(int nowpage) { this.nowpage = nowpage; } public int getCurrentpage() { return currentpage; } public void setCurrentpage(int currentpage) { this.currentpage = currentpage; } public int getFirst() { return first; } public void setFirst(int first) { this.first = first; } public int getTotalpage() { return totalpage; } public void setTotalpage(int totalpage) { this.totalpage = totalpage; } public int getRadpage() { return radpage; } public void setRadpage(int radpage) { this.radpage = radpage; } public int getAllPage(int alldata,int nowpage){ int page = (alldata / nowpage + (alldata % nowpage == 0?0 : 1) ); return page; } }
jsp页面
<% SplitPage splitPage = new SplitPage(); Object pageIndex = session.getAttribute("currpage"); Object maxpage = session.getAttribute("page"); int currpage = Integer.parseInt(pageIndex.toString()); int pagemax = Integer.parseInt(maxpage.toString()); %>
desc | no | officename |
---|---|---|
<%=desc %> | <%=no %> | <%=officename %> |
首页 | 上一页 | 下一页 | 尾页 |
相关文章推荐
- java 路径相关问题
- java调用中国天气网api获得天气预报信息的方法
- spring websocket服务器遇到问题记录1
- Java注解 Annotation
- 附加上一篇根据ip取得所在地
- java之字符串组合
- SpringMVC + ehcache( ehcache-spring-annotations)基于注解的服务器端数据缓存
- windows环境下JAVA+GDAL配置
- servlet 单实例多线程
- 诊断 Java 代码: 轻松掌握 Java 泛型
- Eclipse SVN插件的帐号、密码修改
- 运行DbVisualizer报the java_home environment viariable does not point to a working 32-bit JDK OR JRE错误
- java 文件输出流覆盖问题
- Java基础-异常-throws Exception-抛声明
- Java SE 8: Lambda表达式
- nc eclipse stadio编译错误怎么办
- BigDecimal.setScale 处理java小数点
- spring接口用户超时验证
- springmvc入门基础之注解和参数传递
- Java实现三种常用的查找算法(顺序查查找,折半查找,二叉排序树查找)