---------------------------------------------------------------action-----------------------------------------------------------------------------
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws ClassNotFoundException, SQLException
{
ShoperForm shoppingForm = (ShoperForm) form;
HttpSession session = request.getSession(false);
ShoppingCart car = new ShoppingCart();
car = (ShoppingCart) session.getAttribute("hash");
ArrayList totlebook = (ArrayList)servlet.getServletContext().getAttribute("Bookdata");
ArrayList pagebook = new ArrayList();
/*page获得翻页时获取的页数,一开始默认的页数为第1页 */
int page = 1;
String spage = request.getParameter("page");
if(spage == null)
{
page=1;
}
else
{
page = Integer.parseInt(spage);
}
/*sqan设置记录跨度*/
int sqan=3;
/*count获得总记录数*/
int count = totlebook.size();
/*countpages获得总页数*/
int countpages = count/sqan ;
if(count%sqan>0)
{
countpages=countpages+1;
}
/*翻上一页获得的page<1时,page还应设置为1*/
if(page<1)
{
page=1;
}
/*翻下一页获得的page>最大页数时,page应设置为最大页数*/
if(page>countpages)
{
page=countpages;
}
/*上一页页数*/
int page1=page-1;
/*下一页页数*/
int page2=page+1;
/*大于总记录数时*/
int fcount=(page-1)*sqan+sqan;
if(fcount>count)
{
fcount=count;
}
for(int i=(page-1)*sqan ; i<fcount ; i++)
{
pagebook.add(totlebook.get(i));
}
session.setAttribute("totlebook",pagebook);
session.setAttribute("page", page);
session.setAttribute("page1", page1);
session.setAttribute("page2", page2);
session.setAttribute("countpages",countpages);
Enumeration e=request.getParameterNames();
while(e.hasMoreElements())
{
String Bookname=(String) e.nextElement();
String temp[]=Bookname.split(",");
if(temp.length>2&&temp[0].equals("addbook"))
{
Book bk = new Book();
bk.setIsbn(temp[1]);
bk.setName(temp[2]);
bk.setPrice(temp[3]);
car.addBook(bk);
}
if(temp.length>2&&temp[0].equals("delete"))
{
car.delete(temp[1]);
}
}
Book[] books = car.Booklist();
session.setAttribute("books",books);
return (mapping.findForward("success"));
}
}
-------------------------------------------------------------------jsp-----------------------------------------------------------------------------
<%@ page language="java" import="java.util.*"%>
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.util.*" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="newUser" value="${user}" scope="session"/>
<c:set var="totlebook" value="${totlebook}" scope="session"/>
<c:set var="books" value="${books}" scope="session"/>
<c:set var="page" value="${page}" scope="session"/>
<c:set var="countpages" value="${countpages}" scope="session"/>
<c:set var="page1" value="${page1}" scope="session"/>
<c:set var="page2" value="${page2}" scope="session"/>
<html>
<head>
<title>JSP for ShoperForm form</title>
</head>
<body>
Welcome to New Bookonline!<bean:write name="newUser" property="username"/><br/>
You can select books in your shoppingcart as follows:<br>
BookList:<br>
<html:form action='shoper.do'>
<c:forEach var="booklist" items="${totlebook}">
<LI><input type=checkbox name="addbook,${booklist.isbn},${booklist.name},${booklist.price}" value="addBook">
<c:out value="isbn:${booklist.isbn}"/>
<c:out value="${booklist.name}"/>
<c:out value="${booklist.price}"/><br>
</c:forEach>
<br>第${page}/${countpages}页---<a href="shoper.do?page=${page1}">上一页</a>.<a href="shoper.do?page=${page2}">下一页</a><br>
<br><html:submit>
<bean:message key="button.addBook"/>
</html:submit>
</html:form><br>
Now In the ShoppingCar:<br>
<c:if test="${empty books}">
None
</c:if>
<html:form action='shoper.do'>
<c:forEach var="mybooklist" items="${books}">
<LI><input type=checkbox name="delete,${mybooklist.isbn},${mybooklist.name},${mybooklist.price}" value=checkout>
<c:out value="isbn:${mybooklist.isbn}"/>
<c:out value="${mybooklist.name}"/>
<c:out value="${mybooklist.price}"/><br>
</c:forEach><br>
<html:submit>
<bean:message key="button.delete"/>
</html:submit>
</html:form><br>
<html:form action='checkout.do'>
<html:submit value="checkout"/>
</html:form><br>
</body>
</html>