没有 GET 映射,即使映射是正确的



>2019-09-05 14:02:28.776 警告 11096 --- [NIO-8080-exec-3] o.s.web.servlet.PageNotFound : 没有 GET 的映射/公司/删除

我有一个使用 spring boot 和 JSP 页面的 CRUD 项目。

这是控制器删除方法

@PostMapping("delete/{coupid}")
public String removeCoupon(@PathVariable int coupid) {
couponService.deleteById(coupid);
return "couponRemoved";
}

有一种方法可以在 JSP 页面中显示所有优惠券:

@GetMapping("/read")
public String getAllCoupons(Model theModel) {
List<Coupon> theCoupon = companyService.getCurrentCompany().getCoupons();
theModel.addAttribute("theCoupon", theCoupon);
return "showAllCoupons";
}

只需将每个优惠券添加到模型中,然后重定向到一个页面,该页面显示所有带有循环的优惠券:

<table class="i">
<tr>
<th>id</th>
<th>title</th>
<th>start</th>
<th>end</th>
<th>amount</th>
<th>type</th>
<th>message</th>
<th>price</th>
<th>image</th>
</tr>
<c:forEach var="tempCoupon" items="${theCoupon}" >
<tr> 
<td> ${tempCoupon.coupid} </td>
<td> ${tempCoupon.title} </td>
<td> ${tempCoupon.startd} </td>
<td> ${tempCoupon.endd} </td>
<td> ${tempCoupon.amount} </td>
<td> ${tempCoupon.type} </td>
<td> ${tempCoupon.message} </td>
<td> ${tempCoupon.price} </td>
<td> ${tempCoupon.image} </td>
<td><a href="${pageContext.request.contextPath}/company/delete?coupid=${tempCoupon.coupid}"> Remove ${tempCoupon.coupid} </a></td>
</tr>
</c:forEach>
</table>

正如你在JSP c:forEach循环中看到的,我还包含一个href链接:

<td><a href="${pageContext.request.contextPath}/company/delete?coupid=${tempCoupon.coupid}"> Remove ${tempCoupon.coupid} </a></td>

它在循环中获取当前优惠券并将其 ID 放入链接中。

当我运行它并单击删除时,我得到这个:

2019-09-05 14:02:28.776 警告 11096 --- [NIO-8080-exec-3] o.s.web.servlet.PageNotFound : 没有 GET 的映射/公司/删除

在您的请求中,您将coupid用作PathVariable而不是RequestParam因此也像它一样发送

例如:

<td><a href="${pageContext.request.contextPath}/company/delete/${tempCoupon.coupid}"> Remove ${tempCoupon.coupid} </a></td>

所以基本上你的资源可以像/company/delete/123一样访问,你试图像这样访问它:/company/delete?coupid=123导致错误。

此外,您实际上是在发送GET请求,但您的资源是POST因此请将其更改为GET

@GetMapping("delete/{coupid}")
public String removeCoupon(@PathVariable int coupid) {
couponService.deleteById(coupid);
return "couponRemoved";
}

最新更新