bookStore案例第一篇【部署開發環境、解決分類模塊】

前言

鞏固Servlet+JSP開發模式,作一個比較完整的小項目java

成果圖

該項目包含了兩個部分,前臺和後臺。sql

前臺用於顯示數據庫

這裏寫圖片描述

後臺用於管理瀏覽器

這裏寫圖片描述

該項目可分爲5個模塊來組成:分類模塊,用戶模塊,圖書模塊,購買模塊,訂單模塊markdown


搭建環境

創建包結構

這裏寫圖片描述

導入開發包

這裏寫圖片描述

前臺分幀頁面

  • index.jsp【沒有body標籤的】
<frameset rows="25%,*">
    <frame src="${pageContext.request.contextPath}/client/head.jsp"/>
    <frame src="${pageContext.request.contextPath}/client/body.jsp"/>
  </frameset>
  • head.jsp
<body style="text-align: center">
<h1>歡迎來到購物中心</h1>
  • body是空白的jsp頁面jsp

  • 效果:工具

這裏寫圖片描述


後臺分幀頁面

  • manager.jsp【嵌套了framset標籤,也是沒有body標籤的】
<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>
  • head.jsp
<body style="text-align: center">
<h1>後臺管理</h1>
  • left.jsp
<a href="#">分類管理</a> <br> <br> <a href="#">圖書管理</a> <br> <br> <a href="#">訂單管理</a> <br> <br> 
  • body.jsp是空白的post

  • 效果:測試

這裏寫圖片描述

分幀的文件夾目錄結構

這裏寫圖片描述

值得注意的是:spa

  • 文件夾的名字不能使用「manager」,否則會出現:403 Access Denied錯誤
  • frameset標籤是能夠嵌套的,分列用「cols」,分行用「rows」

導入工具類和方法的代碼

  • 過濾中文亂碼數據
  • HTML轉義
  • DAOFactory
  • JDBC鏈接池
  • UUID工具類
  • c3p0.xml配置文件

這些代碼均可以在個人博客分類:複用代碼中找到!


分類模塊

首先,咱們來作分類模塊吧

建立實體Category

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) );

編寫CategoryDAO

/** * 分類模塊 * 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);
        }

    }
}

測試DAO

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());
        }
    }

}

抽取成DAO接口

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>
  • 顯示添加分類的JSP頁面
<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> 
  • 處理添加分類的Servlet
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);

        }
  • 效果:

這裏寫圖片描述


後臺頁面的查看分類

  • 在超連接上,綁定處理請求的Servlet
else if (method.equals("look")) { List<Category> list = service.getAllCategory(); request.setAttribute("list", list); request.getRequestDispatcher("/background/lookCategory.jsp").forward(request, response); } 
  • 顯示分類頁面的JSP
<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>
  • 效果:

這裏寫圖片描述

相關文章
相關標籤/搜索