关于修改商品的疑问

关于修改商品的疑问

          <form id="admin-form" name="addForm" action="${pageContext.request.contextPath}/ProductServlet?method=update" method="post" enctype="multipart/form-data">
              <input type="hidden" name="pid" value="${product.pid}">
              <input type="hidden" name="filename" value="${product.filename}">
              <input type="hidden" name="path" value="${product.path}">
              <div class="panel-body bg-light">
                  <div class="section-divider mt20 mb40">
                      <span> 基本信息 </span>
                  </div>
                  <div class="section row">
   <div class="col-md-2"></div>
   <div class="col-md-1">
                          <label for="sn" class="field prepend-icon">
                              <label for="sn" class="field-icon">
            名称
                              </label>
                          </label>
                      </div>
                      <div class="col-md-6">
                          <label for="sn" class="field">
                              <input  name="pname" class="gui-input" placeholder="名称" type="text" value="${product.pname}"/>
                          </label>
                      </div>
                  </div>
<div class="section row">
   <div class="col-md-2"></div>
   <div class="col-md-1">
                          <label for="sn" class="field prepend-icon">
                              <label for="sn" class="field-icon">
            价格
                              </label>
                          </label>
                      </div>
                      <div class="col-md-6">
                          <label for="sn" class="field">
                              <input  name="price" class="gui-input" placeholder="价格" type="text" value="${product.price}"/>
                              
                          </label>
                      </div>
                  </div>
                  <div class="section row">
                      <div class="col-md-2"></div>
                      <div class="col-md-1">
                          <label for="sn" class="field prepend-icon">
                              <label for="sn" class="field-icon">
                                  作者
                              </label>
                          </label>
                      </div>
                      <div class="col-md-6">
                          <label for="sn" class="field">
                              <input  name="author" class="gui-input" placeholder="作者" type="text" value="${product.author}"/>

                          </label>
                      </div>
                  </div>
                  <div class="section row">
   <div class="col-md-2"></div>
   <div class="col-md-1">
                          <label for="sn" class="field prepend-icon">
                              <label for="sn" class="field-icon">
            分类
                              </label>
                          </label>
                      </div>
                      <div class="col-md-6">
                          <label for="sn" class="field select">
                              <select id="departmentSn" name="cid" class="gui-input" placeholder="分类...">
                                  <c:forEach var="cl" items="${categoryList}">
                                      <c:choose>
                                          <c:when test="${cl.cid==product.category.cid}">
                                              <option value="${product.category.cid}" selected="selected">${product.category.cname}</option>
                                          </c:when>
                                          <c:otherwise>
                                              <option value="${cl.cid}">${cl.cname}</option>
                                          </c:otherwise>
                                      </c:choose>
                                  </c:forEach>
                              </select>
                              <i class="arrow double"></i>
                          </label>
                      </div>
                  </div>
 <div class="section row">
   <div class="col-md-2"></div>
   <div class="col-md-1">
                          <label for="sn" class="field prepend-icon">
                              <label for="sn" class="field-icon">
            图片
                              </label>
                          </label>
                      </div>
                      <div class="col-md-6">
                          <label for="name" class="field">
                              <input id="name" name="filename" class="gui-input" placeholder="图片" type="file" value="上传图片"/>
                          </label>
                      </div>
                  </div>                  
<div class="section row">
   <div class="col-md-2"></div>
   <div class="col-md-1">
                          <label for="sn" class="field prepend-icon">
                              <label for="sn" class="field-icon">
            描述
                              </label>
                          </label>
                      </div>
   <div class="col-md-6">
      <label for="address" class="field">
         <input id="address" name="description" class="gui-input" placeholder="描述" type="text" value="${product.description}"/>
      </label>
   </div>
                  </div>
                  <div class="panel-footer text-center">
                      <button type="submit" class="button"> 保存 </button>
                      <button type="button" class="button" onclick="javascript:window.history.go(-1);"> 返回 </button>
                  </div>
              </div>
          </form>

我有个地方搞不懂,如果用户修改商品的时候,没有更换图片,那文件上传项是不是就是为空,然后在工具类中

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

判断为空时,就不进行数据的保存,

然后就会从隐藏字段中获取之前商品已经存在的图片数据

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

请问我理解的对吗?



2:

我的疑问是,如果用户在修改商品时,更换了图片,上传了新的图片,那会不会跟隐藏字段中的旧数据发生冲突的?

我搞不懂,为什么有隐藏字段的旧数据在,新上传的图片可以把它覆盖掉?

正在回答 回答被采纳积分+1

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

13回答
吃吃吃鱼的猫 2019-06-05 11:53:32

同学你好,隐藏字段放form表单中的最后面,会覆盖之前的数据,同学理解就好,不必纠结这个哦~

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

好帮手慕阿满 2019-06-04 20:20:54

同学你好,试验了一下,这里的隐藏字段应该写在form表单中的前边。另外同学最后的问题是什么意思?不是很理解同学最后想表达什么。建议同学表示的更清楚些。

祝:学习愉快~

  • 提问者 MasonM #1
    老师,我的意思是,如果把隐藏字段放form表单中的最后面,就会出现用户修改商品图片失败的情况,就会一直使用隐藏字段中的数据
    2019-06-04 20:24:54
提问者 MasonM 2019-06-04 17:22:01

还是不太懂,因为我是这样想的,因为有隐藏字段在,也有文件上传项在,如果在用户编辑商品,更换了图片,然后通过UploadUtils工具类去获取数据,如果照老师说的,一次性读完的话,

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

就是,隐藏字段会被fileItem.isFormField()捕获到数据,并存入map集合中,

而在文件上传项目的数据会被else那捕获到,也会存入map集合呀,

为什么说,隐藏字段在最底下的时候,也不影响用户更换图片呢?

因为隐藏字段和文件上传项目的数据都会被UploadUtils工具类捕获到并存入map中的呀?

请老师解答一下,麻烦您了!谢谢!

提问者 MasonM 2019-06-04 17:14:16

老师,但UploadUtils工具类中的fileName只是一个变量,是通过fileItem获取到的文件名呀

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


好帮手慕阿满 2019-06-04 15:06:39

同学你好,检查了一下同学的代码,发现同学在product_update.jsp页面中,<input type="hidden" name="fileName" value="${product.filename}">的name是filename,全部小写了 ,

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

而在UploadUtils工具类中,判断文件名时为fileName,如:

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

这里不能正确的获取文件名,所以数据库中的图片名及path都没改变,建议同学修改一下product_update.jsp页面中的fileName再试试。

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

  • 提问者 MasonM #1
    老师是把product_update.jsp页面中哪些改成fileName呢?
    2019-06-04 17:07:57
  • 提问者 MasonM #2
    请看新回答
    2019-06-04 17:11:40
吃吃吃鱼的猫 2019-06-03 19:39:19

同学你好,老师已经联系你,关注一下私信哦~

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

  • 提问者 MasonM #1
    老师怎么我还没收到你的私信呢?
    2019-06-03 19:43:58
吃吃吃鱼的猫 2019-06-03 18:58:03

同学你好,

下图标识的输出语句是从哪里输出的呢?

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

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

  • 提问者 MasonM #1
    老师,这个输出语句是在uploadFile工具类中的哦
    2019-06-03 18:59:28
提问者 MasonM 2019-06-03 17:58:34

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

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

输出这个

CategoryService的findAll方法执行了

CategoryDao的findAll方法执行了...

filename=wen.jpg

{path=/upload/34c97b19171b4318bdf699e7817fe207.jpg, filename=wen.jpg, pname=测试, price=100.0, author=qq, cid=1}

测试:/upload/eb2f54d7fa2f4c89903baad84d9e36cf.jpg  wumingnvlang.jpg

ProductServlet的findAll方法

ProductService的findAll方法

ProductDao的findAll方法

商品Product{pid=75, pname='测试', author='qq', price=100.0, description='qqq', filename='wumingnvlang.jpg', path='/upload/eb2f54d7fa2f4c89903baad84d9e36cf.jpg', category=Category{cid=1, cname='人物画', cdesc='描述人物的画'}}  商品分类名称:人物画


吃吃吃鱼的猫 2019-06-03 17:44:43

同学你好,同学在Servlet层的update方法的第一行,取出path的值输出。

然后再进行测试。

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

  • 提问者 MasonM #1
    测试了也这样,老师请看
    2019-06-03 17:56:51
提问者 MasonM 2019-06-03 16:16:45

这是页面代码

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


<!-- Mirrored from admindesigns.com/demos/absolute/1.1/admin_forms-validation.html by HTTrack Website Copier/3.x [XR&CO'2014], Thu, 06 Aug 2015 02:56:15 GMT -->
<head>
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">

<title>油画商城--修改商品</title>

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/skin/default_skin/css/theme.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/css/admin-forms.css">
<link rel="shortcut icon" href="${pageContext.request.contextPath }/assets/img/favicon.ico">
</head>

<body class="admin-validation-page" data-spy="scroll" data-target="#nav-spy" data-offset="200">
<div id="main">
    <%@ include file="header.jsp" %>
    
     <%@ include file="left.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">
                <form id="admin-form" name="addForm" action="${pageContext.request.contextPath}/ProductServlet?method=update" method="post" enctype="multipart/form-data">

                    <div class="panel-body bg-light">
                        <div class="section-divider mt20 mb40">
                            <span> 基本信息 </span>
                        </div>
                        <div class="section row">
                     <div class="col-md-2"></div>
                     <div class="col-md-1">
                                <label for="sn" class="field prepend-icon">
                                    <label for="sn" class="field-icon">
                              名称
                                    </label>
                                </label>
                            </div>
                            <div class="col-md-6">
                                <label for="sn" class="field">
                                    <input  name="pname" class="gui-input" placeholder="名称" type="text" value="${product.pname}"/>
                                </label>
                            </div>
                        </div>
                  <div class="section row">
                     <div class="col-md-2"></div>
                     <div class="col-md-1">
                                <label for="sn" class="field prepend-icon">
                                    <label for="sn" class="field-icon">
                              价格
                                    </label>
                                </label>
                            </div>
                            <div class="col-md-6">
                                <label for="sn" class="field">
                                    <input  name="price" class="gui-input" placeholder="价格" type="text" value="${product.price}"/>
                                    
                                </label>
                            </div>
                        </div>
                        <div class="section row">
                            <div class="col-md-2"></div>
                            <div class="col-md-1">
                                <label for="sn" class="field prepend-icon">
                                    <label for="sn" class="field-icon">
                                        作者
                                    </label>
                                </label>
                            </div>
                            <div class="col-md-6">
                                <label for="sn" class="field">
                                    <input  name="author" class="gui-input" placeholder="作者" type="text" value="${product.author}"/>

                                </label>
                            </div>
                        </div>
                        <div class="section row">
                     <div class="col-md-2"></div>
                     <div class="col-md-1">
                                <label for="sn" class="field prepend-icon">
                                    <label for="sn" class="field-icon">
                              分类
                                    </label>
                                </label>
                            </div>
                            <div class="col-md-6">
                                <label for="sn" class="field select">
                                    <select id="departmentSn" name="cid" class="gui-input" placeholder="分类...">
                                        <c:forEach var="cl" items="${categoryList}">
                                            <c:choose>
                                                <c:when test="${cl.cid==product.category.cid}">
                                                    <option value="${product.category.cid}" selected="selected">${product.category.cname}</option>
                                                </c:when>
                                                <c:otherwise>
                                                    <option value="${cl.cid}">${cl.cname}</option>
                                                </c:otherwise>
                                            </c:choose>
                                        </c:forEach>
                                    </select>
                                    <i class="arrow double"></i>
                                </label>
                            </div>
                        </div>
                   <div class="section row">
                     <div class="col-md-2"></div>
                     <div class="col-md-1">
                                <label for="sn" class="field prepend-icon">
                                    <label for="sn" class="field-icon">
                              图片
                                    </label>
                                </label>
                            </div>
                            <div class="col-md-6">
                                <label for="name" class="field">
                                    <input id="name" name="filename" class="gui-input" placeholder="图片" type="file" value="上传图片"/>
                                </label>
                            </div>
                        </div>                
                  <div class="section row">
                     <div class="col-md-2"></div>
                     <div class="col-md-1">
                                <label for="sn" class="field prepend-icon">
                                    <label for="sn" class="field-icon">
                              描述
                                    </label>
                                </label>
                            </div>
                     <div class="col-md-6">
                        <label for="address" class="field">
                           <input id="address" name="description" class="gui-input" placeholder="描述" type="text" value="${product.description}"/>
                        </label>
                     </div>
                        </div>
                        <div class="panel-footer text-center">
                            <button type="submit" class="button"> 保存 </button>
                            <button type="button" class="button" onclick="javascript:window.history.go(-1);"> 返回 </button>
                        </div>
                    </div>

                    <input type="hidden" name="pid" value="${product.pid}">
                    <input type="hidden" name="filename" value="${product.filename}">
                    <input type="hidden" name="path" value="${product.path}">
                </form>
            </div>
        </div>
    </div>
</section>

</section>

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

我把隐藏字段放最底下了

提问者 MasonM 2019-06-03 16:14:15

首先,我添加一个新的商品

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

然后我更新他的图片

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

然后我再查询数据库

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

发现图片并没有发生更换呀



  • 提问者 MasonM #1
    页面代码我贴再贴一下,因为限制不允许超过8000字
    2019-06-03 16:14:43
好帮手慕阿莹 2019-06-01 18:10:18

1、是的同学理解的是对的,文件上传像为空时,还是原来的数据,不进行更新。

2、不会冲突的,它会把它覆盖掉,以为修改时,是把修改页面提交过了来的数据重新放到一个新的对象里去了,然后把这个新的对象拿去修改到数据库中,不会冲突,而是覆盖掉了:

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

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

3、他们是按照表单提交的属性来读取数据的,并不是把之前的map中的数据覆盖掉了。也并没有说在读取到隐藏字段的时候,才会把filenamePath 之类的存入。它就是按照顺序读取的。都会一起读完的。map中的存的就会是:

例如:key:id  value:1

           key:name value:“abc”

            。。。。。

然后都把它们取出来,放到新的product中去。

然后把这个新的product传给sql进行更新数据。所以这个隐藏字符放在哪里都是无所谓的。都可以通过map.get(key) 拿出来的。


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


  • 提问者 MasonM #1
    老师不对喔,我测试了一下,把隐藏字段放最底下的话,是没办法更换图片的,一直都是之前的那个图片呀,请看我的新回答谢谢!!
    2019-06-03 16:09:06
提问者 MasonM 2019-06-01 13:54:06

关于疑问2,我的猜想是,因为隐藏字段在表单的上部分,而文件上传项是在下面,如果提交表单时候,是优先处理上面的表单项数据,比如

1:普通表单项1

2:普通表单项2

3:文件表单项

4:普通表单项3

在UploadFile工具类中,是按照表单项的顺序来读取数据的,请问对吗?

回到问题2

然后由于隐藏字段在文件上传项的上面

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

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

所以,如果用户上传的新的图片,在工具类读取到隐藏字段时候,会把filename,path,之类的先存入map集合中,然后当工具类读取到有新的文件上传,并且文件不为空,就开始读取数据,并把之前map集合中关于filename,path的数据覆盖掉了.

请问我的猜想正确吗?

UploadFile工具类读取数据是不是根据表单项的顺序来读取的呢?

如果我把隐藏字段放在最底下,会不会就一直无法更换新的图片呢?

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

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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