查询id页面显示500,搞不懂了,老师帮我看看
package com.imooc.supermarket;
public class Goods {
private Integer id;
private String name;
private String category;
private Float price;
private String remarks;
public Goods(Integer id, String name, String category, Float price, String remarks) {
super();
this.id = id;
this.name = name;
this.category = category;
this.price = price;
this.remarks = remarks;
}
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 getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", category=" + category + ", price=" + price + ", remarks="
+ remarks + "]";
}
}
package com.imooc.supermarket;
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("/GoodsList")
public class GoodsListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GoodsListServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取全局对象
ServletContext sc=request.getServletContext();
//每次启动doGet方法判断全局对象list是否为空,避免重复操作。
if(sc.getAttribute("list")==null) {
List<Goods> list=new ArrayList<Goods>();
list.add(new Goods(1001,"小米手机","电子产品",2999f,"为发烧而生"));
list.add(new Goods(1002,"一加手机","电子产品",2799f,"不将就"));
list.add(new Goods(1003,"苹果平板","家用电脑",8999f,"你的下一部电脑何必是电脑"));
sc.setAttribute("list", list);
}
request.getRequestDispatcher("/goods.jsp").forward(request, response);
}
}
package com.imooc.supermarket;
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("/GoodsAdd")
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 category=request.getParameter("goodsType");
String price=request.getParameter("price");
String remarks=request.getParameter("description");
//分别把对象属性传入Goods类中
Goods goods=new Goods(Integer.parseInt(id),name,category,Float.parseFloat(price),remarks);
ServletContext sc=request.getServletContext();
//获取全局对象list,并返回一个list集合。
List<Goods> list=(List)sc.getAttribute("list");
//把获取的对象类放入List集合中
list.add(goods);
//把list集合再次添加到全局对象中list中。
sc.setAttribute("list", list);
//请求转发
request.getRequestDispatcher("/goods.jsp").forward(request, response);
}
}
package com.imooc.supermarket;
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("/SearchGoods")
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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取输入id名称,因为定义的id为整数,所以要转换成整数,方便判断
int id=Integer.parseInt(request.getParameter("id"));
//获取全局对象sc
ServletContext sc=request.getServletContext();
//获取全局对象list,返还list集合
List<Goods> list=(List)sc.getAttribute("list");
//定义一个boolean类型,方便判断
boolean isSearch=false;
Goods SearchGoods=null;
//遍历list集合赋值给goods
for(Goods goods:list) {
//判断输入id是否在list集合中存在
if(goods.getId()==id) {
SearchGoods=goods;
isSearch=true;
}
}
//存在的情况
if(isSearch) {
sc.setAttribute("list", SearchGoods);
request.getRequestDispatcher("/searchById.jsp").forward(request, response);
//不存在的情况
}else {
request.setAttribute("msg", "商品id不存在");
request.getRequestDispatcher("/searchById.jsp").forward(request, response);
}
}
}
Goods.jsp
<%@ page language="java" 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="/supermarket/SearchGoods" 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>
<!-- 遍历全局对象list,分别取出对应属性 -->
<c:forEach items="${applicationScope.list }" var="l" varStatus="idx">
<tr>
<td>${idx.index+1 }</td>
<td>${l.id }</td>
<td>${l.name }</td>
<td>${l.category }</td>
<td style="color:red; font-weight: bold">¥<fmt:formatNumber value="${l.price }" pattern="0,000.00"></fmt:formatNumber></td>
<td>${l.remarks }</td>
<td> </td>
</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">×</span>
</button>
<h4 class="modal-title">新增商品</h4>
</div>
<div class="modal-body">
<form action="/supermarket/GoodsAdd" 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>
searchById.jsp
<%@ page language="java" 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> </th>
</tr>
</thead>
<c:choose>
<c:when test="${requestScope.msg!=null }">
<h1 style="color:red">${requestScope.msg}</h1>
</c:when>
<c:otherwise>
<tbody>
<tr>
<td>${applicationScope.list.id }</td>
<td>${applicationScope.list.name }</td>
<td>${applicationScope.list.category }</td>
<td style="color:red; font-weight: bold">¥<fmt:formatNumber value="${applicationScope.list.price }" pattern="0,000.00"></fmt:formatNumber>
<td>${applicationScope.list.remarks }</td>
</td>
</tr>
</tbody>
</c:otherwise>
</c:choose>
</table>
</div>
</div>
</div>
</body>
</html>
正在回答
同学你好,1、根据Illegal text inside "c:choose" tag报错信息:
在<c:choose>标签中不能识别<!-- 这样的标签,同学可以检查一下,自己对应的页面中,是不是在<c:choose>标签范围内,书写了<!-- -->这样的注释内容,老师测试贴出代码并没有对应的注释内容,所以也没有复现同学所描述的问题。
2、根据如下报错信息
并没有加载到对应的searchById的jsp页面,同学可以尝试清理一下缓存,重启项目再来试试。
如上所示,清理后重新项目再来试试。
祝学习愉快!
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星