请老师帮忙看一下,点击编辑id传不过去

请老师帮忙看一下,点击编辑id传不过去

java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at com.imooc.canvas.global.GlobalController.service(GlobalController.java:57)

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:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at com.imooc.canvas.global.EncodingFilter.doFilter(EncodingFilter.java:24)

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:198)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:748)

Caused by: java.lang.NumberFormatException: For input string: ""

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

at java.lang.Integer.parseInt(Integer.java:592)

at java.lang.Integer.parseInt(Integer.java:615)

at com.imooc.canvas.controller.CanvasController.builder(CanvasController.java:78)

at com.imooc.canvas.controller.CanvasController.edit(CanvasController.java:62)

... 30 more

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

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<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="#" >分类一</a>
        </nav>
        <nav>
            <a href="#" >分类</a>
        </nav>
        <nav>
            <a href="#">登录</a>
            <a href="#" onclick="alert('功能暂未开放');">注册</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>
                <th>最后修改时间</th>
                <th>描述</th>
                <th>编辑</th>
                <th>删除</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${list}" var="canvas">
                <tr>
                    <td>${canvas.name}</td>
                    <td>${canvas.category.name}</td>
                    <td>¥<fmt:formatNumber type="currency" pattern="#,#00.00#" value="${canvas.price}"/></td>
                    <td><fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss" value="${canvas.createTime}"/></td>
                    <td><fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss" value="${canvas.updateTime}"/></td>
                    <td>${canvas.description}</td>
                    <td><a href="/admin/Canvas/toEdit.do?id=${canvas.id}">编辑</a></td>
                    <td><a href="#">删除</a>
                </tr>
            </c:forEach>

            </tbody>
        </table>
    </div>
</section>
<section class="page">
    <div class="container">
        <div id="fatie">
            <a href="/admin/Canvas/toAdd.do"><button>新建</button></a>
        </div>
    </div>
</section>
<footer>
    copy@慕课网
</footer>
</body>
</html>
package com.imooc.canvas.controller;

import com.imooc.canvas.biz.CanvasBiz;
import com.imooc.canvas.biz.CategoryBiz;
import com.imooc.canvas.biz.Impl.CanvasBizImpl;
import com.imooc.canvas.biz.Impl.CategoryBizImpl;
import com.imooc.canvas.entity.Canvas;
import com.imooc.canvas.entity.Category;
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.Date;
import java.util.List;

public class CanvasController {
    private CanvasBiz canvasBiz = new CanvasBizImpl();
    private CategoryBiz categoryBiz = new CategoryBizImpl();

    //    /admin/Canvas/list.do
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Canvas> list = canvasBiz.getAll();
        request.setAttribute("list",list);
        request.getRequestDispatcher("/WEB-INF/pages/admin/canvas_list.jsp").forward(request,response);
    }

    //    /admin/Canvas/toAdd.do
    public void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        List<Category> list = categoryBiz.getRoot();
        request.setAttribute("list",list);
        request.getRequestDispatcher("/WEB-INF/pages/admin/canvas_add.jsp").forward(request,response);
    }

    //    /admin/Canvas/add.do
    public void add(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Canvas canvas = builder(request);
        canvasBiz.add(canvas);
        response.sendRedirect("list.do");
    }

    //     /admin/Canvas/toEdit.do
    public void toEdit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Canvas canvas = canvasBiz.get(id);
        request.setAttribute("canvas",canvas);
        List<Category> list = categoryBiz.getRoot();
        request.setAttribute("list",list);
        request.getRequestDispatcher("/WEB-INF/pages/admin/canvas_edit.jsp").forward(request,response);
    }

    //       /admin/Canvas/edit.do
    public void edit(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Canvas canvas = builder(request);
        canvasBiz.edit(canvas);
        response.sendRedirect("list.do");
    }

    private Canvas builder(HttpServletRequest request) throws FileUploadException, UnsupportedEncodingException {
        Canvas canvas = new Canvas();
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("UTF-8");
        List<FileItem> list = upload.parseRequest(request);
        for (FileItem item:list){
            //判断是表单项还是文件上传项
            if(item.isFormField()){
                //表单项
                if (item.getFieldName().equals("id")){
                    canvas.setId(Integer.parseInt(item.getString("UTF-8")));
                }
                if (item.getFieldName().equals("name")){
                    canvas.setName(item.getString("UTF-8"));
                }
                if (item.getFieldName().equals("categoryId")){
                    canvas.setCategoryId(Integer.parseInt(item.getString("UTF-8")));
                }
                if(item.getFieldName().equals("price")){
                    canvas.setPrice(Integer.parseInt(item.getString("UTF-8")));
                }
                if (item.getFieldName().equals("imagePath")&&canvas.getImagePath()==null){
                    canvas.setImagePath(item.getString("UTF-8"));
                }
                if (item.getFieldName().equals("description")){
                    canvas.setDescription(item.getString("UTF-8"));
                }
            }else {
                //文件上传项
                if (item.getFieldName().equals("imagePath")){
                    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();
                    try {
                        item.write(new File(rootPath+path));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    canvas.setImagePath(path);
                }
            }
        }
        return canvas;
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/add.css">
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container">
        <div class="navbar-header">
            <a class="navbar-brand" href="#">
                慕课网油画管理
            </a>
        </div>
    </div>
</nav>
<div class="container">
    <div class="jumbotron">
        <h1>Hello, admin!</h1>
        <p>请小心的编辑油画记录</p>
    </div>
    <div class="page-header">
        <h3><small>编辑</small></h3>
    </div>
    <form class="form-horizontal" action="/admin/Canvas/edit.do" method="post" enctype="multipart/form-data">
        <input type="hidden" name="id" value="${canvas.id}">
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">名称 :</label>
            <div class="col-sm-8">
                <input name="name" class="form-control" id="name" value="${canvas.name}">

            </div>
        </div>
        <div class="form-group">
            <label for="categoryId" class="col-sm-2 control-label">分类 :</label>
            <select id="categoryId" name="categoryId" class="col-sm-2 form-control" style="width: auto">
                <c:forEach items="${list}" var="cat">
                    <option id="001" value="${cat.id}">${cat.name}</option>
                </c:forEach>

            </select>
        </div>
        <div class="form-group">
            <label for="price" class="col-sm-2 control-label">价格 :</label>
            <div class="col-sm-8">
                <input name="price" type="number" class="form-control" id="price" value="${canvas.price}">
            </div>
        </div>
        <div class="form-group">
            <label for="smallImg" class="col-sm-2 control-label">图片 :</label>
            <div class="col-sm-8">
                <input id="smallImg" name="imagePath" class="file-loading"
                       type="file" multiple accept=".jpg,.jpeg,.png" data-min-file-count="1"
                       data-show-preview="true" value="${canvas.imagePath}">
            </div>
        </div>
        <div class="form-group">
            <label for="description" class="col-sm-2 control-label">描述 :</label>
            <div class="col-sm-8">
                <input name="description" type="text" class="form-control" id="description" value="${canvas.description}">
            </div>
        </div>


        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-primary">保存</button>&nbsp;&nbsp;&nbsp;
            </div>
        </div>
    </form>
</div>
<footer class="text-center" >
    copy@imooc
</footer>
</body>
</html>


正在回答

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

4回答

你好!从你贴出的图来看,代码出问题,还是因为id为0的canvas没有查询到。

在list方法中,将list集合的内容打印输出,看一下id值是什么

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

或者尝试在jsp中写Java脚本,也就是<%  %>在此处获取list的值并遍历,查看一下id的值是多少,是否为0。

不知道你的Canvas类中id的类型是什么,int,Integer还是String。

如果还是有问题不好排查,建议把作业交上来整体看一下吧,在作业中可以说明一下这个问题没有解决。

祝学习愉快!

  • 精慕门6573819 提问者 #1
    解决了,SQL语句写的有问题
    2019-01-02 21:23:39
提问者 精慕门6573819 2019-01-02 13:56:37
好帮手慕珊 2019-01-02 13:30:20

你好!id为0的canvas存在吗?还是这里的id本身就有问题?你把鼠标放到编辑上,看一下浏览器左下角的链接是什么,id和当前所在行的id一致吗?

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


另外,点击编辑后,表单中的这些值是应该被查询出来并填充好的,从效果图看并没有填充好,所以说明该canvas的信息没有查询到

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

再有,通过报错信息查看一下,具体是代码的哪一行报错了

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


祝学习愉快!

  • 提问者 精慕门6573819 #1
    id为0的canvas不存在,鼠标放到编辑上左下角id也都是0,与当前行不一致,报错的代码已贴出
    2019-01-02 13:58:42
好帮手慕珊 2019-01-02 11:57:57

你好!是点击编辑调整到修改页面的时候报错的吗?

从截图看,地址中是有id的值的,建议先在toEdit方法中将request.getParameter("id")的值输出看看是什么,确认一下是否得到了id的值。

祝学习愉快!

  • 提问者 精慕门6573819 #1
    是在编辑页面填好内容点击修改后报错,不管点哪一条编辑id传的都是0
    2019-01-02 12:28:02
  • 提问者 精慕门6573819 #2
    toEdit中打印id没有输出
    2019-01-02 12:30:16
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
Java数据库开发与实战应用2018版
  • 参与学习           人
  • 提交作业       277    份
  • 解答问题       4297    个

Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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