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

时间:2015-02-09 17:51:38

标签: java spring spring-mvc http-status-code-405

我遇到Spring MVC项目的问题" HTTP状态405 - 请求方法' POST'不支持"当我试图在控制器中调用post方法时。我没有使用弹簧安全装置。返回"索引"是基础jsp,基于"查看"归因于它改变视图。有人能找到我做错的事吗?

控制器:

@Controller
@RequestMapping(value = "rent")
public class RentController {

@Autowired
CarBean carBean;

@RequestMapping(value = "create/{carId}", method = RequestMethod.POST)
public String makeRentPost(@ModelAttribute("makeRent") @Valid RentForm rent, @PathVariable("carId") Long carId, BindingResult result, ModelMap model) {
    if (!result.hasErrors()) {
        model.addAttribute("makeRent", new RentForm());
        model.addAttribute("view", ViewEnums.MAKERENT.toString());
    } else {
        model.addAttribute("view", ViewEnums.MAIN.toString());
    }
    return "index";
}


@RequestMapping(value = "create/{carId}", method = RequestMethod.GET)
public String makeRent(@PathVariable("carId") Long carId, ModelMap modelMap) {
    modelMap.addAttribute("makeRent", new RentForm());
    modelMap.addAttribute("car", carBean.get(carId));
    modelMap.addAttribute("view", ViewEnums.MAKERENT.toString());
    return "index";
}
}

JSP:

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
 <%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>

<div id="featured" class="container">
        <div id="box1">
            <img src="${car.photoURL}" alt="" height="220" width="300"/>

            <h2 class="subtitle">${car.brand} ${car.model}</h2>
            <ul>
                <li><b><s:message code="car.year"/> </b>${car.year}</li>
                <li><b><s:message code="car.engineCapacity"/> </b>${car.engineCapacity}</li>
                <li><b><s:message code="car.doorsNumber"/> </b>${car.doorsNumber}</li>
                <li><b><s:message code="car.color"/> </b>${car.color}</li>
                <li><b><s:message code="car.passengersNumber"/> </b>${car.passengersNumber}</li>
                <li><b><s:message code="car.segment"/> </b>${car.segment}</li>
                <li><b><s:message code="car.gearbox"/> </b>${car.gearbox}</li>
                <li><b><s:message code="car.engine"/> </b>${car.engine}</li>
            </ul>
        </div>
</div>

<form:form action="${pageContext.request.contextPath}/rental/rent/create/${car.id}" method="POST" commandName="makeRent">
    <ul style="list-style-type:none">
        <li><s:message code="rent.startDate"/> <form:input path="startDate" id="date-pick-start" /> <form:errors path="startDate"/></li>
        <li><s:message code="rent.endDate"/> <form:input path="endDate" id="date-pick-end" /> <form:errors path="endDate"/></li>
        <li><input type="submit" value="Dodaj"/></li>
    </ul>
</form:form>

表单对象:

public class RentForm {

@NotBlank
private String startDate;
@NotBlank
private String endDate;

public String getStartDate() {
    return startDate;
}

public void setStartDate(String startDate) {
    this.startDate = startDate;
}

public String getEndDate() {
    return endDate;
}

public void setEndDate(String endDate) {
    this.endDate = endDate;
}
}

1 个答案:

答案 0 :(得分:1)

您的表单映射与表单操作不一致,表单操作是

${pageContext.request.contextPath}/rental/rent/create/${car.id}

你的类级别映射是rent方法级别映射是create所以在请求中唯一可以在它之前的是上下文路径,但是你有${pageContext.request.contextPath}/rental

你可能已经加倍了你的上下文(硬编码)和变量,所以你的请求被一些只接受GET请求的映射捕获