Guyb Guyb - 3 months ago 14
HTML Question

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

I`m trying to send post request from JSP view to Controller by Button "onClick" method but i getting 404 Error that the RequestMapping is not sign, why is that?

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>

Answer

When you have a form, the action field is what will be executed when you will click on the input of type "submit".

As a solution you can modify your code as follow:

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