查看详情,删除,和 编辑之后保存 返回空白页面

查看详情,删除,和 编辑之后保存 返回空白页面

具体遇到的问题

添加分类没有问题 只是修改保存和查看详情和删除后台报错 浏览器返回空白页

报错信息的截图

java.lang.NoSuchMethodException: com.imooc.icake.controller.CakeController.detail(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
at java.lang.Class.getMethod(Class.java:1786)
at com.imooc.icake.global.GlobalController.service(GlobalController.java:64)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.imooc.icake.global.EncodingFilter.doFilter(EncodingFilter.java:41)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocolConnectionHandler.process(AbstractProtocol.java:868)atorg.apache.tomcat.util.net.NioEndpointConnectionHandler.process(AbstractProtocol.java:868) at org.apache.tomcat.util.net.NioEndpointConnectionHandler.process(AbstractProtocol.java:868)atorg.apache.tomcat.util.net.NioEndpointSocketProcessor.doRun(NioEndpoint.java:1589)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutorWorker.run(ThreadPoolExecutor.java:624)atorg.apache.tomcat.util.threads.TaskThreadWorker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThreadWorker.run(ThreadPoolExecutor.java:624)atorg.apache.tomcat.util.threads.TaskThreadWrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

相关课程内容截图

尝试过的解决思路和结果

粘贴全部相关代码,切记添加代码注释(请勿截图)

package com.imooc.icake.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.icake.biz.CakeBiz;
import com.imooc.icake.biz.CatalogBiz;
import com.imooc.icake.biz.impl.CakeBizImpl;
import com.imooc.icake.biz.impl.CatalogBizImpl;
import com.imooc.icake.entiy.Cake;
import com.imooc.icake.entiy.Catalog;
import com.sun.deploy.net.HttpResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

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

public class CakeController {
    private CakeBiz cakeBiz = new CakeBizImpl();
    private CatalogBiz catalogBiz = new CatalogBizImpl();
    //  /admin/Cake/list.do
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置分页
        String pageNum = request.getParameter("pageNum");
        if(pageNum == null){
            pageNum = "1";
        }
        PageHelper.startPage(Integer.parseInt(pageNum),20);
        List<Cake> list = cakeBiz.getRootAll();
        PageInfo pageInfo = PageInfo.of(list);

        request.setAttribute("pageInfo",pageInfo);
        request.getRequestDispatcher("/WEB-INF/pages/admin/cake_list.jsp").forward(request, response);
    }

    //  /admin/Cake/toAdd.do 跳转添加页面
    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Catalog root = catalogBiz.getRoot();

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

    }
    //  /admin/Cake/add.do  添加页面
    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException {
        Cake cake = builderCake(request);
        cakeBiz.add(cake);
        response.sendRedirect("list.do");
    }

    //  /admin/Cake/eoEdit.do 跳转修改页面
    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int id = Integer.parseInt(request.getParameter("id"));
        Cake cake = cakeBiz.root(id);
        Catalog root = catalogBiz.getRoot();
        request.setAttribute("cake",cake);
        request.setAttribute("root",root);
        request.getRequestDispatcher("/WEB-INF/pages/admin/cake_edit.jsp").forward(request, response);

    }
    //  /admin/Cake/edit.do 修改
    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException {
        Cake cake = builderCake(request);
        cakeBiz.update(cake);
        response.sendRedirect("list.do");
    }
    private Cake builderCake(HttpServletRequest request) throws FileUploadException, UnsupportedEncodingException {
        Cake cake = new Cake();
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> list = upload.parseRequest(request);
        for (FileItem item: list) {
            if (item.isFormField()) {
                if(item.getFieldName().equals("title")){
                    cake.setTitle(item.getString("UTF-8"));
                }
                if(item.getFieldName().equals("cid")){
                    cake.setCid(Integer.parseInt(item.getString("UTF-8")));
                }
                if(item.getFieldName().equals("price")){
                    cake.setPrice(Double.parseDouble(item.getString("UTF-8")));
                }
                if(item.getFieldName().equals("taste")){
                    cake.setTaste(item.getString("UTF-8"));
                }
                if(item.getFieldName().equals("sweetness")){
                    cake.setSweetness(Integer.parseInt(item.getString("UTF-8")));
                }
                if(item.getFieldName().equals("weight")){
                    cake.setWeight(Integer.parseInt(item.getString("UTF-8")));
                }
                if(item.getFieldName().equals("material")){
                    cake.setMaterial(item.getString("UTF-8"));
                }
                if(item.getFieldName().equals("status")){
                    cake.setStatus(item.getString("UTF-8"));
                }
                if(item.getFieldName().equals("size")){
                    cake.setSize(Integer.parseInt(item.getString("UTF-8")));
                }
                if(item.getFieldName().equals("id")){
                    cake.setId(Integer.parseInt(item.getString("UTF-8")));
                }
                if(item.getFieldName().equals("imagePath") && cake.getImagePath() == null){
                    cake.setImagePath(item.getString("UTF-8"));
                }
            } else {
                if (item.getFieldName().equals("image")){
                    if(item.getSize() <= 100){
                        continue;
                    }
                    String rootPath = request.getServletContext().getRealPath("/");
                    String path = item.getName();
                    String type = ".jpg";
                    if(path.indexOf(".") != -1){
                        type = path.substring(path.lastIndexOf("."));
                    }
                    path = "/download/images/"+System.currentTimeMillis()+type;
                    try {
                        item.write(new File(rootPath+path));
                        cake.setImagePath(path);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        }
        return cake;
    }
    //  /admin/Cake/remove.do 删除
    public void remove(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        cakeBiz.remove(id);
        response.sendRedirect("liat.do");
    }
    //  /admin/Cake/detail.do 查看详情
    public void detail(HttpServletResponse response,HttpServletRequest request) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Cake cake = cakeBiz.root(id);
        request.setAttribute("cake",cake);
        request.getRequestDispatcher("/WEB-INF/pages/admin/cake_detail.jsp").forward(request,response);
    }
}



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 {
        /*
        * .do 规定请求的结尾以.do结尾
        * http://localhost:8080/icake_war_exploded/admin/Catalog/list.do
        * /login.do             DefaultController类 login方法
        * /Cake/detail.do       CakeController类  detail方法
        * /admin/Cake/add.do    CakeController类    add方法
        * */
        //获取HttpServletRequest和HttpServletResponse对象
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        //获取请求的路径
        String path = request.getServletPath();
        System.out.println(path);
        //判断路径中是否包括/admin 如果包括则从小标7开始截取
        if (path.indexOf("/admin") != -1) {
            path = path.substring(7);
        } else {
            //不包含则去掉/
            path = path.substring(1);
        }
        System.out.println("2---" + path);
        int index = path.indexOf("/");
        String className = null;//类名
        //方法名
        String methodName = null;
        if (index != -1){

            //如果path中还有/则拼接类名 获取方法名
            className = "com.imooc.icake.controller." + path.substring(0,index) + "Controller";
            methodName = path.substring(index + 1, path.indexOf(".do"));

        }else{
            //如果不包含则默认类名
            className ="com.imooc.icake.controller.DefaultController";
            methodName = path.substring(0,path.indexOf(".do"));
        }
        try {
            System.out.println("className--" + className);
            System.out.println("methodName--" + methodName);
            //利用反射获取类名 并调用方法
            Class cla = Class.forName(className);
            System.out.println("cla-->" + cla);
            Object object = cla.newInstance();
            System.out.println("object===>" + object);
            Method  method = cla.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();
        }

    }
}


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

<%@ 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='${pageContext.request.contextPath}/admin/Cake/toAdd.do';"></i>
                                    </button>
                                </div>
                            </div>
                            <div class="col-xs-12 col-md-9 text-right">
                                <form action="${pageContext.request.contextPath}/admin/Cake/list.do" method="post">
                                    共${pageInfo.total}条 ${pageInfo.pageNum}/${pageInfo.pages}
                                    <div class="btn-group">
                                        <button type="submit" name="pageNum" value="${pageInfo.pageNum - 1}" class="btn btn-default light">
                                            <i class="fa fa-chevron-left"></i>
                                        </button>
                                        <button type="submit" name="pageNum" value="${pageInfo.pageNum + 1}" class="btn btn-default light">
                                            <i class="fa fa-chevron-right"></i>
                                        </button>
                                    </div>
                                </form>
                            </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 class="hidden-xs">价格</th>
                                <th class="hidden-xs">口味</th>
                                <th class="hidden-xs">尺寸</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:forEach items="${pageInfo.list}" var="cake">
                            <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>${cake.title}</td>
                                <td>${cake.catalog.title}</td>
                                <td>${cake.price}</td>
                                <td>${cake.taste}</td>
                                <td>${cake.size}</td>
                                <td>
                                    <a href="${pageContext.request.contextPath}/admin/Cake/detail.do?id=${cake.id}">查看</a>
                                    <a href="${pageContext.request.contextPath}/admin/Cake/toEdit.do?id=${cake.id}">编辑</a>
                                    <a href="${pageContext.request.contextPath}/admin/Cake/remove.do?id=${cake.id}">删除</a>
                                </td>
                            </tr>
                            </c:forEach>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </section>
</section>

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




正在回答

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

2回答

同学你好,1. NumberFormatException异常是数字格式转换异常,当试图将一个String转换为指定的数字类型,而该字符串确不满足数字类型要求的格式时,抛出这个异常。同学可根据错误信息反馈的行数,查看代码进行调试。也有可能是因为参数传递有误造成的,同学可以检查一下自己是不是参数传递有误

2. 如果还存在问题,则建议同学反馈全部错误信息,便于老师定位问题。

好帮手慕小尤 2020-10-25 12:00:14

同学你好,1. detail()方法的参数存在一点问题,建议同学调整参数,如下所示:然后清除缓存,重新进行测试。

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

public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}

2. 如果还存在问题,则建议同学反馈CakeBizImpl类中的方法,便于老师定位问题。

  • 提问者 慕粉1705037409 #1
    Caused by: java.lang.NumberFormatException: For input string: "22.0" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615)
    2020-10-25 21:45:49
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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