3

Am new to Spring Web MVC and developing a web application. There is a case where i need to pass a data value to Controller. (Note: Here the data value is a value of bean object )

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 above image, if i click on "Add item to Cart" the respective item number should be passed to 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 am able to pass the value. But as per standards of GET and POST methods (described here ), i think am doing wrong. Please suggest is there any other way to pass the data in to the POST method. Please suggest me. Thanks in advance

(This is first time am posting here, excuse me if there are any mistakes )

Community
  • 1
  • 1
swagath001
  • 79
  • 1
  • 4
  • 9

3 Answers3

1

Yes you are correct you should use POST request to do any kind of DML operation on server. GET should be used for data fetch operations only. Reason is to avoid the doubly form submission which you can do with F5 or double click of submit button. You should use method=RequestMethod.POST instead of GET. See Spring MVC Post Request

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314
1

You have add to cart button so on clicking on it you can have Ajax call and use appropriate request mapping in controller.

<a href="javascript:addItemToCart(${item.itemNo},"passURL");">Add to cart item?</a>

In Javascript, you can pass parameter and give 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");
    }
  });

}
Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
1

Put everything inside a form (using Spring's <form:form> tag). This will POST the form to the same URL at which it is displayed. If you want to POST to a different URL, then set the action attribute of the form.

Change your Add item to Cart link to a submit button, as you should not change data with a GET. You can style it as you wish and make it look like a link if you want.

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 with a request mapping like this:

@RequestMapping(value="additemtocart", method=RequestMethod.POST, params={"itemNumber"})

You should then use the Post-Redirect-Get pattern to redirect back to the product list. See flashAttributes in Spring MVC documentation.

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • Do you want me to change entire link to button ? And how can handler method with value "additemtocart" would be called up on clicking on submit button. can you specify that one too ? – swagath001 Nov 19 '14 at 20:09
  • Edited my answer. Make sure everything goes in a form. This will make a POST request to the server at the same URL that the page is displayed as. You can change the `action` attribute of the form for a different URL. – Neil McGuigan Nov 19 '14 at 20:13