404错误,请老师帮忙看看
catalogbiz
package com.andreas.cake.biz; import com.andreas.cake.entity.Catalog; import java.util.List; public interface CatalogBiz { void add(List<Catalog> list); void remove(int id); Catalog getRoot(); }
catalogbizimpl
package com.andreas.cake.biz.impl; import com.andreas.cake.biz.CatalogBiz; import com.andreas.cake.dao.CatalogDao; import com.andreas.cake.entity.Catalog; import com.andreas.cake.global.DaoFactory; import java.util.List; public class CatalogBizImpl implements CatalogBiz { //声明成员变量,catalogDao 其由dao工厂取出 private CatalogDao catalogDao = DaoFactory.getInstance().getDao(CatalogDao.class); public void add(List<Catalog> list) { catalogDao.BatchInsert(list); } public void remove(int id) { catalogDao.BatchDelete(id); } public Catalog getRoot() { return catalogDao.select(10000); //此处插入的10000是业务规则,已经在数据库内给定了最根节点的组件 } }
catalogController
package com.andreas.cake.controller; import com.andreas.cake.biz.CatalogBiz; import com.andreas.cake.biz.impl.CatalogBizImpl; import com.andreas.cake.entity.Catalog; import com.sun.deploy.net.HttpRequest; import com.sun.deploy.net.HttpResponse; import org.apache.ibatis.datasource.pooled.PooledDataSource; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class CatalogController { //声明业务处理对象 private CatalogBiz catalogBiz = new CatalogBizImpl(); //用户打开界面显示所有商品的方法list.do public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //由于分类是一个树状结构,拿到了根节点root就相当于拿到了所有节点 Catalog root = catalogBiz.getRoot(); request.setAttribute("root", root); request.getRequestDispatcher("/WEB-INF/pages/admin/catalog_list.jsp").forward(request, response); } //用户打开添加界面的方法toAdd.do public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Catalog root = catalogBiz.getRoot(); request.setAttribute("root", root); request.getRequestDispatcher("/WEB-INF/pages/admin/catalog_add.jsp").forward(request, response); } //用户在提交添加数据后执行的方法Add.do public void Add(HttpServletRequest request, HttpServletResponse response) throws IOException { String[] titles = request.getParameterValues("title"); String[] pids = request.getParameterValues("pid"); String[] infos = request.getParameterValues("info"); List<Catalog> catalogs = new ArrayList<Catalog>(); for (int i = 0; i <= titles.length; i++) { Catalog catalog = new Catalog(); catalog.setTitle(titles[i]); catalog.setInfo(infos[i]); catalog.setPid((Integer.parseInt(pids[i]))); catalogs.add(catalog); } catalogBiz.add(catalogs); response.sendRedirect("list.do");//同目录下可使用相对路径 } //用户删除商品的方法remove.do public void Remove(HttpServletRequest request, HttpServletResponse response) throws IOException { int id = Integer.parseInt(request.getParameter("id")); catalogBiz.remove(id); response.sendRedirect("list.do");//同目录下可使用相对路径 } }
catalogDao
package com.andreas.cake.dao; import com.andreas.cake.entity.Catalog; import com.sun.xml.internal.bind.v2.model.core.ID; import org.apache.ibatis.annotations.*; import java.util.List; public interface CatalogDao { //使用注解开发sql,动态sql可以使用插入脚本方式解决 @Insert("<script> " + "insert into catalog(title,pid,info) values" + "<foreach collection='list' item='catalog' separator=','>" + "(#{catalog.title},#{catalog.pid},#{catalog.info})" + "</foreach>" + "</script") //配置数据库自增属性 @Options(useGeneratedKeys = true, keyProperty = "id") //批量插入方法 void BatchInsert(List<Catalog> list); @Delete("delete from catalog where id = #{id}") //批量删除的方法 void BatchDelete(int id); //批量查询的方法 @Select("select * from catalog where id=#{id}") Catalog select(int id); @Results(id = "all",value = { @Result(column = "id", property = "id", id = true), @Result(column = "title", property = "title"), @Result(column = "pid",property = "pid"), @Result(column = "info",property = "info"), //将此分类下的所有子类查询出来交给此属性 @Result(column = "id",property = "children",many = @Many(select = "SelectByPid")) }) @Select("select * from catalog where pid=#{pid}") @ResultMap("all")//直接他用过id all 调用value List<Catalog> SelectByPid(int pid); }
Daofactory
package com.andreas.cake.global; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; public class DaoFactory { //工具类使用单例模式封装 private static DaoFactory daoFactory; SqlSessionFactory sqlSessionFactory; //构造方法私有 private DaoFactory(){ try { SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsReader("/mybatis.xml")); } catch (IOException e) { e.printStackTrace(); } } //提供公有的静态反回对象的方法 public static DaoFactory getInstance() { if (daoFactory == null) { daoFactory = new DaoFactory(); } return daoFactory; } public <T> T getDao(Class<T> tClass) { // SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); // SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsReader("/mybatis.xml")); //由于只需要执行一次,所以可以放在上面的单例构造方法中,只执行一次,提高代码效率 return sqlSessionFactory.openSession(true).getMapper(tClass);//自动提交事物 } }
encodingfilter
package com.andreas.cake.global; import com.sun.deploy.net.HttpRequest; import com.sun.deploy.net.HttpResponse; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; public class EncodingFileter implements Filter { private String encoding = "UTF-8"; public void init(FilterConfig filterConfig) throws ServletException { //可以通过filterConfig获取初始化参数来指定编码 if (filterConfig.getInitParameter("encoding") != null) { encoding = filterConfig.getInitParameter("encoding"); } } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) servletResponse; HttpServletRequest request = (HttpServletRequest) servletRequest; request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); filterChain.doFilter(request,response); } public void destroy() { } }
golbalController
package com.andreas.cake.global; import javafx.scene.shape.Path; import org.apache.ibatis.javassist.bytecode.stackmap.BasicBlock; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class GlobalController extends GenericServlet { //请求处理方法,可以根据用户请求的url可以分辨出由哪个类的哪个方法来处理,然后再调用其方法 public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; //单用户发送请求时候获取请求路径 String path = request.getServletPath(); //判断前后台请求 if (path.indexOf("admin") != -1) { path = path.substring(7); } else { path = path.substring(1); } int index = path.indexOf("/"); //通过请求路径获取到一个类名一个方法名 String className = null; String methodName = null; if (index != -1) { className = "com.andreas.cake.controller" + path.substring(0, index) + "Controller"; methodName = path.substring(index + 1, path.indexOf(".do")); } else { className = "com.andreas.cake.controller.DefaultController"; methodName = path.substring(0, path.indexOf(".do")); } try { //通过反射执行相应的方法 Class cla =Class.forName(className); Object obj = cla.newInstance();//获取对象 Method method = cla.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class); //执行方法,(调用次方的的对象,调用方法的参数) method.invoke(obj, 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(); } } }
mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.4//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- mybatis的环境标签,mybayis可以支持多个数据源--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/music?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <!-- 当前项目使用注解开发,自动扫描包中的接口设置成映射器接口--> <package name="com.andreas.cake.dao"/> </mappers> </configuration>
cataloglist.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%-- Created by IntelliJ IDEA. User: Andreas Byrsting Date: 2020/9/3 Time: 14:42 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <jsp:include page="top.jsp"></jsp:include> <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="/admin/Catalog/remove.do?=${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="/admin/Catalog/remove.do?=${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: 50px;">${cat3.title}</td> <td>${cat3.info}</td> <td> <a href="/admin/Catalog/remove.do?=${cat3.id}">删除</a> </td> </tr> </c:forEach> </c:forEach> </c:forEach> </tbody> </table> </div> </div> </div> </div> </section> </section> <jsp:include page="bottom.jsp"></jsp:include>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <filter> <filter-name>encoding</filter-name> <filter-class>com.andreas.cake.global.EncodingFileter</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.andreas.cake.global.GlobalController</servlet-class> </servlet> <servlet-mapping> <servlet-name>GlobalServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
32
收起
正在回答
3回答
同学你好,
1、CatalogDao接口中script尾标签少了>,如下:
2、select()方法写错位置了,参考代码如下:
祝学习愉快~
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星