プロが教える店舗&オフィスのセキュリティ対策術

1ページに10件ずつ表示したいのですが、1回目「次のページ」ボタンを押すと
11件目から表示しますが、2回目「次のページ」ボタンを押しても、
21件目から表示せず、再び11件目から表示します。
どこがおかしいか、どなたかご指摘お願い致します。

「最初の10行を表示するServlet」

public class hoge extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
request.getSession().invalidate();
CatalogDAO dao = new CatalogDAO();
List<Product> list = dao.getProductList();
request.getSession(true).setAttribute("products",list);
table = new ArrayList<Object>();
OffsetBean offsetBean = new OffsetBean();
offsetBean.setStr(0);
table.add(offsetBean);
request.getSession(true).setAttribute("object1", table);
}catch(Exception e){
throw new ServletException(e);
}
getServletContext().getRequestDispatcher("catalog.jsp").forward(request,response);
}
private List<Object> table;
public List<Object> getTable() {
return table;
}
}

「catalog.jsp」

<%@ page language="java" contentType="text/html; charset=Windows-31J" pageEncoding="Windows-31J" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach var="var2" items="${object1}">
<table>
<tr bgColor=#bb5a5a>
<th><font color=#ffffff>番号</font></th>
<th><font color=#ffffff>商品名</font></th>
<th><font color=#ffffff>価格</font></th>
<th><font color=#ffffff></font></th>
</tr>
<c:forEach var="var" items="${products}" begin="${var2.str}" end="${var2.str + 9}">
<tr bgColor=#fddddd>
<td align="center" width="40"><c:out value="${var.id}" /></td>
<td width="290"><c:out value="${var.name}" /></td>
<td align="right" width="70"><c:out value="${var.price}" />円</td>
<td>
<form method="post" action="/book/catalog/CatalogDetailShow">
<input type="hidden" name="id" value="${var.id}"/>
<br><input type="submit" name="submit" value="詳細"/>
</form>
</td>
</tr>
</c:forEach>
</table>
<c:if test="${var2.str >= 0}">
<c:if test="${var2.str < products.size() - products.size()%10 }">
<form method="post" action="/book/catalog/NextPage">
<input type="submit" name="submit" value="次のページ"/>
</form>
</c:if>
</c:if>
<c:if test="${var2.str > 9}">
<form method="post" action="/book/catalog/BackPage">
<input type="submit" name="submit" value="前のページ"/>
</form>
</c:if>
</c:forEach>

「次の10行を表示するServlet」

public class NextPage extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
OffsetBean offsetBean= new OffsetBean();
offsetBean.setStr(offsetBean.getStr() + 10);
table = new ArrayList<Object>();
table.add(offsetBean);
request.getSession(true).setAttribute("object1", table);
}catch(Exception e){
throw new ServletException(e);
}
getServletContext().getRequestDispatcher("catalog.jsp").forward(request,response);
}
private List<Object> table;
public List<Object> getTable() {
return table;
}
}

「Bean・・・OffsetBean.java」

public class OffsetBean {
private int str;
public void setStr(int str) {
this.str = str;
}
public int getStr() {
return this.str;
}
}

A 回答 (1件)

ソースみてないけど offset みてみ。

    • good
    • 1

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!