删除图书分类管理

删除图书分类管理

package com.imooc.web.servlet;


import java.io.IOException;


import javax.servlet.ServletContext;

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 com.alibaba.fastjson.JSON;

import com.imooc.service.impl.CategoryServiceImpl;


/**

 * Servlet implementation class addCategory

 */

@WebServlet("/addCategory")

public class addCategory extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public addCategory() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

//response.getWriter().append("Served at: ").append(request.getContextPath());

request.setCharacterEncoding("UTF-8");

String classId=request.getParameter("categoryId");

String className=request.getParameter("categoryName");

CategoryServiceImpl csi=new CategoryServiceImpl();

csi.addCategory(classId, className);

request.getSession().setAttribute("category", csi.getCategorydb());

System.out.println("classId"+classId+"  className"+className);

request.getRequestDispatcher("/categoryList.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);

}


}

package com.imooc.service.impl;


import java.util.ArrayList;

import java.util.List;


import com.imooc.domain.Category;

import com.imooc.service.CategoryService;


public class CategoryServiceImpl implements CategoryService {

private static final List<Category> categoryDb=new ArrayList<Category>();

public static List<Category> getCategorydb() {

return categoryDb;

}


@Override

public void addCategory(String categoryId, String catgoryName) {

// TODO Auto-generated method stub

Category cg=new Category();

cg.setClassId(categoryId);

cg.setClassName(catgoryName);

categoryDb.add(cg);

}


@Override

public void deleteCategory(String categoryId) {

// TODO Auto-generated method stub

for(Category c:categoryDb) {System.out.println("categoryId:"+categoryId+"   c.categoryId:"+c.getClassId());

if(c.getClassId().equals(categoryId)) {

categoryDb.remove(c);

return;

}

}

}


}

package com.imooc.domain;


public class Category {

private String classId;

private String className;

public String getClassId() {

return classId;

}

public void setClassId(String classId) {

this.classId = classId;

}

public String getClassName() {

return className;

}

public void setClassName(String className) {

this.className = className;

}

@Override

public String toString() {

return "Category [classId=" + classId + ", className=" + className + "]";

}

/*

 * classId 图书分类ID

 * className 分类名称

 */

public Category(String classId, String className) {

super();

this.classId = classId;

this.className = className;

}

public Category() {

super();

// TODO Auto-generated constructor stub

}


}

package com.imooc.web.servlet;


import java.io.IOException;

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 com.imooc.service.impl.CategoryServiceImpl;


/**

 * Servlet implementation class deleteCategory

 */

@WebServlet("/deleteCategory")

public class deleteCategory extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public deleteCategory() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

//response.getWriter().append("Served at: ").append(request.getContextPath());

String classId=request.getParameter("categoryId");System.out.println("*****************");

System.out.println("categoryId:"+classId);

CategoryServiceImpl csi=new CategoryServiceImpl();

csi.deleteCategory(classId);

request.getSession().setAttribute("category", csi.getCategorydb());

request.getRequestDispatcher("/categoryList.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);

}


}

<%@ 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>

        <meta charset="UTF-8">

        <title>图书后台管理</title>

        <link rel="stylesheet" href="css/index.css">

        <link rel="stylesheet" href="css/bootstrap.min.css">

    </head>

    <script type="text/javascript" src="js/jquery-3.4.1.js"></script>

<script type="text/javascript">

$(function(){

$.ajax({

})

})

</script>

    <body>

       <header>

            <div class="container">

                    <nav>

                            <a href="bookList.html" >图书信息管理</a>

                    </nav>

                    <nav>

                            <a href="categoryList.html" >分类管理</a>

                    </nav>

                   

            </div>

        </header>

        <section class="banner">

            <div class="container">

                <div>

                    <h1>图书管理系统</h1>

                    <p>图书分类管理</p>

                </div>

            </div>

        </section>

        <section class="main">

            <div class="container">

                <table class="table table-striped">

                    <thead>

                    <tr>

                        <th>序号</th>

                        <th>分类编号</th>

                        <th>分类名称</th>

                        <th>操作</th>

                    </tr>

                    </thead>

                    <tbody>

                        <c:forEach items="${category}" var="ca" varStatus="idx">

                            <tr>

                                <td>${idx.index}</td>

                                <td>${ca.classId}</td>

                                <td>${ca.className}</td>

                                <td><a href="/zuoyebook/deleteCategory?categoryId=${ca.classId}">删除</a></td>

                                <!--在循环显示数据时,此处的ca0001可以用EL表达式进行替换-->


                            </tr>

                        </c:forEach>

                    </tbody>

                </table>

            </div>

        </section>

        <section class="page">

            <div class="container">

                <div id="fatie">

                    <a href="addCategory.html"><button>新建</button></a>

                </div>

            </div>

        </section>

        <footer>

            copy@慕课网

        </footer>

    </body>

</html>

http://img1.sycdn.imooc.com//climg/5e55eed608827a8f12640430.jpghttp://img1.sycdn.imooc.com//climg/5e55efd908e1158f09170351.jpg点击删除后出现500的情况或者序号和操作还存在的情况,老师,这是哪里有问题?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕小尤 2020-02-26 18:25:29

同学你好,1. SendRedirect 重定向,浏览器地址栏变为目标地址。而forword转发,浏览器地址栏不变,当刷新页面时会添加或删除无效的数据。所以需要使用 重定向。

2. 区别: request.getRequestDispatcher()是请求转发,前后页面共享一个request 。

    response.sendRedirect()是重新定向,前后页面不是一个request。

    RequestDispatcher.forward()是在服务器端运行。

    HttpServletResponse.sendRedirect()是通过向客户浏览器发送命令来完成。

3. SendRedirect 重定向,url是截止到项目名前,例如:http://127.0.0.1:8080/    访问页面时候需要加上项目名,如项目为ssm :response.sendRedirect("/ssm/loginPage")

而forword转发,url是截止到项目名后,例如:http://127.0.0.1:8080/ssm/  访问页面直接写访问地址:  如项目为ssm :request.getRequestDispacther("/login.jsp").forword(request,response)

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

好帮手慕小尤 2020-02-26 15:54:46

同学你好,1. 同学的代码报空异常,同学可以在删除前进行判断,判断列表中是否存在此id。

2. 同学描述的是否是空数据。如下图所示:

http://img1.sycdn.imooc.com//climg/5e561d6b09c422b810200081.jpg

如果是,则是因为同学在添加或删除数据后,并没有修改路径导致添加空数据,或在删除时传递无效的id,所以建议同学使用sendRedirect()进行重定向。如下所示:

http://img1.sycdn.imooc.com//climg/5e56244109e5dc3e08550290.jpghttp://img1.sycdn.imooc.com//climg/5e56243109309ef709530256.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 慕的地2082093 #1
    老师我开始使用request.getRequestDispatcher("/categoryList.jsp").forward(request, response);为什么不行, response.sendRedirect("/zuoyebook/categoryList.jsp"); getRequestDispatcher和response.sendRedirect有什么区别?为什么getRequestDispatcher,不需要加工程名?sendRedirect却需要加工程名在家需要跳转的页面,不然会出现404的情况?
    2020-02-26 16:43:15
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师