1

2019-09-05 14:02:28.776 WARN 11096 --- [nio-8080-exec-3] osweb.servlet.PageNotFound:没有 GET /company/delete 的映射

我有一个使用 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 WARN 11096 --- [nio-8080-exec-3] osweb.servlet.PageNotFound:没有 GET /company/delete 的映射

4

1 回答 1

2

在您的请求中,您使用coupidPathVariable不是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";
}
于 2019-09-05T11:14:01.523 回答