首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >HTTP状态405 -不支持请求方法'POST‘(Spring MVC)

HTTP状态405 -不支持请求方法'POST‘(Spring MVC)
EN

Stack Overflow用户
提问于 2012-06-22 04:04:00
回答 7查看 125.9K关注 0票数 24

我收到这个错误:HTTP Status 405 - Request method 'POST' not supported

我正在尝试做的是创建一个带有下拉框的表单,该表单根据在另一个下拉框中选择的其他值进行填充。例如,当我在customerName框中选择一个名称时,应该运行.jsp页面中的onChange函数,并提交页面,然后使用customerCountry框中的相应值再次加载。

然而,我得到了这个HTTP状态405错误。我已经在互联网上搜索了解决方案,但没有找到任何有用的东西。以下是我的代码的相关部分:

jsp页面的一部分

代码语言:javascript
复制
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
            <style>
            .error { color: red; }
            </style>

        <script>
            function repopulate(){  
                document.deliveryForm.submit();
            }

            function setFalse(){
                document.getElementById("hasId").value ="false";
                document.deliveryForm.submit();
                // document.submitForm.submit(); (This was causing the error)

            }
        </script>

    </head>
    <body>

        <h1>Create New Delivery</h1>

        <c:url var="saveUrl" value="/test/delivery/add" />
        <form:form modelAttribute="deliveryDtoAttribute" method="POST" action="${saveUrl}" name="deliveryForm">
            <table>


                <tr>
                    <td><form:hidden id="hasId" path="hasCustomerName" value="true"/></td>
                </tr>

                <tr>
                    <td>Customer Name</td>
                    <td><form:select path="customerName" onChange="repopulate()">
                        <form:option value="" label="--- Select ---" />
                        <form:options items="${customerNameList}" />
                        </form:select>
                    </td>
                    <td><form:errors path="customerName" cssClass="error" /></td>
                </tr>

                <tr>
                    <td>Customer Country</td>
                    <td><form:select path="customerCountry">
                        <form:option value="" label="--- Select ---" />
                        <form:options items="${customerCountryList}" />
                        </form:select>
                    </td>
                    <td><form:errors path="customerCountry" cssClass="error" /></td>
                </tr>

        </form:form>

        <form:form name="submitForm">
        <input type="button" value="Save" onClick="setFalse()"/>
        </form:form>

    </body>
</html>

控制器的一部分:

代码语言:javascript
复制
@RequestMapping(value = "/add", method = RequestMethod.GET)
    public String getDelivery(ModelMap model) {
        DeliveryDto deliveryDto = new DeliveryDto();

        model.addAttribute("deliveryDtoAttribute", deliveryDto);
        model.addAttribute("customerNameList",
                customerService.listAllCustomerNames());
        model.addAttribute("customerCountryList", customerService
                    .listAllCustomerCountries(deliveryDto.getCustomerName()));
        return "new-delivery";
    }

    // I want to enter this method if hasId=true which means that a value in the CustomerName 
    // drop down list was selected. This should set the CountryList to the corresponding values 
    // from the database. I want this post method to be triggered by the onChange in the jsp page

    @RequestMapping(value = "/add", method = RequestMethod.POST, params="hasCustomerName=true")
    public String postDelivery(
            @ModelAttribute("deliveryDtoAttribute") DeliveryDto deliveryDto,
            BindingResult result, ModelMap model) {


            model.addAttribute("deliveryDtoAttribute", deliveryDto);

            model.addAttribute("customerNameList",
                    customerService.listAllCustomerNames());
            model.addAttribute("customerCountryList", customerService
                    .listAllCustomerCountries(deliveryDto.getCustomerName()));

            return "new-delivery";
    }

    // This next post method should only be entered if the save button is hit in the jsp page

    @RequestMapping(value = "/add", method = RequestMethod.POST, params="hasCustomerName=false")
    public String postDelivery2(
            @ModelAttribute("deliveryDtoAttribute") @Valid DeliveryDto deliveryDto,
            BindingResult result, ModelMap model) {

        if (result.hasErrors()) {

            model.addAttribute("deliveryDtoAttribute", deliveryDto);

            model.addAttribute("customerNameList",
                    customerService.listAllCustomerNames());
            model.addAttribute("customerCountryList", customerService
                    .listAllCustomerCountries(deliveryDto.getCustomerName()));

            return "new-delivery";
        } else {

            Delivery delivery = new Delivery();

            //Setters to set delivery values

            return "redirect:/mis/home";
        }

    }

为什么我会得到这个错误?任何帮助都将不胜感激!谢谢

编辑:hasId更改为hasCustomerName。不过,我仍然收到HTTP Status 405 - Request method 'POST' not supported错误。

EDIT2:注释掉了setFalse函数中导致错误的行

// D

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2012-06-22 17:43:31

我找到了导致HTTP错误的问题。

在由Save按钮触发的setFalse()函数中,我的代码试图提交包含该按钮的表单。

代码语言:javascript
复制
        function setFalse(){
            document.getElementById("hasId").value ="false";
            document.deliveryForm.submit();
            document.submitForm.submit();

当我删除document.submitForm.submit();时,它可以工作:

代码语言:javascript
复制
        function setFalse(){
            document.getElementById("hasId").value ="false";
            document.deliveryForm.submit()

@Roger Lindsjö感谢您发现了我的错误,因为我没有传递正确的参数!

票数 3
EN

Stack Overflow用户

发布于 2015-08-11 23:07:24

我不确定这是否有帮助,但我也有同样的问题。

您正在使用具有CSRF保护的springSecurityFilterChain。这意味着当你通过POST请求发送表单时,你必须发送一个令牌。尝试将下一个输入添加到表单中:

代码语言:javascript
复制
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
票数 22
EN

Stack Overflow用户

发布于 2015-02-25 04:53:38

检查返回的是@ResponseBody还是@ResponseStatus

我也遇到过类似的问题。我的控制器看起来像这样:

代码语言:javascript
复制
@RequestMapping(value="/user", method = RequestMethod.POST)
public String updateUser(@RequestBody User user){
    return userService.updateUser(user).getId();
}

当使用POST请求调用时,我总是得到以下错误:

HTTP状态405 -请求方法'POST‘不支持

过了一段时间,我发现这个方法实际上是被调用的,但是因为没有@ResponseBody和@ResponseStatus,Spring MVC会引发这个错误。

要解决这个问题,只需添加一个@ResponseBody

代码语言:javascript
复制
@RequestMapping(value="/user", method = RequestMethod.POST)
public @ResponseBody String updateUser(@RequestBody User user){
    return userService.updateUser(user).getId();
}

或者将@ResponseStatus添加到您的方法。

代码语言:javascript
复制
@RequestMapping(value="/user", method = RequestMethod.POST)
@ResponseStatus(value=HttpStatus.OK)
public String updateUser(@RequestBody User user){
    return userService.updateUser(user).getId();
}
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11145884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档