一次机试之后的心得体会,附分页查询实现

来源:互联网 发布:手机淘宝怎样删除订单 编辑:程序博客网 时间:2024/06/07 00:07

      在周末已经知道上课后要机试的情况下还是什么都没有写,什么准备工作都没有做,而且我的基础又是那么的差。

        原因:不知道怎么下手,寄托于,可以照着之前的项目写,照着格式方法去搬用。自以为可以行得通。

      结果发现,没有自己亲自打过代码,只知道大概是毫无用处的,就像高中的数学一样,

       不亲自去写一遍去算一遍,就不会形成深刻的记忆。

       不会有所谓的经验。

       而如果没有经验在遇到相同的情况,相同的问题时你就会无从下手。什么问题都解决不了。

         分页查询,只模模糊糊记着要查询出来数据总条数,count 。利用数据总条数和自己规定的单页显示数据条数可以计算出最大页码号。

那么某一页的数据是如何显示呢?

刚做的这个项目中有start和pagesize,currentPage 等几个属性。如何利用这几个属性来实现数据的显示呢?

public void list(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int currentPage = 1;  //currentPage应该就是当前页码,初始值设为1。也就是当我们查询时从第一页开始。
if (request.getParameter("currentPage") != null) {
currentPage = Integer.parseInt(request.getParameter("currentPage"));//如果当前页码号不为空,则将其转化为十进制的数据currentPage
}
int maxPage = ps.getMaxPage(pageSize);//maxPage是最大页码号。调用service中的maxPage获得。
List<Product> list = ps.findProduct(currentPage, pageSize);//这里是商品集合,调用service中的findProduct并用括号中的前台属性去查询数据;


request.setAttribute("list", list);
request.setAttribute("currentPage", currentPage);
request.setAttribute("maxPage", maxPage);            
  //推测此处的数据是为了前台方便获取。连接前台并定义;


request.getRequestDispatcher("/admin/product/list.jsp").forward(
request, response);     //
}

分页方法已经被封装到了一个工具类中。



前台绑定:

                                            <c:forEach items="${list}" var="product">  //循环执行查询商品,并显示数据的代码;

                                                ${product.pimage}      //el表达式用来连接获取后台查询到的数据(不知道具体和哪里连接。)

                                                   <tr align="center">
<td colspan="7">
<c:choose>          /*此标签的作用:          
                                                                                          <c:choose>就像在Java switch语句,它可以让你在一些替代方案之间选择。                             
                                                                                           switch语句中有case语句,<c:choose>标签具有<c:when>标签。

                                                                  switch语句中有默认default子句来指定一个默认的行为,类似的方式<c:choose>已<c:otherwise>作为default语句。

                                                                             */

                               //这里整体应该是在写点击上一页实现什么功能
<c:when test="${currentPage eq 1}">  //当 test中的currentPage=1时
上一页
</c:when>
<c:otherwise>
<a href="${pageContext.request.contextPath}/product?currentPage=${currentPage - 1}&&method=list">上一页</a>
</c:otherwise>  //否则就跳转/product?currentPage=${currentPage - 1}&&method=list为这一页。
</c:choose>

          //下面是显示页码的代码。从第一页开始maxPage结束
<c:forEach begin="1" end="${maxPage}" var="index">
<c:if test="${index eq currentPage}">     //如果当前页面等于currentPage
<a href="javascript:void(0)" style="color:red">${index}</a>
</c:if>
<c:if test="${index ne currentPage}">
<a href="${pageContext.request.contextPath}/product?currentPage=${index}&&method=list">${index}</a>                     //product?currentPage 就是将product中的currentPage参数显示出来即显示页码号
</c:if>
</c:forEach>
<c:choose>
<c:when test="${currentPage eq maxPage}">
下一页
</c:when>
<c:otherwise>
<a href="${pageContext.request.contextPath}/product?currentPage=${currentPage + 1}&&method=list">下一页</a>
</c:otherwise>
</c:choose>
</td>

</tr>


<c:choose>就像在Java switch语句,它可以让你在一些替代方案之间选择。switch语句中有case语句,<c:choose>标签具有<c:when>标签。switch语句中有默认default子句来指定一个默认的行为,类似的方式<c:choose>已<c:otherwise>作为default语句。