【问题标题】:No Mapping for GET even though mapping is right即使映射正确,也没有 GET 映射
【发布时间】:2020-01-08 06:41:10
【问题描述】:

2019-09-05 14:02:28.776 警告 11096 --- [nio-8080-exec-3] os.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] os.web.servlet.PageNotFound : 没有 GET 映射 /公司/删除

【问题讨论】:

    标签: java spring spring-boot spring-mvc jsp


    【解决方案1】:

    在您的请求中,您使用 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";
    }
    

    【讨论】:

    • 我想补充一点,这很危险......一个谷歌索引,你的内容就不见了......
    • 这是什么意思?在网址中?
    猜你喜欢
    • 2010-12-30
    • 1970-01-01
    • 2020-07-30
    • 2020-11-07
    • 2018-05-09
    • 1970-01-01
    • 2019-09-27
    • 2020-08-02
    • 2017-07-13
    相关资源
    最近更新 更多