我一直在尝试让我的项目更加结构化,因此我一直在关注https://netbeans.org/kb/docs/javaee/ecommerce/page-views-controller.html#controller中的netbeans电子商务示例项目中的示例。我根据例子更改了控制器。现在我无法进入显示数据库中值的页面。
我的Servlet如下所示。
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
Op_Stock ops = new Op_Stock();
String userPath = request.getServletPath();
String param = null;
Map<String, String> prdMap = new LinkedHashMap<String, String>();
Map<String, String> lvlMap = new LinkedHashMap<String, String>();
ArrayList<HashMap<String, String>> stkList = new ArrayList<>();
switch (userPath) {
case "/ViewStock":
stkList = ops.getAllStockDetails();
request.setAttribute("stkList", stkList);
userPath = "Stock.jsp";
param = "type=m&page=stk";
break;
case "/AddStockForm":
prdMap = ops.getAllProducts();
lvlMap = ops.getAllLevels();
request.setAttribute("prdMap", prdMap);
request.setAttribute("lvlMap", lvlMap);
userPath = "Insertstk.jsp";
param = "type=m&page=stk";
break;
case "/AddStock":
System.out.println("prdId: " + request.getParameter("prdId"));
int iprdId = Integer.parseInt(request.getParameter("prdId"));
int istkIn = Integer.parseInt(request.getParameter("stkIn"));
int istkthld = Integer.parseInt(request.getParameter("stkthld"));
int ilvlId = Integer.parseInt(request.getParameter("lvlId"));
int stkIn = ops.insertStockDetails(iprdId, istkIn, istkthld, ilvlId);
stkList = ops.getAllStockDetails();
request.setAttribute("stkList", stkList);
if (stkIn > 0) {
userPath = "/Stock.jsp";
param = "message=Stock Details Created!&pg=stk&type=m";
} else {
prdMap = ops.getAllProducts();
lvlMap = ops.getAllLevels();
request.setAttribute("prdMap", prdMap);
request.setAttribute("lvlMap", lvlMap);
userPath = "/Insertstk.jsp";
param = "message=Could not create!&pg=stk&type=m";
}
break;
case "/DeleteStock":
int delStk = ops.deleteStockDetails(request.getParameter("stkId"));
if (delStk > 0) {
userPath = "Stock.jsp";
param = "message=Stock Details Deleted!&pg=stk&type=m";
stkList = ops.getAllStockDetails();
request.setAttribute("stkList", stkList);
} else {
userPath = "Stock.jsp";
stkList = ops.getAllStockDetails();
request.setAttribute("stkList", stkList);
param = "message=Could not delete!&pg=stk&type=m";
}
break;
}
// use RequestDispatcher to forward request internally
String url = userPath + "?" + param;
request.getRequestDispatcher(url).forward(request, response);
//response.sendRedirect(url);
}
在servlet中,我将设置属性存储在存储数据库内容的请求中。
假设我正在进行插入操作,该操作位于开关盒中&#34; / AddStock&#34;执行此操作后,请求将转发到显示表格的页面,因为我使用转发它将转到显示页面,但地址栏中的URL将与我的插入表单中的所有请求参数保持一致因此,当我刷新页面时,插入操作将再次发生。如果我使用response.sendRedirect(url),我将无法使用请求属性,因此无法显示数据库值。
我的插入表格如下。
<div class="col-md-10">
<form method="POST" action="AddStock" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">Product</label>
<div class="col-sm-4">
<select id="s_prdname" class="form-control" name="prdId" required>
<option>Select</option>
<c:forEach items="${prdList}" var="mapItem">
<option value="${mapItem.key}">${mapItem.value}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Level</label>
<div class="col-sm-4">
<select id="s_lvlname" class="form-control" name="lvlId" required>
<option>Select</option>
<c:forEach items="${lvlList}" var="mapItem">
<option value="${mapItem.key}">${mapItem.value}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">In-Stock</label>
<div class="col-sm-2">
<input type="number" name="stkIn" class="form-control" id="stkIn"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Threshold</label>
<div class="col-sm-2">
<input type="number" name="stkthld" class="form-control" id="stkthld"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default btn-primary"><strong>Submit</strong></button>
</div>
</div>
</form>
</div>
我的显示页面如下。
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover">
<thead>
<th><strong>Product Name</strong></th>
<th><strong>Level</strong></th>
<th><strong>In Stock</strong></th>
<th><strong>Threshold</strong></th>
<th><strong>Expiry</strong></th>
<th><strong>Operations</strong></th>
</thead>
<tbody>
<jsp:useBean id="lvl" scope="request" class="Level.Level"/>
<jsp:useBean id="prd" scope="request" class="Product.Product"/>
<c:forEach items="${stkList}" var="row">
<tr>
<td><jsp:setProperty name="prd" property="prdId" value="${row.prdStk}"/><jsp:getProperty name="prd" property="prdName"/></td>
<td><jsp:setProperty name="lvl" property="lvlId" value="${row.lvlStk}"/><jsp:getProperty name="lvl" property="lvlName"/></td>
<td>${row.inStk}</td>
<td>${row.thldStk}</td>
<td>${row.expStk}</td>
<td>
<button type="button" class="btn btn-default btn-sm btn-danger" onclick="deletedata('${row.idStk}');">
<span class="glyphicon glyphicon-trash"></span>
</button>
<button class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal">
<span class="glyphicon glyphicon-eye-open"></span>
</button>
</td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tfoot>
</table>
</div>
请帮我解决这个问题,如果我使用的方法有误,请提出正确的方法。
答案 0 :(得分:0)
为了解决这个问题我用过。
response.sendRedirect(url);
而不是
request.getRequestDispatcher(url).forward(request, response);
而不是通过在请求中设置属性来显示数据库中的值,我已经习惯了访问我班级中的方法,如下所示。
<jsp:useBean id="stk" scope="request" class="Stock.Op_Stock"/>
<c:forEach items="${stk.allStockDetails}" var="row">
// Values in the map displayed here
</c:forEach>