新增商品后总是出现goodsList cannot be resolved to a variable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | 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( "goodsPrice" ); String goodsRemark=request.getParameter( "description" ); System.out.println(goodsId); Goods goods = new Goods(Integer.parseInt(goodsId),goodsName,goodsType,Float.parseFloat(price),goodsRemark); ServletContext context=request.getServletContext(); List goodsList=(List)context.getAttribute( "goods" ); goodsList.add(goods); context.setAttribute( "goods" , goodsList); request.getRequestDispatcher( "/goods.jsp" ).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( "/goods.jsp" ).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( "/goods.jsp" ).forward(request, response); } } //goods.jsp <%@ page contentType= "text/html; charset=UTF-8" %> <%@ taglib uri= "http://java.sun.com/jsp/jstl/core" prefix= "c" %> <!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> <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>价格</th> <th>${good.goodsRemark}</th> <th> </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" >×</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= "请输入商品编号" > </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> |
Description Resource Path Location Type
goodsList cannot be resolved to a variable GoodsAddServlet.java /Gim/src/main/java/com/imooc/gim line 51 Java Problem
9
收起
正在回答
1回答
同学你好,需要同学提供一下Goods类代码。
祝学习愉快~
java工程师2020版
- 参与学习 人
- 提交作业 9404 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧