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

标签 java javascript spring hibernate methods

我收到此错误:HTTP 状态 405 - 不支持请求方法“POST”

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

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

jsp页面的一部分

<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>

Controller 的一部分:

@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

最佳答案

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

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

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>

关于java - HTTP 状态 405 - 不支持请求方法 'POST' (Spring MVC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11145884/

相关文章:

javascript - ember.js 是否足够稳定,可以在现实世界的项目中使用?

javascript - 计算预定义输入属性的输入总数

java - Spring @Transaction注解和异常处理

java - JTextField/JPasswordField 的提示

java - 如何使用 Glide 通过字节数组加载图像?

java - 是否可以使用Java/Kotlin在Android MotionLayout中从“开始”状态切换到“结束”状态?

java - Hibernate 多对多 - 保存时未填充连接表

javascript - jquery 节点数组

java - Mac OS X 上的 STS 3.6.4 启动时崩溃

spring - Oauth2 授权类型和良好文档的实时示例,Oauth2 与 Spring MVC 的示例