方法不允许,status = 405尝试创建删除实体按钮

时间:2017-05-23 01:01:10

标签: java spring-boot thymeleaf http-error

不确定我在这个问题上做错了什么。尝试创建一个可以从数据库中删除已添加实体的按钮。我得到了405错误,但我不确定我是否因为我在控制器中所做的事情或者我在百里香中写的一些内容而得到了这个错误。谢谢你的帮助。

控制器:

            let bulletmove = SKAction.moveTo(y: self.frame.height, duration: 2)

            let bulletremove = SKAction.removeFromParent()

            addChild(bullett)

            bullett.run(SKAction.sequence([bulletmove, bulletremove])) 

查看:

@Controller
public class BuyerController {

    private BuyerService buyerService;

    @Autowired
    public void setBuyerService(BuyerService buyerService){
        this.buyerService = buyerService;
    }

    @RequestMapping("/add-buyer")
    public String showBuyerPager(Model model){
        List<Buyer> buyers = buyerService.findAllBuyers();
        model.addAttribute("buyers", buyers);
        model.addAttribute("buyer", new Buyer());
        return "add-buyer";
    }

    @GetMapping("/showBuyerForm")
    public String addBuyerForm(Model model){
        model.addAttribute("buyer", new Buyer());
        model.addAttribute("buyerId", new Buyer().getBuyerId());
        return "add-buyer";
    }

    @PostMapping("/addBuyer")
    public String postBuyerForm(@ModelAttribute("buyer") Buyer buyer, Model model){
        buyerService.saveBuyer(buyer);
        model.addAttribute("buyer", new Buyer());
        return "redirect:/";
    }


    @GetMapping("/deleteBuyer")
    public String deleteBuyer(@RequestParam("buyerid") Long id){
        buyerService.deleteBuyer(id);
        return "redirect:/";
    }


} 

2 个答案:

答案 0 :(得分:3)

我对Thymeleaf了解不多,但您可以更简单地将前端代码从表单更改为基本链接:

    <c:url var="deleteBuyer" value="/DeleteBuyer">
        <c:param name="buyerId" value="${buyer.buyerId}" />
    </c:url>
    <a class="simpleLink" href="${deleteBuyer}">delete</a>

在你的控制器中处理它:

`   @GetMapping("/DeleteBuyer")
    public String deleteAnswer(@RequestParam("buyerId") int theId) {

        buyerService.deleteBuyer(theId);
        return "redirect:/";
    }`

我希望这对你有所帮助。

答案 1 :(得分:1)

更改

<form th:action="@{/deleteBuyer}" th:object="${buyer}" method="post">

GET符合Spring请求映射

<form th:action="@{/deleteBuyer}" th:object="${buyer}" method="get">

以下是HTTP Methods