鞏固Servlet+JSP開發模式,作一個比較完整的小項目java
該項目包含了兩個部分,前臺和後臺。sql
前臺用於顯示數據庫
後臺用於管理瀏覽器
該項目可分爲5個模塊來組成:分類模塊,用戶模塊,圖書模塊,購買模塊,訂單模塊。markdown
<frameset rows="25%,*"> <frame src="${pageContext.request.contextPath}/client/head.jsp"/> <frame src="${pageContext.request.contextPath}/client/body.jsp"/> </frameset>
<body style="text-align: center"> <h1>歡迎來到購物中心</h1>
body是空白的jsp頁面jsp
效果:工具
<frameset rows="25%,*"> <frame src="${pageContext.request.contextPath}/background/head.jsp"/> <frameset cols="15%,*"> <frame src="${pageContext.request.contextPath}/background/left.jsp"/> <frame src="${pageContext.request.contextPath}/background/body.jsp"/> </frameset> </frameset>
<body style="text-align: center"> <h1>後臺管理</h1>
<a href="#">分類管理</a> <br> <br> <a href="#">圖書管理</a> <br> <br> <a href="#">訂單管理</a> <br> <br>
body.jsp是空白的post
效果:測試
值得注意的是:spa
這些代碼均可以在個人博客分類:複用代碼中找到!
首先,咱們來作分類模塊吧
private String id; private String name; private String description; //各類setter、getter
CREATE TABLE category ( id VARCHAR(40) PRIMARY KEY, name VARCHAR(10) NOT NULL UNIQUE , description VARCHAR(255) );
/** * 分類模塊 * 1:添加分類 * 2:查找分類 * 3:修改分類 * * * */ public class CategoryImpl { public void addCategory(Category category) { QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource()); String sql = "INSERT INTO category (id, name, description) VALUES(?,?,?)"; try { queryRunner.update(sql, new Object[]{category.getId(), category.getName(), category.getDescription()}); } catch (SQLException e) { throw new RuntimeException(e); } } public Category findCategory(String id) { QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource()); String sql = "SELECT * FROM category WHERE id=?"; try { Category category = (Category) queryRunner.query(sql, id, new BeanHandler(Category.class)); return category; } catch (SQLException e) { throw new RuntimeException(e); } } public List<Category> getAllCategory() { QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource()); String sql = "SELECT * FROM category"; try { List<Category> categories = (List<Category>) queryRunner.query(sql, new BeanListHandler(Category.class)); return categories; } catch (SQLException e) { throw new RuntimeException(e); } } }
public class demo { @Test public void add() { Category category = new Category(); category.setId("2"); category.setName("數據庫系列"); category.setDescription("這是數據庫系列"); CategoryImpl category1 = new CategoryImpl(); category1.addCategory(category); } @Test public void find() { String id = "1"; CategoryImpl category1 = new CategoryImpl(); Category category = category1.findCategory(id); System.out.println(category.getName()); } @Test public void getAll() { CategoryImpl category1 = new CategoryImpl(); List<Category> categories = category1.getAllCategory(); for (Category category : categories) { System.out.println(category.getName()); } } }
public interface CategoryDao { void addCategory(Category category); Category findCategory(String id); List<Category> getAllCategory(); }
<a href="${pageContext.request.contextPath}/background/addCategory.jsp" target="body">添加分類</a>
<form action="${pageContext.request.contextPath}/CategoryServlet?method=add" method="post"> 分類名稱:<input type="text" name="name"><br> 分類描述:<textarea name="description"></textarea><br> <input type="submit" value="提交"> </form>
if (method.equals("add")) { try { //把瀏覽器帶過來的數據封裝到bean中 Category category = WebUtils.request2Bean(request, Category.class); category.setId(WebUtils.makeId()); service.addCategory(category); request.setAttribute("message", "添加分類成功!"); } catch (Exception e) { request.setAttribute("message","添加分類失敗"); e.printStackTrace(); } request.getRequestDispatcher("/message.jsp").forward(request, response); }
else if (method.equals("look")) { List<Category> list = service.getAllCategory(); request.setAttribute("list", list); request.getRequestDispatcher("/background/lookCategory.jsp").forward(request, response); }
<c:if test="${empty(list)}"> 暫時尚未分類數據哦,請你添加把 </c:if> <c:if test="${!empty(list)}"> <table border="1px"> <tr> <td>分類名字</td> <td>分類描述</td> <td>操做</td> </tr> <c:forEach items="${list}" var="category"> <tr> <td>${category.name}</td> <td>${category.description}</td> <td> <a href="#">刪除</a> <a href="#">修改</a> </td> </tr> </c:forEach> </table> </c:if>