1

I have problem with Spring MVC project I get "HTTP Status 405 - Request method 'POST' not supported" when I'm trying to call post method in controller. I'm not using spring security. Return "index" is base jsp and base on "view" attribute it change view. Someone can find what I'm doing wrong?

Controller:

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

A form object:

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

1

Your form mapping is not aligned with your form action, the form action is

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

your class level mapping is rent a method level mapping is create so the only thing that can precede it in the request is the context path, yet you have ${pageContext.request.contextPath}/rental

you have probably doubled your context (both hard-coded) and via variable, so your request is getting caught by some mapping accepting only GET requests

Sign up to request clarification or add additional context in comments.

2 Comments

I have declared /rental in web.xml <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/rental/*</url-pattern> </servlet-mapping> In other controllers everything works fine. Get method is working in RentController.
I've changed path for post request: @RequestMapping(value = "create/post/{carId}", method = RequestMethod.POST), now I get 404 error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.