直接访问good.jsp未调用/gl显示预置的商品list

直接访问good.jsp未调用/gl显示预置的商品list

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
 
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>商品列表</title>
    <link href="css/bootstrap.css" type="text/css" rel="stylesheet">
    </link>
    <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.js"></script>
    <style type="text/css">
    .pagination {
        margin: 0px
    }
    .pagination>li>a,
    .pagination>li>span {
        margin: 0 5px;
        border: 1px solid #dddddd;
    }
    .glyphicon {
        margin-right: 3px;
    }
    .form-control[readonly] {
        cursor: pointer;
        background-color: white;
    }
    #dlgPhoto .modal-body {
        text-align: center;
    }
    .preview {
        max-width: 500px;
    }
    </style>
    <script>
        $(function () {
            
            $("#btnAdd").click(function () {
                $('#dlgForm').modal()
            });
        })
    </script>
</head>
<body>
    <div class="container">
        <div class="row">
            <h1 style="text-align: center">IMOOC商品信息表</h1>
            <div class="panel panel-default">
                <div class="clearfix panel-heading ">
                    <div>
                        <label class="form-inline">
                            <div class="form-group" style="width: 850px;">
                                <button class="btn btn-primary" id="btnAdd">新增
                                </button>
                            </div>
                            <div class="form-group pull-right">
                            <form action="/Gim/sg" method="post">
                                <input type="text" class="form-control" id="searchById" name="id" placeholder="根据商品id进行查询">
                                <button type="submit" class="btn btn-primary">查询</button>
                                </form>
                            </div>
                        </label>
                    </div>
                    <table class="table table-bordered table-hover">
                     <thead>
                              <c:if test="${applicationScope.idFlag== true }">
                              <h1 style='color:red'>不允许有重复商品编号,请新增商品!</h1>                          
                              </c:if>               
                             
                              
                              <tr>
                                <th>序号</th>
                                <th>商品编号</th>
                                <th>商品名称</th>
                                <th>商品类别</th>
                                <th>价格</th>
                                <th>备注</th>
                                <th>&nbsp;</th>
                         </tr>       
                           
                        <c:forEach items="${applicationScope.goods}" var ="good" varStatus="idx">
                            <tr>
                                <th>${idx.index+1}</th>
                                <th>${good.goodsId}</th>
                                <th>${good.goodsName}</th>
                                <th>${good.goodsType}</th>
                                <th>¥<fmt:formatNumber value = "${good.price}" pattern="0,000.00"></fmt:formatNumber></th>
                                <th>${good.goodsRemark}</th>
                                <th>&nbsp;</th>
                            </tr>
                            </c:forEach>                        
                       
                        
                        </thead>
                    </table>
                </div>
            </div>
        </div>
        <!-- 表单 -->
        <div class="modal fade" tabindex="-1" role="dialog" id="dlgForm">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title">新增商品</h4>
                    </div>
                    <div class="modal-body">
                        <form action="/Gim/create" method="post">
                            <div class="form-group">
                                <label>商品编号</label>
                                <input type="text" name="goodsId" class="form-control" id="empno" placeholder="请输入商品编号" required>
                            </div>
                            <div class="form-group">
                                <label>商品名称</label>
                                <input type="text" name="goodsName" class="form-control" id="ename" placeholder="请输入商品名称" required>
                            </div>
                            <div class="form-group">
                                <label>商品类别</label>
                                <select id="gname" name="goodsType" class="form-control">
                                    <option selected="selected">服装</option>
                                    <option value="家用">家用电器</option>
                                    <option value="生活">生活用品</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>价格</label>
                                <input type="text" name="price" class="form-control" id="sal" placeholder="请输入价格" required>
                            </div>
                            <div class="form-group">
                                <label>备注</label>
                                <input type="text" name="description" class="form-control" id="sal" placeholder="请输入备注" required>
                            </div>
                            <div class="form-group" style="text-align: center;">
                                <button type="submit" class="btn btn-primary">保存</button>
                            </div>
                        </form>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>商品信息</title>
    <link href="css/bootstrap.css" type="text/css" rel="stylesheet">
    </link>
    <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.js"></script>
    <style type="text/css">
    .pagination {
        margin: 0px
    }
    .pagination>li>a,
    .pagination>li>span {
        margin: 0 5px;
        border: 1px solid #dddddd;
    }
    .glyphicon {
        margin-right: 3px;
    }
    .form-control[readonly] {
        cursor: pointer;
        background-color: white;
    }
    #dlgPhoto .modal-body {
        text-align: center;
    }
    .preview {
        max-width: 500px;
    }
    </style>
</head>
<body>
    <button class="btn btn-primary" style="margin-left: 30px; margin-top: 40px;" onclick="javascript:history.back(-1);">返回</button>
    <div class="container">
        <div class="row">
            <h1 style="text-align: center">IMOOC商品信息表</h1>
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>商品编号</th>
                        <th>商品名称</th>
                        <th>商品类别</th>
                        <th>价格</th>
                        <th>备注</th>
                        <th>&nbsp;</th>
                    </tr>
                </thead>
                <tbody>
                   <c:choose>
                        <c:when test="${requestScope.error == 1}">
                           <h1 style='color:red'>输入错误</h1>            
                        </c:when> 
                     <c:when test="${applicationScope.flag == 2}">
                                 <h1 style='color:red'>商品id不存在</h1>                                        
                         </c:when>                       
                         <c:when test="${applicationScope.flag == 1}">                            
                              <tr>
                                   <th>${good1.goodsId}</th>
                                    <th>${good1.goodsName}</th>
                                    <th>${good1.goodsType}</th>
                                    <th>¥<fmt:formatNumber value = "${good1.price}" pattern="0,000.00"></fmt:formatNumber></th>
                                    <th>${good1.goodsRemark}</th>
                                    <th>&nbsp;</th>
                              </tr>                         
                         </c:when>                      
                     </c:choose>
                </tbody>
            </table>
        </div>
    </div>
    </div>
</body>
</html>
package com.imooc.gim;
public class Goods {
 private Integer goodsId;
 private String goodsName;
 private String goodsType;
 private float price;
 private String goodsRemark;
 public Goods() {
 
 }
public Goods(Integer goodsId, String goodsName, String goodsType, float price, String goodsRemark) {
super();
this.goodsId = goodsId;
this.goodsName = goodsName;
this.goodsType = goodsType;
this.price = price;
this.goodsRemark = goodsRemark;
}
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public String getgoodsType() {
return goodsType;
}
public void setgoodsType(String goodsType) {
this.goodsType = goodsType;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getGoodsRemark() {
return goodsRemark;
}
public void setGoodsRemark(String goodsRemark) {
this.goodsRemark = goodsRemark;
}
@Override
public String toString() {
return "Goods [goodsId=" + goodsId + ", goodsName=" + goodsName + ", goodsType=" + goodsType
+ ", price=" + price + ", goodsRemark=" + goodsRemark + "]";
}
 
}
package com.imooc.gim;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class SearchGoodsByIdServlet
 */
@WebServlet("/sg")
public class SearchGoodsByIdServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchGoodsByIdServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
 * @param good 
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String id = request.getParameter("id");
  ServletContext context=request.getServletContext();
  context.getAttribute("goods");
  List goodsList=(List)context.getAttribute("goods");
  if(goodsList==null) {
     request.getRequestDispatcher("/gl").forward(request, response);
  }    
  
  Boolean flag=false;
  Goods gooda=null;
  try {
  Integer goodsId = Integer.parseInt(id);  
  for(int i=0; i<goodsList.size(); i++) {
 
  Integer id1 = ((Goods)goodsList.get(i)).getGoodsId();
  if(id1.equals(goodsId) ) {
  gooda=(Goods) goodsList.get(i);
  flag=true;   
  }
      }
  
 
  int flag1 =0;  
  
  if(flag) {
  flag1 = 1;
  }else {
  flag1 = 2;
  }
  context.setAttribute("good1",gooda);
  context.setAttribute("flag",flag1);
  }catch(NumberFormatException e) {
  request.setAttribute("error", 1);
  request.getRequestDispatcher("/searchById.jsp").forward(request, response);
  }
  
  
    
     request.getRequestDispatcher("/searchById.jsp").forward(request, response);
  
         
  
}
}
package com.imooc.gim;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class GoodsAddServlet
 */
@WebServlet("/create")
public class GoodsAddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GoodsAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String goodsId=request.getParameter("goodsId");
    String goodsName=request.getParameter("goodsName");
    String goodsType=request.getParameter("goodsType");
    String price=request.getParameter("price");
    String goodsRemark=request.getParameter("description");
    System.out.println(goodsId);
    Goods good = new Goods(Integer.parseInt(goodsId),goodsName,goodsType,Float.parseFloat(price),goodsRemark);
    ServletContext context=request.getServletContext();
    context.getAttribute("goods");
    List<Goods> goodsList=(List)context.getAttribute("goods");
    if(goodsList==null) {
     request.getRequestDispatcher("/gl").forward(request, response);
    }    
    
boolean idFlag=false;
    for(Goods goo: goodsList) {
 if(goo.getGoodsId()==Integer.parseInt(goodsId)) {
 idFlag=true;
 break;
 }
}
    
    if(idFlag==false) {
    goodsList.add(good);
    context.setAttribute("goods", goodsList);
    }else {
    System.out.println("重复商品编号");
    context.setAttribute("idFlag", idFlag);
    }
    
    request.getRequestDispatcher("/gl").forward(request, response);
}
}
package com.imooc.gim;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class GoodsAddServlet
 */
@WebServlet("/gl")
public class GoodsListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
    /**
     * Default constructor. 
     */
    public GoodsListServlet() {
        // TODO Auto-generated constructor stgaub
    }
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request, response);
}
/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    ServletContext context=request.getServletContext();
   
    if(context.getAttribute("goods")==null) {   
        
    List<Goods> goodsList=new ArrayList<>();
    
    goodsList.add(new Goods(1001,"格力空调","家用电器",3000.00f,"铂金品质"));
    goodsList.add(new Goods(1002,"LV","服装",18888.00f,"生活一场旅行"));
    context.setAttribute("goods", goodsList);     
    
    }
    
    
    
    
    
    request.getRequestDispatcher("/good.jsp").forward(request, response);
    
   }
    
}


正在回答

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

1回答

同学你好,同学很好的将作业批复时出现的问题进行了解决。很棒,继续加油~

另外建议同学下次将修改后的作业重新上传,因为作业批复和问答区通常不是同一个助教老师在做回复。由于问答区的助教老师没有批复过同学作业,可能对同学的问题会理解错误或者无法针对性的作出解析。

祝学习愉快~


  • 晓舟 提问者 #1

    批复的老师说还有问题再在问答区问下,偶就这一个问题了。

    2021-12-07 08:46:26
  • 好帮手慕小蓝 回复 提问者 晓舟 #2

    同学你好,批复文档中是指:

    1.如果同学对文档提出的修改建议有异议,可以来问答区反馈和提问;

    2.如果同学对文档提出的修改建议有疑问,无论是思路还是实现,都可以来问答区提问,老师会帮助同学解答。

    如果同学将作业完善之后,还是建议同学再次提交作业,由老师再次测试更好一些。

    祝学习愉快~

    2021-12-07 09:34:49
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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