Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring:请求方法';邮政';不支持_Java_Spring Mvc_Http Post - Fatal编程技术网

Java Spring:请求方法';邮政';不支持

Java Spring:请求方法';邮政';不支持,java,spring-mvc,http-post,Java,Spring Mvc,Http Post,浏览了论坛,但没有找到解决我问题的方法。共有2个页面:index.jsp-起始页面,其中包括要填充的表单和结果列表;edit.jsp-允许编辑index.jsp提供的结果列表中任何行的数据。 当我填写表单时,所有数据子提交成功,当我尝试编辑结果列表中的任何行时,我重定向到edit.jsp,但如果我提交更改,将引发异常:HTTP状态405-请求方法“POST”不受支持。如果您能告诉我如何处理这个问题,我将不胜感激 index.jsp <%@ page contentType="text/ht

浏览了论坛,但没有找到解决我问题的方法。共有2个页面:index.jsp-起始页面,其中包括要填充的表单和结果列表;edit.jsp-允许编辑index.jsp提供的结果列表中任何行的数据。 当我填写表单时,所有数据子提交成功,当我尝试编辑结果列表中的任何行时,我重定向到edit.jsp,但如果我提交更改,将引发异常:HTTP状态405-请求方法“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时,您的URL变成
/edit/{someid}
,并且您的edit.jsp表单被加载。当您编辑数据并单击submit时,您的URL将变成
/edit/{someid}/edited
,映射将匹配使用GET方法的
/edit/{someid}
处理程序方法,这就是为什么会出现错误的原因

要解决此问题,请在edit.jsp中简单地向操作添加反斜杠,
action=“/edited”


希望能有所帮助

非常感谢!它起作用了!我浪费了一天时间试图找出它的毛病。
<%@ 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
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";
    }
}