spring web application - html button - post form form form - 404 ERROR

时间:2016-07-27 11:48:09

标签: java html spring jsp spring-mvc

我试图通过按钮“onClick”方法从JSP视图向Controller发送post请求,但是我得到404错误,RequestMapping没有签名,为什么会这样?

HomeController中:

@RequestMapping(value = "/showSelectedRequest/{id}", method = RequestMethod.POST)
public String loadRequestProducts(@PathVariable("id") int id, Model model) {

    logger.debug("HomeController.RequestIdSelected() - Start");
    logger.debug("HomeController.RequestIdSelected: id: " + id);
    model.addAttribute("RequestIdSelected", id);

    logger.debug("HomeController.RequestIdSelected() - Done");
    return "/home";

}

回到Home.jsp:

<form action="${contextPath}/requestlist" method="post">    
    <table class="table table-sm">
        <thead class="thead-inverse">
            <tr>
                <th>
                    Id
                </th>
                <th>
                    Name
                </th>
                <th>
                    Show request
                </th>
            </tr>
        </thead>

        <c:forEach items="${requestDTOList}" var="requestDTO">
            <tr>
                <td>
                    ${requestDTO.getId()}
                </td>
                <td>
                    ${requestDTO.getName()}
                </td>
                <td>
                    <button class="btn btn-info"  onclick="post(/showSelectedRequest/${requestDTO.getId()})">Query</button>
                    <input type="hidden"  name="${_csrf.parameterName}"   value="${_csrf.token}"/>
                </td>
            </tr>
        </c:forEach>
    </table>
</form>

1 个答案:

答案 0 :(得分:1)

当您有表单时,操作字段是您单击“提交”类型的输入时将执行的操作。

作为解决方案,您可以修改代码,如下所示:

<form action="${contextPath}/showSelectedRequest/${requestDTO.getId()}" method="post">  
    // Form elements ...
    <input type="submit" value="Query" />
</form>