2015-02-09 109 views
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; 
} 
} 

回答

1

您的形式映射沒有與形式的行動一致,表單動作是

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

你的職業等級映射是rent方法級映射爲create,這樣可以在請求它之前的唯一的事情是上下文路徑,但你有${pageContext.request.contextPath}/rental

你可能已經翻了一番您的環境(硬編碼),並通過可變的,所以你的要求越來越一些映射抓到只接受GET請求

+0

我有在web.xml中聲明/租用 MVC-調度 /租用/ * 在其他控制器一切正常。 Get方法在RentController中工作。 – cris135 2015-02-09 18:38:37

+0

我已更改發佈請求的路徑: @RequestMapping(value =「create/post/{carId}」,method = RequestMethod.POST),現在我得到404錯誤 – cris135 2015-02-09 18:44:54

相關問題