【问题标题】:Spring: Request method 'POST' not supportedSpring:不支持请求方法“POST”
【发布时间】:2014-11-28 21:10:31
【问题描述】:

浏览了论坛,但没有找到可以解决我的问题的解决方案。有 2 个页面: index.jsp - 起始页面,其中包括要填充的表单和结果列表; edit.jsp - 允许从 index.jsp 提供的结果列表中编辑任何行的数据。 当我填写成功提交的所有数据表单时,当我尝试编辑结果列表中的任何行时,我重定向到 edit.jsp 但如果我提交更改,则会引发异常:HTTP Status 405 - Request method 'POST'不支持。我会很感激任何想法如何处理这个问题。

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";
    }
}

【问题讨论】:

    标签: java spring-mvc http-post


    【解决方案1】:

    您的问题是您在表单中使用相对映射,当您单击编辑时,您的 URL 变为 /edit/{someid} 并且您的 edit.jsp 表单已加载。当您编辑数据并单击提交时,您的 URL 将变为 /edit/{someid}/edited,映射将匹配 /edit/{someid} 处理程序方法,女巫正在使用 GET 方法,这就是您收到错误的原因。

    为了解决这个问题,在你的 edit.jsp 中简单地添加一个反斜杠,action="/edited"

    希望对你有帮助

    【讨论】:

    • 非常感谢!有用!我浪费了一天的时间试图找出问题所在。
    猜你喜欢
    • 2016-08-28
    • 2016-09-13
    • 2018-06-03
    • 2015-05-28
    • 2014-06-10
    • 2020-03-31
    • 2016-02-21
    • 1970-01-01
    相关资源
    最近更新 更多