根据id查询商品的时候出现异常?
package com.imooc.display;
public class Goods {
// private String num;
private Integer id;//商品编号
private String name;//商品名称
private String type;//商品类别
private Float price;//商品价格
private String ps;//商品备注
public Goods(Integer id, String name, String type, Float price, String ps) {
super();
this.id = id;
this.name = name;
this.type = type;
this.price = price;
this.ps = ps;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getPs() {
return ps;
}
public void setPs(String ps) {
this.ps = ps;
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", type=" + type + ", price=" + price + ", ps=" + ps + "]";
}
}
package com.imooc.display;
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 GoodsListServlet
*/
@WebServlet("/gls")
public class GoodsListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GoodsListServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context=request.getServletContext();
if(context.getAttribute("goods")==null) {
List list=new ArrayList();
list.add(new Goods(1001,"格力空调","家用电器",30000f,"铂金气质,空净合一"));
list.add(new Goods(1002,"LV","服装",18888f,"生活不是一场旅行,每场旅行都会有终结"));
context.setAttribute("goods",list);
}
request.getRequestDispatcher("/goods.jsp").forward(request, response);
}
}
<%@ 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>
<c:if test="${applicationScope.goods==null }">
<h1 style="text-align: left;color:red">商品id不存在</h1>
</c:if>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品类别</th>
<th>价格</th>
<th>备注</th>
<th> </th>
</tr>
</thead>
<tbody>
<c:if test="${applicationScope.goods!=null }">
<tr>
<td>${idx.index+1 }</td>
<td>${god.id }</td>
<td>${god.name }</td>
<td>${god.type}</td>
<td style="color: red; font-weight: bold">¥<fmt:formatNumber value="${god.price}" pattern="0,000.00"></fmt:formatNumber>
<td>${god.ps}</td>
</td>
</tr>
</c:if>
</tbody>
</table>
</div>
</div>
</div>
</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>
<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="" 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>
<tr>
<th>序号</th>
<th>商品编号</th>
<th>商品名称</th>
<th>商品类别</th>
<th>价格</th>
<th>备注</th>
<th> </th>
</tr>
</thead>
<tbody>
<c:forEach items="${applicationScope.goods }" var="god" varStatus="idx">
<tr>
<td>${idx.index+1 }</td>
<td>${god.id }</td>
<td>${god.name }</td>
<td>${god.type}</td>
<td style="color: red; font-weight: bold">¥<fmt:formatNumber value="${god.price}" pattern="0,000.00"></fmt:formatNumber>
<td>${god.ps}</td>
</td>
</tr>
</c:forEach>
</tbody>
</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">×</span>
</button>
<h4 class="modal-title">新增商品</h4>
</div>
<div class="modal-body">
<form action="/display/gas" method="post">
<div class="form-group">
<label>商品编号</label>
<input type="text" name="goodsId" class="form-control" id="empno" placeholder="请输入商品编号">
</div>
<div class="form-group">
<label>商品名称</label>
<input type="text" name="goodsName" class="form-control" id="ename" placeholder="请输入商品名称">
</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="请输入价格">
</div>
<div class="form-group">
<label>备注</label>
<input type="text" name="description" class="form-control" id="sal" placeholder="请输入备注">
</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>
package com.imooc.display;
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("/gas")
public class GoodsAddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GoodsAddServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String id=request.getParameter("goodsId");
String name=request.getParameter("goodsName");
String type=request.getParameter("goodsType");
String price=request.getParameter("price");
String ps=request.getParameter("description");
Goods goods=new Goods(Integer.parseInt(id),name,type,Float.parseFloat(price),ps);
ServletContext context=request.getServletContext();
List list=(List)context.getAttribute("goods");
list.add(goods);
context.setAttribute("goods",list);
request.getRequestDispatcher("/goods.jsp").forward(request, response);
}
}
package com.imooc.display;
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("/sgbis")
public class SearchGoodsByIdServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SearchGoodsByIdServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String id=request.getParameter("goodsId");
ServletContext context=request.getServletContext();
List list=(List)context.getAttribute("goods");
boolean flag=false;
for(int i=0;i<list.size();i++) {
if(((Goods)list.get(i)).getId()==Integer.parseInt(id)) {
flag=true;
context.setAttribute("goods",list.get(i));
break;
}
if(!flag) {
context.setAttribute("goods",null);
}
}
request.getRequestDispatcher("/searchById.jsp").forward(request, response);
}
}
以上代码运行的时候出现一下异常,
老师,帮忙看一下哪里出错了。
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星