1
我有Spring MVC項目的問題當我試圖在控制器中調用post方法時,我得到「HTTP Status 405 - Request method'POST'not supported」。我沒有使用彈簧安全。返回「索引」是基於jsp,並基於它的「view」屬性改變視圖。有人可以找到我做錯了什麼?Spring MVC HTTP狀態405 - 不支持請求方法'POST'
控制器:
@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;
}
}
我有在web.xml中聲明/租用 MVC-調度 servlet的名稱> /租用/ * URL模式> servlet的映射> 在其他控制器一切正常。 Get方法在RentController中工作。 –
cris135
2015-02-09 18:38:37
我已更改發佈請求的路徑: @RequestMapping(value =「create/post/{carId}」,method = RequestMethod.POST),現在我得到404錯誤 – cris135 2015-02-09 18:44:54