简体   繁体   中英

Spring: Request method 'POST' not supported

Browsed the forum but didn't find solution which can solve my problem. There are 2 pages: index.jsp - start page which includes form to be populated and the list of results; edit.jsp - allows to edit data of any row from the list of results provided by index.jsp. When I fill in the form all the data subbmitted successfully, when I try to edit any row in the list of results I redirected to edit.jsp but if I submit the changes an exception is thrown: HTTP Status 405 - Request method 'POST' not supported. I would appreciate any idea how to treat the issue.

index.jsp

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

<html>
  <head>
    <title></title>
  </head>
  <body>
    <form:form method="post" action="add" modelAttribute="account">
        <table>
        <tr>
            <td><form:label path="number">Number</form:label></td>
            <td><form:input path="number"/></td>
        </tr>
        <tr>
            <td><form:label path="amount">Amount</form:label></td>
            <td><form:input path="amount"/></td>
        </tr>
        <tr>
            <td><form:label path="currency">Currency</form:label></td>
            <td><form:input path="currency"/></td>
        </tr>
        <tr>
            <td><form:label path="date">Date</form:label></td>
            <td><form:input path="date" type="date"/>
        </tr>
        </table>
        <input type="submit" value="Submit"/>
    </form:form>
    <table>
        <tr border="1">
            <td>Number</td>
            <td>Amount</td>
            <td>Currency</td>
            <td>Date</td>
        </tr>
        <c:forEach items="${listOfAccounts}" var="items">
        <tr border="1">
            <td>${items.number}</td>
            <td>${items.amount}</td>
            <td>${items.currency}</td>
            <td>${items.date}</td>
            <td><a href="<c:url value='edit/${items.id}'/>">edit</a></td>
        </tr>
        </c:forEach>
  </body>
</html>

edit.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>Edit Account</title>
</head>
<body>
    <form:form modelAttribute="account" method="post" action="edited">
        <form:hidden path="id" value="${account.id}"></form:hidden>
        <form:label path="number">Number</form:label>
        <form:input path="number" value="${account.number}"/><br>
        <form:label path="amount">Amount</form:label>
        <form:input path="amount" value="${account.amount}"/><br>
        <form:label path="currency">Currency</form:label>
        <form:input path="currency" value="${account.currency}"/><br>
        <form:label path="date">Date</form:label>
        <form:input path="date" type="date" value="${account.date}"/>
        <input type="submit" value="Submit"/>
    </form:form>
</body>
</html>

Controller.java

@Controller
public class AccountController {

    @Autowired
    private AccountService accountService;
    private Account account;

    @RequestMapping(value="/", method = RequestMethod.GET)
    public String welcomeMethod(ModelMap map) {
        Account account = new Account();
        map.addAttribute("account", account);
        map.addAttribute("listOfAccounts", accountService.getListOfAccounts());
        return "index";
    }

    @RequestMapping(value="add", method = RequestMethod.POST)
    public String addAccount(@ModelAttribute(value="account") Account account, ModelMap map) {
        accountService.addAccount(account);
        map.addAttribute("listOfAccounts", accountService.getListOfAccounts());
        return "index";
    }

    @RequestMapping(value="edit/{id}", method = RequestMethod.GET)
    public String editAccount(@PathVariable("id") int id, ModelMap model) {
        Account account = accountService.getAccountById(id);
        model.addAttribute("account", account);
        return "edit";
    }

    @RequestMapping(value="edited", method = RequestMethod.POST)
    public String updateAccount(@ModelAttribute(value="account") Account account, ModelMap map) {
        accountService.updateAccount(account);
        map.addAttribute("listOfAccounts", accountService.getListOfAccounts());
        return "index";
    }
}

Your problem is that you are using relative mappings in your form, when you click on edit, your URL becomes /edit/{someid} and your edit.jsp form is loaded. When you edit the data and click submit, your URL will become /edit/{someid}/edited , the mapping will match the /edit/{someid} handler method witch is using a GET method and that is why you get your error.

To solve it, in your edit.jsp simple add a backslash to action, action="/edited"

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM