关于分类ID问题

关于分类ID问题

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>科室列表</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/index.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
</head>

<body>
<header>
    <div class="container">

        <nav>
            <a href="internal.do?id=${1}" >内科</a>
        </nav>
        <nav>
            <a href="surgery.do?id=${3}" >外科</a>
        </nav>
        <nav>
            <a href="list.do" >分类</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="${LIST}" var="cat">
                <tr>
                    <td>${cat.name}</td>
                    <td>${cat.id}</td>
                    <td>${cat.createTime}</td>
                    <td>${cat.updateTime}</td>
                    <td>

                        <a href="toEdit.do?id=${cat.id}">修改</a>
                        <a href="remove.do?id=${cat.id}">删除</a>

                    </td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </div>
</section>
<section class="page">
    <div class="container">
        <div id="fatie">
            <a href="toAdd.do"><button>新建</button></a>
        </div>
    </div>
</section>
<footer>
    copy@慕课网
</footer>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>科室列表</title>
    <link rel="stylesheet" href="../css/index.css">
    <link rel="stylesheet" href="../css/bootstrap.min.css">
</head>

<body>
<header>
    <div class="container">

        <nav>
            <a href="internal.do" >内科</a>
        </nav>
        <nav>
            <a href="surgery.do" >外科</a>
        </nav>
        <nav>
            <a href="list.do" >分类</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="${LIST}" var="dep">
                <tr>
                    <td>${dep.name}</td>
                    <td>${dep.categoryId}</td>
                    <td>${dep.createTime}</td>
                    <td>${dep.updateTime}</td>
                    <td>

                        <a href="">修改</a>
                        <a href="">删除</a>

                    </td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </div>
</section>
<section class="page">
    <div class="container">
        <div id="fatie">
            <a href="toAdd_i.do"><button>新建</button></a>
        </div>
    </div>
</section>
<footer>
    copy@慕课网
</footer>
</body>
</html>
package com.imooc.hp.controller;

import com.imooc.hp.entity.Category;
import com.imooc.hp.entity.Department;
import com.imooc.hp.service.CategoryService;
import com.imooc.hp.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

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

@Controller("categoryController")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DepartmentService departmentService;

    //分类
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> list=categoryService.getAll();
        request.setAttribute("LIST",list);
        request.getRequestDispatcher("../category.jsp").forward(request,response);
    }


    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("../add_c.jsp").forward(request,response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");

        Category category=new Category();
        category.setName(name);

        categoryService.add(category);
        response.sendRedirect("list.do");
    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        Category category=categoryService.get(id);
        request.setAttribute("OBJ",category);
        request.getRequestDispatcher("../edit.jsp").forward(request,response);
    }
    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        String name=request.getParameter("name");

        Category category=new Category();
        category.setId(id);
        category.setName(name);

        categoryService.edit(category);
        response.sendRedirect("list.do");
    }

    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        categoryService.remove(id);
        response.sendRedirect("list.do");
    }

    //内科
    public void internal(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int categoryId=Integer.parseInt(request.getParameter("id"));
        List<Department> list=departmentService.getAll(categoryId);
        request.setAttribute("LIST",list);
        request.getRequestDispatcher("../category_n.jsp").forward(request,response);
    }
    public void toAdd_i(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> cat=categoryService.getAll();
        request.setAttribute("CAT",cat);
        request.getRequestDispatcher("../add_i.jsp").forward(request,response);
    }
    public void add_i(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");
        int categoryId=Integer.parseInt(request.getParameter("categoryId"));

        Department department=new Department();
        department.setName(name);
        department.setCategoryId(categoryId);

        departmentService.add(department);
        response.sendRedirect("internal.do");
    }

    //外科
    public void surgery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int categoryId=Integer.parseInt(request.getParameter("categoryId"));
        List<Department> list=departmentService.getAll(categoryId);
        request.setAttribute("LIST",list);
        request.getRequestDispatcher("../category_w.jsp").forward(request,response);
    }
    public void toAdd_s(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("../add_s.jsp").forward(request,response);
    }
    public void add_s(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");

        Department department=new Department();
        department.setName(name);

        departmentService.add(department);
        response.sendRedirect("surgery.do");
    }
}

当我进入内科列表时就如下显示,不知道怎么改

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

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

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

3回答
好帮手慕柯南 2020-04-27 09:59:55

同学你好!

你这里是将分类id保存在了category中

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

但是你在获取时是讲解获取科室中的分类id属性

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

你应该获取科室下分类属性下的分类id属性,比如:

${dep.category.categoryId}

祝学习愉快~



好帮手慕柯南 2020-04-26 18:47:56

同学你好!

在代码的左侧打断点就可以,比如:

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

启动时以debug模式启动,比如:

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

执行相应的代码就会进入断点,你就可以看到数据了

祝学习愉快~


  • 提问者 masami酱 #1
    打断点后查到的分类ID都是0;是不是我的查询代码有问题了?我的查询代码如下: <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.imooc.hp.dao.DepartmentDao"> <resultMap id="resultMap" type="Department"> <id property="id" column="id" javaType="int"/> <result property="name" column="name" javaType="String"/> <result property="createTime" column="create_time" javaType="java.util.Date"/> <result property="updateTime" column="update_time" javaType="java.util.Date"/> <association property="category" column="category_id" javaType="Category" select="com.imooc.hp.dao.CategoryDao.selectById"/> </resultMap> <insert id="insert" parameterType="Department" useGeneratedKeys="true"> insert into dept(name,category_id,create_time,update_time) values (#{name},#{categoryId},#{createTime},#{updateTime}) </insert> <delete id="delete" parameterType="int"> delete from dept where id=#{id} </delete> <update id="update" parameterType="Department"> update dept set name=#{name},update_time=#{updateTime} where id=#{id} </update> <select id="selectById" parameterType="Integer" resultMap="resultMap"> select * from dept where id=#{id} </select> <select id="selectAll" resultMap="resultMap"> select * from dept where category_id=#{categoryId} </select> </mapper>
    2020-04-26 20:17:17
好帮手慕柯南 2020-04-26 11:36:57

同学你好!

建议你在后台打断点,查询一下你查询到的List中是否有查到分类的id呢?

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

建议一下你查询的sql是否有误

如果仍有问题,可以继续追问

祝学习愉快~

  • 提问者 masami酱 #1
    怎么打断点查看?
    2020-04-26 16:30:27
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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