没有查询到数据

来源:3-2 分类管理表现层

Wchisper

2019-07-26 19:14:27

http://img.mukewang.com/climg/5d3ade9c0001bd0409150633.jpg

http://img.mukewang.com/climg/5d3ade9c0001f97319170758.jpg

package com.coke.cake.global;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
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.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
 *需求困难:
 *       1:如果每一次请求都写一个servlet过会非常复杂
 *       2:如果我们想统一的处理这些servlet也是没有办法同意的
 * 新的解决思路:
 *      我们将所有的用户请求,交给一个servlet来处理,这个servlet并不做任何的请求处理。
 * 但是它根据我们的请求(通过前台传递的url分辨出哪一个servlet进行处理),让它告诉程序,哪
 * 一个servlet进行处理
 * */

public class GlobalController extends GenericServlet {
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        /*
         * url分析:
         *   以 ".do" 为后缀结尾的交给我们这个servlet出理
         *   /Login.do           不是前台和后台 :默认一个servlet   login
         *   /Cake/detail.do         detail
         *   /admin/Cake/add.do    add
         * */

        //强制转换得到HttpServletXxx 下的request/response
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        //获取url的路径格式
        String path = request.getServletPath();
        System.out.println(path);
        System.out.println("---------------");

        //判断传过来的是后台页面还是前台页面或者登录页面
        if (path.indexOf("/admin") != -1) {
            //代表path是后台页面
            path = path.substring(7);
        } else {
            path = path.substring(1);
        }
        /*
         * 经过上面判断后url
         *    login.do
         *    cake/detail.do    类名:cakeServlet  方法名:detail
         *    cake/add.do       类名:cakeServlet  方法名:add
         * */
        int index = path.indexOf("/");
        String className = null;
        String methodName = null;
        if (index != -1) {
            className = "com.coke.cake.servlet." + path.substring(0, index) + "Servlet";
            methodName = path.substring(index + 1, path.indexOf(".do"));
        } else {
            className = "com.coke.cake.servlet.defaultServlet";
            methodName = path.substring(0, path.indexOf(".do"));
        }
        System.out.println(className+"--------"+methodName);
        try {
            /*
             * 通过反射执行这个方法
             * */
            //通过class的静态方法,传递className
            Class c = Class.forName(className);
            Object object = c.newInstance();
            Method method = c.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //参数一:调用这个方法的对象是哪一个,参数二,三传递调用方法的参数
            method.invoke(object, request, response);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

CatalogServlet中查询所有分类的方法

/**
 * 查询所有分类
 * url="/admin/Catalog/list.do"
 *
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //调用业务
    Catalog root = catalogService.getRoot();

    Integer id = root.getId();
    Integer pid = root.getPid();
    String info = root.getInfo();
    root.getChildren();
    System.out.println(id + "----" + pid + "---" + info);
    //页面跳转
    request.setAttribute("root", root);
    request.getRequestDispatcher("/WEB-INF/pages/admin/catalog_list.jsp").forward(request, response);
}

CatalogService接口

package com.coke.cake.service;

import com.coke.cake.entity.Catalog;

import java.util.List;

public interface CatalogService {
    //添加方法,批量添加
    void add(List<Catalog> list);

    //删除方法
    void delete(int id);

    //获取分类
    Catalog getRoot();
}

CatalogServceImpl

package com.coke.cake.service.CatalogServiceImpl;

import com.coke.cake.dao.CatalogDao;
import com.coke.cake.entity.Catalog;
import com.coke.cake.global.DaoFactory;
import com.coke.cake.service.CatalogService;

import java.util.List;

public class CatalogServiceImpl implements CatalogService {
    private CatalogDao catalogDao = DaoFactory.getInstance().getDao(CatalogDao.class);


    public void add(List<Catalog> list) {
        catalogDao.beachInsert(list);
    }

    public void delete(int id) {
        catalogDao.delete(id);
    }

    public Catalog getRoot() {

        return catalogDao.select(10000);
    }
}

CatalogDao

package com.coke.cake.dao;

import com.coke.cake.entity.Catalog;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CatalogDao {
    /**
     * 插入数据和批量插入数据
     * 直接嵌入脚本
     * @param list
     */
    @Insert("<script>" +
            "insert into catalog (title,pid,info) vales" +
            "<foreach collection='list' item='catalog' separator=',' >" +
            "(#{catalog.title},#{catalog.pid},#{catalog.info})" +
            "</foreach>" +
            "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void beachInsert(List<Catalog> list);

    /**
     * 删除数据
     *
     * @param id
     */
    @Delete("delete from catalog where id=#{id}")
    void delete(int id);

    /**
     * 跟据id查询最高级分类
     *
     * @param id
     * @return
     */
    @Select("select * from catalog where id=#{id}")
    @Results(id = "all", value = {          //all是别名,当多个方法同时用相同的注解时,相当于resultMap
            @Result(column = "id", property = "id", id = true), //相当于<resultMap>下面的<result>
            @Result(column = "title", property = "title"),  //相当于<resultMap>下面的<result>
            @Result(column = "pid", property = "pid"),
            @Result(column = "info", property = "info"),
            @Result(column = "id", property = "children", many = @Many(select = "selectByPid"))
    })
    Catalog select(int id);

    /**
     * 根据它父类的id,查询它下面所有的子类
     *
     * @param pid
     * @return
     */
    @Select("select * from catalog where pid=#{pid}")
    @ResultMap("all")
    List<Catalog> selectByPid(int pid);
}

web.xml

<!--过滤器配置-->
<filter>
    <filter-name>encoding</filter-name>
    <filter-class>com.coke.cake.global.EncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>GlobalServlet</servlet-name>
    <servlet-class>com.coke.cake.global.GlobalController</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>GlobalServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

启动服务器后控制台也没有报错

http://img.mukewang.com/climg/5d3ae0060001917216710796.jpg

图中是打印的是CataloServlet中list方法,在上面有代码,发现没后获取到数据库中的信息

写回答

6回答

好帮手慕阿满

2019-07-27

同学你好,在catalog_list.jsp页面中,没有引入jstl的标签库,如:

http://img.mukewang.com/climg/5d3bf04100019e0b08850142.jpg

建议同学添加jstl的标签库再试试,

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

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

0
hchisper
h 老师,我在top中写jstl的标签库了, catalog_list中<jsp:include page="top.jsp"/> 不能把标签库 也带过来吗?
h019-07-27
共1条回复

Wchisper

提问者

2019-07-27

Catalog_list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="top.jsp"/>
<section id="content_wrapper">
    <section id="content" class="table-layout animated fadeIn">
        <div class="tray tray-center">
            <div class="content-header">
                <h2> 分类管理</h2>
                <p class="lead"></p>
            </div>
            <div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
                <div class="panel  heading-border">
                    <div class="panel-menu">
                        <div class="row">
                            <div class="hidden-xs hidden-sm col-md-3">
                                <div class="btn-group">
                                    <button type="button" class="btn btn-default light">
                                        <i class="fa fa-trash"></i>
                                    </button>
                                    <button type="button" class="btn btn-default light">
                                        <i class="fa fa-plus"
                                           onclick="javascript:window.location.href='/admin/Catalog/toAdd.do';"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="panel-body pn">
                        <table id="message-table" class="table admin-form theme-warning tc-checkbox-1">
                            <thead>
                            <tr class="">
                                <th class="text-center hidden-xs">Select</th>
                                <th class="hidden-xs">名称</th>
                                <th class="hidden-xs">描述</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:forEach items="${root.children}" var="cat1">
                                <tr class="message-unread">
                                    <td class="hidden-xs">
                                        <label class="option block mn">
                                            <input type="checkbox" name="mobileos" value="FR">
                                            <span class="checkbox mn"></span>
                                        </label>
                                    </td>
                                    <td>${cat1.title}</td>
                                    <td>${cat1.info}</td>
                                    <td>
                                        <a href="${pageContext.request.contextPath}/admin/Catalog/delete.do?id=${cat1.id}">删除</a>
                                    </td>
                                </tr>
                                <c:forEach items="${cat1.children}" var="cat2">
                                    <tr class="message-unread">
                                        <td class="hidden-xs">
                                            <label class="option block mn">
                                                <input type="checkbox" name="mobileos" value="FR">
                                                <span class="checkbox mn"></span>
                                            </label>
                                        </td>
                                        <td style="padding-left: 50px;">${cat2.title}</td>
                                        <td>${cat2.info}</td>
                                        <td>
                                            <a href="${pageContext.request.contextPath}/admin/Catalog/delete.do?id=${cat2.id}">删除</a>
                                        </td>
                                    </tr>
                                    <c:forEach items="${cat2.children}" var="cat3">
                                        <tr class="message-unread">
                                            <td class="hidden-xs">
                                                <label class="option block mn">
                                                    <input type="checkbox" name="mobileos" value="FR">
                                                    <span class="checkbox mn"></span>
                                                </label>
                                            </td>
                                            <td style="padding-left: 100px;">${cat3.title}</td>
                                            <td>${cat3.info}</td>
                                            <td>
                                                <a href="${pageContext.request.contextPath}/admin/Catalog/delete.do?id=${cat3.id}">删除</a>
                                            </td>
                                        </tr>
                                    </c:forEach>
                                </c:forEach>
                            </c:forEach>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </section>
</section>


<jsp:include page="bottom.jsp"/>


0

Wchisper

提问者

2019-07-27

top.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>欢迎使用 IMOOC Cake 后台管理系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="/css/theme.css">
    <link rel="stylesheet" type="text/css" href="/css/admin-forms.css">
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
</head>

<body class="admin-validation-page" data-spy="scroll" data-target="#nav-spy" data-offset="200">
<div id="main">
    <header class="navbar navbar-fixed-top navbar-shadow">
        <div class="navbar-branding">
            <a class="navbar-brand" href="../index.html">
                <H3>IMOOC Cake 后台管理系统</H3>
            </a>
            <span id="toggle_sidemenu_l" class="ad ad-lines"></span>
        </div>
    </header>
    <aside id="sidebar_left" class="nano nano-light affix">
        <div class="sidebar-left-content nano-content">
            <header class="sidebar-header">
                <div class="sidebar-widget author-widget">
                    <div class="media">
                        <a class="media-left" href="#">
                            <img src="${pageContext.request.contextPath}/images/head.jpg" class="img-responsive">
                        </a>
                        <div class="media-body">
                            <div class="media-author">管理员</div>
                            <div class="media-links">
                                <a href="login.html">退出</a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="sidebar-widget search-widget hidden">
                    <div class="input-group">
                        <span class="input-group-addon">
                        <i class="fa fa-search"></i>
                        </span>
                        <input type="text" id="sidebar-search" class="form-control" placeholder="Search...">
                    </div>
                </div>
            </header>
            <ul class="nav sidebar-menu">
                <li class="sidebar-label pt20">商品管理</li>
                <li class="active">
                    <a href="cake_list.html">
                        <span class="glyphicon glyphicon-book"></span>
                        <span class="sidebar-title">商品列表</span>
                    </a>
                </li>
                <li>
                    <a href="cake_add.html">
                        <span class="glyphicon glyphicon-home"></span>
                        <span class="sidebar-title">商品上架</span>
                    </a>
                </li>
                <li class="sidebar-label pt20">分类管理</li>
                <li class="active">
                    <a href="${pageContext.request.contextPath}/admin/Catalog/list.do">
                        <span class="glyphicon glyphicon-book"></span>
                        <span class="sidebar-title">分类列表</span>
                    </a>
                </li>
                <li>
                    <a href="${pageContext.request.contextPath}/admin/Catalog/toAdd.do">
                        <span class="glyphicon glyphicon-home"></span>
                        <span class="sidebar-title">添加分类</span>
                    </a>
                </li>
            </ul>
            <div class="sidebar-toggle-mini">
                <a href="login.html">
                    <span class="fa fa-sign-out"></span>
                </a>
            </div>
        </div>
    </aside>


butten.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


</div>
<style>
    /* demo page styles */
    body {
        min-height: 2300px;
    }

    .content-header b,
    .admin-form .panel.heading-border:before,
    .admin-form .panel .heading-border:before {
        transition: all 0.7s ease;
    }

    /* responsive demo styles */
    @media (max-width: 800px) {
        .admin-form .panel-body {
            padding: 18px 12px;
        }
    }
</style>

<style>
    .ui-datepicker select.ui-datepicker-month,
    .ui-datepicker select.ui-datepicker-year {
        width: 48%;
        margin-top: 0;
        margin-bottom: 0;

        line-height: 25px;
        text-indent: 3px;

        color: #888;
        border-color: #DDD;
        background-color: #FDFDFD;

        -webkit-appearance: none; /*Optionally disable dropdown arrow*/
    }
</style>

<!-- jQuery -->
<script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/js/jquery-ui.min.js"></script>
<!-- Theme Javascript -->
<script src="${pageContext.request.contextPath}/js/utility.js"></script>
<script src="${pageContext.request.contextPath}/js/demo/demo.js"></script>
<script src="${pageContext.request.contextPath}/js/main.js"></script>
<script src="${pageContext.request.contextPath}/js/pages.js"></script>
<!-- END: PAGE SCRIPTS -->
</body>
</html>


0

Wchisper

提问者

2019-07-27


http://img.mukewang.com/climg/5d3be7b10001865713510751.jpg

http://img.mukewang.com/climg/5d3be7b10001369f19201030.jpg

浏览器控制台报错了

0

好帮手慕阿满

2019-07-27

同学你好,问一下同学打印子分类是否有显示呢?如果有显示,那应该前台页面的显示问题,建议同学看一下浏览器控制台是否报错。如果没有,建议同学将前台的页面显示以及代码贴一下。

祝:学习愉快~

0
hchisper
h 打印出数据了 前台代码我会贴在我的回答里面
h019-07-27
共1条回复

好帮手慕阿满

2019-07-27

同学你好,打印出10000----0---null是正常的,pid是指该分类的父分类,info是指描述。id为10000是第一级分类,没有父分类,所以pid为0,info在数据库中存储的就是null,查询结果的是null。所以打印10000----0---null是正常的。或者同学可以打印id为10000的子分类,如:

http://img.mukewang.com/climg/5d3bb19f0001d03806370163.jpg

http://img.mukewang.com/climg/5d3bb1a8000108a805200116.jpg

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

0
hchisper
h 既然没有错 那我数据库的数据为什么,没有显示在前台
h019-07-27
共1条回复

0 学习 · 8016 问题

查看课程