概念

本文讲解和实现类目管理和管理员的退出功能。

类目列表信息

【JavaWeb】网上蛋糕商城后台-类目管理,退出-LMLPHP

点击类目管理,向服务器发送请求/admin/type_list

在servlet包中创建AdminTypeListServlet类,获得所有商品分类

package servlet;

import model.Type;
import service.TypeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "admi_type_list",urlPatterns = "/admin/type_list")
public class AdminTypeListServlet extends HttpServlet {
    private TypeService tService = new TypeService();
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Type> list= tService.GetAllType();
        request.setAttribute("list", list);
      this.getServletContext().removeAttribute("typeList");
        this.getServletContext().setAttribute("typeList",list);
        request.getRequestDispatcher("/admin/type_list.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}

以上代码中,通过调用业务逻辑层获得数据库中的所有分类列表信息,并将上下文对象中保存的类目列表信息删除,重新保存。并将获取的类目列表信息发送到type_list.jsp页面上显示

<%@ page language="java" contentType="text/html; charset=utf-8"
       pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
   <title>类目列表</title>
   <meta charset="utf-8"/>
   <link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>
<div class="container-fluid">
   <jsp:include page="/admin/header.jsp"></jsp:include>
   <br>
   <div>
      <form class="form-inline" method="post" action="/admin/type_add">
         <input type="text" class="form-control" id="input_name" name="name" placeholder="输入类目名称" required="required" style="width: 500px">
         <input type="submit" class="btn btn-warning" value="添加类目"/>
      </form>
   </div>
   <br/>
   <c:if test="${!empty msg }">
      <div class="alert alert-success">${msg }</div>
   </c:if>
   <c:if test="${!empty failMsg }">
      <div class="alert alert-danger">${failMsg }</div>
   </c:if>
   <br>
   <table class="table table-bordered table-hover">
      <tr>
         <th width="5%">ID</th>
         <th width="10%">名称</th>
         <th width="10%">操作</th>
      </tr>

      <c:forEach items="${list }" var="t">
         <tr>
            <td><p>${t.id }</p></td>
            <td><p>${t.name }</p></td>
            <td>
               <a class="btn btn-primary" href="/admin/type_edit.jsp?id=${t.id }&name=${t.encodeName }">修改</a>
               <a class="btn btn-danger" href="/admin/type_delete?id=${t.id }">删除</a>
            </td>
         </tr>
      </c:forEach>
   </table>
</div>
</body>
</html>

【JavaWeb】网上蛋糕商城后台-类目管理,退出-LMLPHP

添加类目

在上图中,文本输入框内输入要新增的类目名称,按回车键或者点击“添加类目”按钮,向服务器发送请求/admin/type_add

在servlet包中创建AdminTypeAddServlet类

package servlet;

import model.Type;
import service.TypeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "admin_type_add",urlPatterns = "/admin/type_add")
public class AdminTypeAddServlet extends HttpServlet {
    private TypeService tService = new TypeService();
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        tService.insert(new Type(name));
        request.getRequestDispatcher("/admin/type_list").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}

以上代码中,接收新增类目的名称发送给业务逻辑层执行,添加成功后同步刷新类目列表页面

//添加新的分类信息
public void insert(Type t) {
    try {
        tDao.insert(t);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

业务逻辑层将新增类目对象传递给数据访问层

//管理员添加新的分类
public void insert(Type t) throws SQLException {
    QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
    String sql = "insert into type(name) values(?)";
    r.update(sql,t.getName());
}

最后刷新列表页面。

修改类目

在类目列表页面中,选择要修改的类目的“修改”按钮,向/admin/type_edit.jsp页面跳转并携带类目的编号和名称

<%@ page language="java" contentType="text/html; charset=utf-8"
       pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
   <title>类目编辑</title>
   <meta charset="utf-8"/>
   <link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>
<div class="container-fluid">
   <jsp:include page="/admin/header.jsp"></jsp:include>
   <br><br>
   <form class="form-horizontal" action="/admin/type_edit" method="post">
      <input type="hidden" name="id" value="${param.id }">
      <div class="form-group">
         <label for="input_name" class="col-sm-1 control-label">类目名称</label>
         <div class="col-sm-6">
            <input type="text" class="form-control" id="input_name" name="name" value="${param.name }" required="required">
         </div>
      </div>
      <div class="form-group">
         <div class="col-sm-offset-1 col-sm-10">
            <button type="submit" class="btn btn-success">提交修改</button>
         </div>
      </div>
   </form>

   <span style="color:red;"></span>

</div>
</body>
</html>

【JavaWeb】网上蛋糕商城后台-类目管理,退出-LMLPHP

当管理员修改类目之后,点击提交修改按钮,将该类目的编号以及更新后的类目名称发送给服务器/admin/type_edit

在servlet包中创建AdminTypeEditServlet类

package servlet;

import model.Type;
import org.apache.commons.beanutils.BeanUtils;
import service.TypeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "admin_type_edit",urlPatterns = "/admin/type_edit")
public class AdminTypeEditServlet extends HttpServlet {
    private TypeService tService = new TypeService();
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Type t = new Type();
        try {
            BeanUtils.copyProperties(t, request.getParameterMap());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        tService.update(t);
        request.getRequestDispatcher("/admin/type_list").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}

以上代码中,将接收的参数从map集合转换封装在type对象中,并发送给业务逻辑层执行

//修改分类信息
public void update(Type t) {
    try {
        tDao.update(t);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

接着,传递给数据访问层执行操作

//修改分类信息
public void update(Type t) throws SQLException {
    QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
    String sql = "update type set name=? where id = ?";
    r.update(sql,t.getName(),t.getId());
}

最后,刷新类目类表页面。

删除类目

在类目类表页面中,选择要删除的类目中的“删除”按钮向服务器发送请求地址/admin/type_delete并携带类目编号

在servlet包中创建AdminTypeDeleteServlet类

package servlet;

import service.TypeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "admin_type_delete",urlPatterns = "/admin/type_delete")
public class AdminTypeDeleteServlet extends HttpServlet {
    private TypeService tService = new TypeService();
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean isSuccess = tService.delete(id);
        if(isSuccess) {
            request.setAttribute("msg", "删除成功");
        }else {
            request.setAttribute("failMsg", "类目下包含商品,无法直接删除类目!");
        }
        request.getRequestDispatcher("/admin/type_list").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}

以上代码中,将接收的类目编号作为条件发送给业务逻辑层执行,如果返回值为true说明删除成功,否则删除失败,该类目下有商品关联不能删除,最后刷新类目列表页面。

//根据id删除分类
public boolean delete(int id) {
    try {
        tDao.delete(id);
        return true;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }
}

接着,将类目编号发送给数据访问层执行

//删除分类信息
public void delete(int id) throws SQLException {
    QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
    String sql = "delete from type where id = ?";
    r.update(sql,id);
}

最后,无论是否删除成功,都要刷新类目列表页面,通过提示信息告知管理员结果。

05-10 06:53