分页功能学习笔记(Struts2+jsp)
来源:互联网 发布:fbp算法源代码及注释 编辑:程序博客网 时间:2024/05/21 11:22
这里的分页主要用到了jstl标签,
框架是struts2
个人在做的过程中犯过的错误:
在Action中忘记写totalPage的get和set方法,导致jsp接收不到数据没显示.
框架是struts2
个人在做的过程中犯过的错误:
在Action中忘记写totalPage的get和set方法,导致jsp接收不到数据没显示.
个人认为在不理解的时候看图理解流程思路清晰后更好。
下面是图和部分主要代码(老师的):
代码写的顺序:DAO->Action->jsp
//DAO:public class CostDAOImpl implements ICostDAO{public List<Cost> findAll() throws DAOException {List<Cost> list = new ArrayList<Cost>();String sql = "select * from COST";Connection con = DBUtil.getConnection();try {PreparedStatement ps = con.prepareStatement(sql);ResultSet rs = ps.executeQuery();while(rs.next()){Cost c = createCost(rs);list.add(c);}} catch (SQLException e) {e.printStackTrace();throw new DAOException("查询全部资费数据失败!", e);}finally{DBUtil.close(con);}return list;}private Cost createCost(ResultSet rs) throws SQLException{Cost c = new Cost();c.setId(rs.getInt("id"));c.setName(rs.getString("name"));c.setBaseDuration(rs.getInt("base_duration"));c.setBaseCost(rs.getDouble("base_cost"));c.setUnitCost(rs.getDouble("unit_cost"));c.setStatus(rs.getString("status"));c.setDescr(rs.getString("descr"));c.setCreateTime(rs.getDate("creatime"));c.setStartTime(rs.getDate("startime"));c.setCostType(rs.getString("cost_type"));return c;}public List<Cost> findByPage(Integer page, Integer pageSize)throws DAOException {List<Cost> list = new ArrayList<Cost>();String sql = "select * from (" +" select c.*, rownum r from cost c" +") where r>? and r<?";Connection con = DBUtil.getConnection();try {PreparedStatement ps = con.prepareStatement(sql);//小于下一页的最小行int nextMin = page*pageSize + 1;//大于上一页的最大行int lastMax = (page-1)*pageSize;ps.setInt(1, lastMax);ps.setInt(2, nextMin);ResultSet rs = ps.executeQuery();while(rs.next()){Cost c = createCost(rs);list.add(c);}} catch (SQLException e) {e.printStackTrace();throw new DAOException("分页查询资费数据失败!", e);}return list;}public Integer findTotalpage(Integer pageSize) throws DAOException {//查询总行数String sql = "select count(*) from cost";Connection con = DBUtil.getConnection();try {PreparedStatement ps = con.prepareStatement(sql);ResultSet rs = ps.executeQuery();if(rs.next()){//根据总行数计算总页数int rows = rs.getInt(1);if(rows%pageSize == 0){return rows/pageSize;}else{return rows/pageSize + 1;}}} catch (SQLException e) {e.printStackTrace();throw new DAOException("查询总行数失败", e);}finally{DBUtil.close(con);}return 0;}}
//Action:public class FindCostAction {//页容量,默认为5private Integer pageSize = 5;//input//页码默认为1private Integer page = 1;//outputprivate List<Cost> costs;private Integer totalPage;/* * 业务方法查询每一页的数据 */public String execute(){ICostDAO dao = DAOFactory.getCostDAO();try {costs = dao.findByPage(page, pageSize);totalPage = dao.findTotalpage(pageSize);} catch (DAOException e) {e.printStackTrace();return "error";}return "success";}public List<Cost> getCosts() {return costs;}public void setCosts(List<Cost> costs) {this.costs = costs;}public Integer getPageSize() {return pageSize;}public void setPageSize(Integer pageSize) {this.pageSize = pageSize;}public Integer getPage() {return page;}public void setPage(Integer page) {this.page = page;}public Integer getTotalPage() {return totalPage;}public void setTotalPage(Integer totalPage) {this.totalPage = totalPage;}}
<c:choose> <c:when test="${page==1 }"> <a href="#">上一页</a> </c:when> <c:otherwise> <a href="findCost?page=${page-1 }">上一页</a> </c:otherwise> </c:choose> <c:forEach begin="1" end="${totalPage}" var="k"> <c:choose> <c:when test="${k==page }"> <!-- 当连接的地址和浏览器中URL的namespace一致时,namespace及其之前 的内容可以省略,这是相对路径的写法 --> <a href="findCost?page=${k }" class="current_page">${k }</a> </c:when> <c:otherwise> <a href="findCost?page=${k }">${k }</a> </c:otherwise> </c:choose> </c:forEach> <c:choose> <c:when test="totalPage"> <a href="#">下一页</a> </c:when> <c:otherwise> <a href="findCost?page=${page+1 }">下一页</a> </c:otherwise> </c:choose>
用OGNL表达式写的Jsp:
注意加标签库
<%@taglib uri="/struts-tags" prefix="s"%> <s:iterator value="costs"> <tr> <td><s:property value="id"/></td> <td><a href="fee_detail.html"><s:property value="name"/></a></td> <td><s:property value="baseDuration"/></td> <td><s:property value="baseCost"/></td> <td><s:property value="unitCost"/></td> <td><s:property value="createTime"/></td> <td><s:property value="startTime"/></td> <td><s:if test="status==0">开通</s:if><s:else>暂停</s:else></td> <td> <input type="button" value="启用" class="btn_start" onclick="startFee();" /> <input type="button" value="修改" class="btn_modify" onclick="location.href='fee_modi.html';" /> <input type="button" value="删除" class="btn_delete" onclick="deleteFee();" /> </td> </tr> </s:iterator> <s:if test="page==1"><a href="#">上一页</a></s:if> <s:else> <a href="findCost?page=<s:property value="page-1"/>">上一页</a> </s:else> <s:iterator begin="1" end="totalPage" var="k"> <s:if test="page==#k"><a href="findCost?page=<s:property value="#k"/>" class="current_page"> <s:property value="#k"/></a></s:if> <s:else><a href="findCost?page=<s:property value="#k"/>"><s:property value="#k"/></a></s:else> </s:iterator> <s:if test="page==totalPage"><a href="#">下一页</a></s:if> <s:else><a href="findCost?page=<s:property value="page+1"/>">下一页</a></s:else>
0 0
- 分页功能学习笔记(Struts2+jsp)
- jsp学习笔记2——分页功能实现
- jsp学习笔记2——分页功能实现
- struts2 jsp标签 学习笔记
- struts2、jsp分页代码
- [学习小笔记] JSP分页显示数据
- JSP实现分页功能
- JSP实现分页功能
- JSP实现分页功能
- JSP实现分页功能
- JSP实现分页功能
- Jsp实现分页功能
- jsp实现分页功能
- 学习笔记之vi之分页功能
- struts2学习笔记之一(初识jsp)
- JSP+Servlet+Struts2的基本原理学习笔记
- JSP+MySQL实现分页功能
- JSP实现网页,分页功能
- the meta:‘http-equiv=“X-UA…
- Respond.js-A fast &amp…
- editplus快捷键
- sublime 快捷键
- CSS字体中英文名称对照表 CSS…
- 分页功能学习笔记(Struts2+jsp)
- iframe安全威胁
- @import 对于网站的性能有某些负面…
- jade Language Reference
- CKEDITOR使用与配置
- 汇编 段内转移和段间转移
- 后缀数组的应用
- 查询前10条数据oracle
- Unbalanced calls to begin/end appearance transitions for <IDOOrderHomeViewController: 0x8da2960>.