新增商品后总是出现goodsList cannot be resolved to a variable

新增商品后总是出现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>&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>价格</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="请输入商品编号">
                            </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

正在回答

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

1回答

同学你好,需要同学提供一下Goods类代码。

祝学习愉快~


  • 晓舟 提问者 #1
    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
    package com.imooc.gim;
    public class Goods {
     private Integer goodsId;
     private String goodsName;
     private String goodsType;
     private float price;
     private String goodsRemark;
    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 + "]";
    }
      
    }


    2021-12-04 09:48:26
  • 好帮手慕小蓝 回复 提问者 晓舟 #2

    同学你好,同学代码中存在两个错误:

    1.同学的GoodsAddServlet中,获取价格时使用的请求参数名为goodsPrice,而JSP中那么属性值为price,会导致参数无法传递,出现空指针异常。

    2.如果在直接访问goods.jsp时,使用添加功能,会出现空指针异常。原因是同学的GoodsAddServlet中,List goodsList = (List) context.getAttribute("goods");语句中的goodsList为null,goodsList的setAttribute是在GoodsListServlet中的,而直接访问JSP时没有执行GoodsListServlet导致的。

    综上,老师多次测试几种访问方式,都没有复现同学所指的“goodsList cannot be resolved to a variable”错误。

    如果该错误依然存在,建议同学详细描述一下操作过程以及提供一下错误的截图。

    祝学习愉快~


    2021-12-04 10:11:14
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

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