HTTP状态405 - 不支持请求方法'POST'(Spring MVC) [英] HTTP Status 405 - Request method 'POST' not supported (Spring MVC)

查看:200
本文介绍了HTTP状态405 - 不支持请求方法'POST'(Spring MVC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误: HTTP状态405 - 请求方法'POST'不支持



我是什么试图做的是制作一个下拉框,根据在另一个下拉框中选择的另一个值填充的表单。例如,当我在 customerName 框中选择名称时,应运行.jsp页面中的 onChange 函数,提交页面,然后再次载入 customerCountry 框中的对应值。



但是,我收到此HTTP状态405错误。我已经在互联网上搜索了一个解决方案,但一直没有找到任何帮助。以下是我的代码的相关部分:

jsp页面的一部分

 < HTML> 
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< title>在此插入标题< / title>
< style>
.error {color:red; }
< / style>

< script>
函数repopulate(){
document.deliveryForm.submit();


函数setFalse(){
document.getElementById(hasId)。value =false;
document.deliveryForm.submit();
// document.submitForm.submit(); (这是导致错误)

}
< / script>

< / head>
< body>

< h1>建立新交付< / h1>

< c:url var =saveUrlvalue =/ test / delivery / add/>
< form:form modelAttribute =deliveryDtoAttributemethod =POSTaction =$ {saveUrl}name =deliveryForm>
< table>


< tr>
< td>< form:hidden id =hasIdpath =hasCustomerNamevalue =true/>< / td>
< / tr>

< tr>
< td>客户名称< / td>
< td>< form:select path =customerNameonChange =repopulate()>
< form:option value =label =--- Select ---/>
< form:options items =$ {customerNameList}/>
< / form:select>
< / td>
< td>< form:errors path =customerNamecssClass =error/>< / td>
< / tr>

< tr>
< td>客户国家< / td>
< td>< form:select path =customerCountry>
< form:option value =label =--- Select ---/>
< form:options items =$ {customerCountryList}/>
< / form:select>
< / td>
< td>< form:errors path =customerCountrycssClass =error/>< / td>
< / tr>

< / form:form>

< form:form name =submitForm>
< input type =buttonvalue =SaveonClick =setFalse()/>
< / form:form>

< / body>
< / html>

控制器部分:

  @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()));
返回新送货;
}

//如果hasId = true,我想输入这个方法,这意味着CustomerName
//下拉列表中的值被选中。这应该将CountryList设置为相应的值
//从数据库中。我想要这个post方法由jsp页面中的onChange触发

@RequestMapping(value =/ add,method = RequestMethod.POST,params =hasCustomerName = true)
public String postDelivery(
@ModelAttribute(deliveryDtoAttribute)DeliveryDto deliveryDto,
BindingResult结果,ModelMap模型){


model.addAttribute(deliveryDtoAttribute, deliveryDto);

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

返回新送货;
}

//只有在jsp页面中点击保存按钮才能进入下一个后期处理方法
$ b $ @RequestMapping(value =/ add ,method = RequestMethod.POST,params =hasCustomerName = false)
public String postDelivery2(
@ModelAttribute(deliveryDtoAttribute)@Valid DeliveryDto deliveryDto,
BindingResult结果,ModelMap模型){

if(result.hasErrors()){

model.addAttribute(deliveryDtoAttribute,deliveryDto);

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

返回新送货;
} else {

递送递送= new Delivery();

// Setter设置交付值

返回redirect:/ mis / home;
}

}

我怎么得到这个错误?任何帮助将非常感激!谢谢
$ b

编辑:已将 hasId 更改为 hasCustomerName code>。尽管我仍然得到了 HTTP Status 405 - Request method'POST'不支持错误。

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



// D

解决方案

HTTP错误。
$ b 在保存按钮触发的 setFalse()函数中,我的代码试图提交表单包含按钮。

  function setFalse(){
document.getElementById(hasId)。value =false;
document.deliveryForm.submit();
document.submitForm.submit();

当我删除 document.submitForm.submit(); 它的工作原理:

 函数setFalse(){
document.getElementById(hasId)。value = 假;
document.deliveryForm.submit()

@RogerLindsjö感谢您发现我的错误我没有传递正确的参数!


Im getting this error: HTTP Status 405 - Request method 'POST' not supported

What I am trying to do is make a form with a drop down box that get populated based on the other value selected in another drop down box. For example when I select a name in the customerName box the onChange function in the .jsp page should be run and the page submitted then loaded again with the corresponding values in the customerCountry box.

however I'm getting this HTTP Status 405 error. I have searched the internet for a solution but haven't been able to find anything that helped. Here is the relevant parts of my code:

part of jsp page

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

part of 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";
        }

    }

How come I get this error? Any help would be much appreciated! Thanks

EDIT: Changed hasId to hasCustomerName. I still get the HTTP Status 405 - Request method 'POST' not supported error though.

EDIT2: Commented out the line in the setFalse function that was causing the error

// D

解决方案

I found the problem that was causing the HTTP error.

In the setFalse() function that is triggered by the Save button my code was trying to submit the form that contained the button.

        function setFalse(){
            document.getElementById("hasId").value ="false";
            document.deliveryForm.submit();
            document.submitForm.submit();

when I remove the document.submitForm.submit(); it works:

        function setFalse(){
            document.getElementById("hasId").value ="false";
            document.deliveryForm.submit()

@Roger Lindsjö Thank you for spotting my error where I wasn't passing on the right parameter!

这篇关于HTTP状态405 - 不支持请求方法'POST'(Spring MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆