Passing entity data from JSP to Spring Controller column method instead of GET method
New to Spring Web MVC and Web Application Development. There is a case where I need to pass a data value to a controller. (Note: here the data value is the bean value)
Item Number Item Name Description Price
Item1018 Item1 Desc1 5.0 Add item to Cart
Item1019 Item2 Desc2 2.0 Add item to Cart
As shown in the picture above, if I click the Add Item to Cart button, the corresponding item number has to be passed to the controller.
viewmenu.jsp
<c:forEach items="${model.itemlists}" var="item">
<tr>
<c:if test = "${item.status == 'available'}">
<td><c:out value="${item.itemNo}"/> </td>
<td><c:out value="${item.itemName}"/></td>
<td><c:out value="${item.description}"/></td>
<td><c:out value="${item.price}"/></td>
<td><a href="<c:url value="additemtocart">
<c:param name='itemNumber' value="${item.itemNo}"/>
</c:url>">Add item to Cart</a> </td>
</c:if>
</tr>
</c:forEach>
CustomerController.java method
@RequestMapping(value = "additemtocart",method = RequestMethod.GET)
public ModelAndView addItemToCart(@RequestParam("itemNumber") String itemno ) throws ClassNotFoundException, SQLException {
System.out.println("Username test in customer controller: "+userName);
}
The code works and can pass a value. But according to the standards for GET and POST methods (described here here ), I think I am doing it wrong. Please suggest another way to pass data to the POST method. Please suggest me. thanks in advance
(This is the first time I post here, sorry if there are any errors)
source to share
Put everything inside the form (using Spring tag <form:form>
). This will POST the form with the same url it is displayed on. If you want to POST to a different url, set the action
form attribute .
Change the link Add item to Cart
to the submit button as you shouldn't change the data with GET. You can mark it however you like and make it look like a link if you like.
Your button should look like this:
<button type=submit name=itemNumber value=xxx>Add item to Cart</button>
In your controller, you should have a handler method displaying the request like this:
@RequestMapping(value="additemtocart", method=RequestMethod.POST, params={"itemNumber"})
Then you must use the Post-Redirect-Get pattern to redirect back to the product listing. See FlashAttributes in the Spring MVC documentation.
source to share
Yes you are right, you should use POST request to perform any DML operation on the server. GET should only be used for data retrieval operations. The reason is to avoid double submission of the form, which you can do with F5 or double click on the submit button. You should use = RequestMethod.POST method instead of GET. See Spring MVC Post Request
source to share
You have a button to cart, so when you click on it, you can receive an Ajax call and use the appropriate request mapping in the controller.
<a href="javascript:addItemToCart(${item.itemNo},"passURL");">Add to cart item?</a>
In Javascript, you can pass a parameter and specify the type as POST for ajax request.
JavaScript code
function addItemToCard(itemNo,targetURL){
$.ajax(function(){
url:targetURL,
type:"POST",
data:"itemNumber":itemNo,
success:function(response){
alert("Added successfully");
}
});
}
source to share