1、包结构。
2、使用了Spring 的 JdbcTemplate 对底层数据进行封装。所以一些必要的包和配置文件要编写正确。
3、控制层只使用一个Servlet实现。
//BookServlet
@SuppressWarnings("serial")
public class BookServlet extends HttpServlet {
//members
private BookDao bookDao = new BookDao();
private HttpSession session = null;
public void doGet(HttpServletRequest request, HttpServletResponse response){
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response){
//the main part for processing requests
//request --> bookServlet
//action
String action = request.getParameter("action");
if(action.equals("listBook")){
//list all books
listBook(request,response);
}
else if(action.equals("addBookToCart")){
//add book to cart
}
else if(action.equals("buyBook")){
//buy books
}
}
/**
* @function list all books
* @param request
* @param response
*/
public void listBook(HttpServletRequest request, HttpServletResponse response){
List<Book> booksList = bookDao.getBooks();
session = request.getSession();
session.setAttribute("booksList", booksList);
try {
response.sendRedirect("booklist.jsp");//重定向
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//bookServlet在web.xml中的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>myOnlineBookSys</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</context-param>
<servlet>
<servlet-name>bookServlet</servlet-name>
<servlet-class>com.whiteblack.control.BookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bookServlet</servlet-name>
<url-pattern>/bookServlet</url-pattern>
</servlet-mapping>
</web-app>
4、booklist.jsp
<%@page import="com.whiteblack.bean.Book"%>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'bookslist.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="content">
<table width="100%" align="center">
<tr>
<td bgcolor="#FFFFFF">
<table width="99%" align="center" bgcolor="#FFFFFF" cellspacing="1">
<tr bgcolor="#EEEEEE" height="40">
<td width="5%" style="font-size:14px;font-weight:bold">书号</td>
<td width="30%" style="font-size:14px;font-weight:bold">书本名称</td>
<td width="15%" style="font-size:14px;font-weight:bold">作者</td>
<td width="15%" style="font-size:14px;font-weight:bold">出版社</td>
<td width="5%" style="font-size:14px;font-weight:bold">价格</td>
<td width="10%" style="font-size:14px;font-weight:bold">库存</td>
<td width="10%" style="font-size:14px;font-weight:bold">购买数量</td>
<td width="5%" style="font-size:14px;font-weight:bold">购买</td>
</tr>
<%
List<Book> booksList =(List<Book>)session.getAttribute("booksList");
//test
System.out.println(request.getParameter("action"));
System.out.println(booksList);
for(int i=0; i<booksList.size(); i++ )
{
Book book = booksList.get(i);
System.out.println(booksList.get(i));
%>
<form action="bookServlet" method="post">
<input type="hidden" name="action" value="addBookToCart"/>
<%
if(i%2 == 0){
%>
<tr bgcolor="#FFFFFF">
<td width="5%"><%=book.getbNumber() %></td>
<td width="30%"><%=book.getbName() %></td>
<td width="15%"><%=book.getbAuther() %></td>
<td width="15%"><%=book.getbPublish() %></td>
<td width="5%">¥<%=book.getbPrice() %></td>
<td width="10%">剩余<%=book.getbStock() %>本</td>
<td width="15%">买<input type="text" name="bookNum" size="5"/>本</td>
<td width="5%"><input type="hidden" value="">
<input type="submit" value="购买"/></td>
</tr>
<%
}
else{
%>
<tr bgcolor="#EEEEEE">
<td width="5%"><%=book.getbNumber() %></td>
<td width="30%"><%=book.getbName() %></td>
<td width="15%"><%=book.getbAuther() %></td>
<td width="15%"><%=book.getbPublish() %></td>
<td width="5%">¥<%=book.getbPrice() %></td>
<td width="10%">剩余<%=book.getbStock() %>本</td>
<td width="15%">买<input type="text" name="bookNum" size="5"/>本</td>
<td width="5%"><input type="hidden" value="<%=book.getbNumber()%>" name="bookNumber">
<input type="submit" value="购买"/></td>
</tr>
<%
}
%>
</form>
<%
}
%>
</table>
</table>
</div>
</body>
</html>
效果:
系统还没开发完毕,后续会继续更新