老师,跳转出现空白页面。

老师,跳转出现空白页面。

目录结构:

http://img1.sycdn.imooc.com//climg/605cb3e3093b980006961101.jpghttp://img1.sycdn.imooc.com//climg/605cb4c409fb083206651103.jpghttp://img1.sycdn.imooc.com//climg/605cb4e809f2cdbb07011215.jpg

配置图片:

http://img1.sycdn.imooc.com//climg/605cb7650968514423591405.jpg

注:那个URL配置我是看老师给其他同学解答的说Catalog是controller中的类,所以改成这样的。同时我也试过老师视频配置的那样依旧不可以。

http://img1.sycdn.imooc.com//climg/605cb806099bd6bb16520409.jpg

http://img1.sycdn.imooc.com//climg/605cb825097d26b122421436.jpg

最后运行后的展示图片:

http://img1.sycdn.imooc.com//climg/605cba9f09e576a724720692.jpghttp://img1.sycdn.imooc.com//climg/605cbad709d1b5b124431135.jpg

注:已经尝试过打断点的方式,并没有报错。

这是所有运行代码:

biz下:

CatelogBiz类:

package com.imooc.icake.biz;


import com.imooc.icake.entity.Catelog;


import java.util.List;


public interface CatelogBiz {

    void insert(List<Catelog> list);


    void remove(int id);

//获取分类

    Catelog getRoot();

}

CatelogBizImpl类:

package com.imooc.icake.biz.impl;


import com.imooc.icake.biz.CatelogBiz;

import com.imooc.icake.dao.CatelogDao;

import com.imooc.icake.entity.Catelog;

import com.imooc.icake.global.DaoFactory;


import java.util.List;


public class CatelogBizImpl implements CatelogBiz {

    //调用catelogDao

    private CatelogDao catelogDao = DaoFactory.getInstance().getDao(CatelogDao.class);


    @Override

    public void insert(List<Catelog> list) {

        catelogDao.batchInsert(list);

    }


    @Override

    public void remove(int id) {

catelogDao.delete(id);

    }


    @Override

    public Catelog getRoot() {

        return catelogDao.selectall(10000);

    }

}

controller中:

CatelogController类:

package com.imooc.icake.controller;


import com.imooc.icake.biz.CatelogBiz;

import com.imooc.icake.biz.impl.CatelogBizImpl;

import com.imooc.icake.entity.Catelog;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.awt.*;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;


//处理提交事务的控制器,与前端相联系

public class CatelogController {

    //查看分类 /admin/catelog_list.do

    //需要业务层,专门用于添加删除

    CatelogBiz biz = new CatelogBizImpl();

    public void list(HttpServletRequest request, HttpServletResponse response) {

        //获取Root节点

        Catelog root = biz.getRoot();

        //给request赋值

        request.setAttribute("root",root);

        //请求转发

        //转发路径/WEB-INF/pages/admin/catelog_list.do

        try {

            request.getRequestDispatcher("/WEB-INF/pages/admin/catalog_list.jsp").forward(request,response);

        } catch (ServletException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }


    //跳转到添加页面/admin/add.do

    public void Add(HttpServletRequest request, HttpServletResponse response) {

        //获取Root节点

        Catelog root = biz.getRoot();

        //给request赋值

        request.setAttribute("root",root);

        //请求转发

        //转发路径/WEB-INF/pages/admin/catelog_list.do

        try {

            request.getRequestDispatcher("/WEB-INF/pages/admin/catalog_add.jsp").forward(request,response);

        } catch (ServletException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    //进行批量添加数据/admin/add_to.do

    public void  AddTo(HttpServletRequest request, HttpServletResponse response) throws IOException {

//将页面所得的数据进行交付给业务层去处理

        //所得到的数据是个集合

        String[] titles = request.getParameterValues("title");

        String[] pids = request.getParameterValues("pid");

        String[] infos = request.getParameterValues("info");

        List<Catelog> catelogList = new ArrayList<Catelog>();

        //使用for循环将数据打包为对象,放在list集合中

        for (int i=0; i < titles.length; i++) {

            Catelog catelog = new Catelog();

            catelog.setTitle(titles[i]);

            catelog.setPid(Integer.parseInt(pids[i]));

            catelog.setInfo(infos[i]);

            catelogList.add(catelog);

        }

        //调用业务层进行添加

        biz.insert(catelogList);

        response.sendRedirect("list.do");

    }


    //删除数据/admin/remove.do

    public void remove1(HttpServletRequest request, HttpServletResponse response) throws IOException {

//删除数据需要获得它的id

        int id =Integer.parseInt( request.getParameter("id"));

        biz.remove(id);

        response.sendRedirect("list.do");

    }

}

dao中:

CatelogDao类:

package com.imooc.icake.dao;


import com.imooc.icake.entity.Catelog;

import org.apache.ibatis.annotations.*;


import java.util.List;


public interface CatelogDao {

    //创建添加数据的方法

    @Insert("<script> insert into catelog(title,pid,info) values" +

            "<foreach collection='list' item='catelog' separator=‘,’>" +

            "(#{catelog.title},#{catelog.id},#{catelog.info})"+

            "</foreach>"+

            "</script>")

    //得到逐渐自增的注解

    @Options(useGeneratedKeys = true,keyProperty = "id")

    void batchInsert(List<Catelog> list);


    //删除cate类

    @Delete("delete from catelog where id={#id}")

    void delete(int id) ;


    //查询cate类

    @Select("select * from catelog where id=#{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"),

            //在获取子类的时候column为多的一方的主键,many的是从哪里来

            @Result(column = "id",property = "children",many = @Many(select = "selectBypid"))

    })

    Catelog selectall(int id);


//获取父类下面所有子类

    @Select("select * from catelog where pid=#{pid}")

    @ResultMap("all")

    List<Catelog> selectBypid(int pid);

}

entity中:

Catelog类:

package com.imooc.icake.entity;


import java.util.ArrayList;

import java.util.List;


public class Catelog {

    private int id;

    private String title;

    private int pid;

    private String info;

    //创建一个属于catelog类的子类

    private List<Catelog> children = new ArrayList<Catelog>();


    public List<Catelog> getChildren() {

        return children;

    }


    public void setChildren(List<Catelog> children) {

        this.children = children;

    }


    public int getId() {

        return id;

    }


    public void setId(int id) {

        this.id = id;

    }


    public String getTitle() {

        return title;

    }


    public void setTitle(String title) {

        this.title = title;

    }


    public int getPid() {

        return pid;

    }


    public void setPid(int pid) {

        this.pid = pid;

    }


    public String getInfo() {

        return info;

    }


    public void setInfo(String info) {

        this.info = info;

    }

}

global中:

DaoFactory类

package com.imooc.icake.global;


import com.mysql.cj.xdevapi.SqlResultBuilder;

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import java.io.IOException;

import java.io.Reader;



public class DaoFactory {

    //1.创建静态私有的DaoFactory对象

    private static DaoFactory daoFactory;

    //2.创建私有构造方法

    private SqlSessionFactory sqlSessionFactory;

    Reader reader=null;

    private DaoFactory() {

        //1、获取工厂类2、创建Build方法

        SqlSessionFactoryBuilder sqlResultBuilder = new SqlSessionFactoryBuilder();

        try {

            reader = Resources.getResourceAsReader("mybatis.xml");

        } catch (IOException e) {

            e.printStackTrace();

        }

        sqlSessionFactory = sqlResultBuilder.build(reader);


    }

    //3.共有静态方法

    public static DaoFactory getInstance() {

        if (daoFactory == null) {

            daoFactory = new DaoFactory();

        }

        return daoFactory;

    }


    //执行的方法和类不唯一,使用泛型

    public <T> T getDao(Class<T> tclass) {

         return sqlSessionFactory.openSession(true).getMapper(tclass);

    }


}

EncodingFilter类:

package com.imooc.icake.global;


import javax.servlet.*;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;


public class EncodingFilter implements Filter {

    private String encoding = "UTF-8";

    @Override

    public void init(FilterConfig filterConfig) throws ServletException {

        if (filterConfig.getInitParameter("encoding") != null) {

            encoding = filterConfig.getInitParameter("encoding");


        }

    }


    @Override

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;

        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

        httpServletRequest.setCharacterEncoding(encoding);

        httpServletResponse.setCharacterEncoding(encoding);

        filterChain.doFilter(httpServletRequest, httpServletResponse);

    }


    @Override

    public void destroy() {


    }

}

GlobalController类:


package com.imooc.icake.global;


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 {


    @Override

    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

        //获得路径1、强制转换 2、获取路径

        HttpServletRequest request = (HttpServletRequest) servletRequest;

        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String path = request.getServletPath();

        /*

        cake/add.do

        login.do

        cakecontrller

         */

        //判断是否为前台数据

        if (path.indexOf("/admin") != -1) {

            path.indexOf(7);

        } else {

            path.indexOf(1);

        }

        //判断是否为有cake模块1、获取index

        int index = path.indexOf("/");

        String className = null;

        String methodName = null;

        if (index != -1) {

            //获得类名的同时获取包名

             className = "com.imooc.icake.controller" + path.substring(0, index) + "Contrller";

             methodName = path.substring(index + 1, path.indexOf(".do"));


        } else {

             className = "com.imooc.icake.controller.DefaultContrller";

            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>

    <environments default="dev">

        <!--配置环境,不同的环境不同的id-->

        <environment id="dev">

            <!--采用jdbc方式对数据库事务进行处理-->

            <transactionManager type="JDBC"></transactionManager>

            <!--采用连接池的方式管理数据库连接-->

                <dataSource type="POOLED">

                    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>

                    <property name="url" value="jdbc:mysql://localhost:3306/icake?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Hongkong"/>

                    <property name="username" value="root"/>

                    <property name="password" value="root"/>

                </dataSource>

            </environment>


    </environments>

    <mappers>

        <package name="com.imooc.icake.dao"/>

    </mappers>

</configuration>

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.imooc.icake.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.imooc.icake.global.GlobalController</servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name>GlobalServlet</servlet-name>

        <url-pattern>*.do</url-pattern>

    </servlet-mapping>

</web-app>


pom.xml中:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>


    <groupId>org.example</groupId>

    <artifactId>icake</artifactId>

    <version>1.0-SNAPSHOT</version>


    <properties>

        <maven.compiler.source>8</maven.compiler.source>

        <maven.compiler.target>8</maven.compiler.target>

    </properties>

<packaging>war</packaging>

    <dependencies>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>8.0.19</version>

        </dependency>

        <dependency>

            <groupId>org.mybatis</groupId>

            <artifactId>mybatis</artifactId>

            <version>3.5.1</version>

        </dependency>

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>servlet-api</artifactId>

            <version>2.5</version>

        </dependency>

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>jstl</artifactId>

            <version>1.2</version>

        </dependency>


    </dependencies>

</project>

希望老师能帮我做出解答,谢谢!

http://img1.sycdn.imooc.com//climg/605cb3e3093b980006961101.jpg

正在回答

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

2回答

同学你好,

1、同学打开项目有误,应该选择icake,如下:

http://img1.sycdn.imooc.com//climg/605ee30f094871e002870139.jpg

选择源码中的icake,点击ok,如下:

http://img1.sycdn.imooc.com//climg/605ee442099e06de05970502.jpg

打开项目后,配置自己的tomcat,再启动访问试试。

2、根据输出信息:

/admin/Catelog/list.do     calssName=com.imooc.icake.controller.Controller     methodName=admin/Catelog/list

java.lang.ClassNotFoundException: com.imooc.icake.controller.Controller

可知,className值有误,同学代码中没有com.imooc.icake.controller.Controller类,所以程序会报错。

建议同学运行课程源码试试。

祝学习愉快~

  • 复java_n 提问者 #1

    http://img1.sycdn.imooc.com//climg/605eeb8109e907e624171145.jpg

    课程源码出现错误

    2021-03-27 16:24:02
  • 复java_n 提问者 #2

    哦哦,mybatis中没有修改参数

    2021-03-27 16:27:21
好帮手慕阿慧 2021-03-26 11:54:30

同学你好,

1、出现空白页面是因为后台报错了,在GlobalController类中,解析请求路径和拼写className中有错误。

参考代码如下:

http://img1.sycdn.imooc.com//climg/605d58dd0996f5ac08450696.jpg

2、在CatelogDao接口中,separator中单引号应该是英文单引号。

如下:

​"<foreach collection='list' item='catelog' separator=','>" +

http://img1.sycdn.imooc.com//climg/605d5abc09b7e56a06620173.jpg

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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