0

I am working on an basic Shopping Cart assignment in Springs. The functionalities of the assignment are just to display a given set of products - with info (with an add to cart button). My problem is, integrating the Add button click with the shopping cart display in the JSP page.

How to use application session to obtain and persist information across multiple "Add" button clicks?

Here is the code snippet:

<script type="text/javascript"> 
var noOfClicks = 0; // Used to count the number of Clicks. 
function button_click() { 
  noOfClicks++; // On click of the button the value is incremented.        
  document.getElementById("item").value = noOfClicks; 
} 
</script>

How do I check if the button is clicked and add it to cart with its attributes (Quantity, Price etc) in a separate summary table?

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
lesac
  • 25
  • 1
  • 8
  • Can you show us what have you tried ? – Hardik Mishra Jul 10 '12 at 05:46
  • @HardikMishra: How do I check if the button is clicked and add it to cart with its attributes (Quantity, Price etc) in a separate summary table? – lesac Jul 10 '12 at 05:56
  • What you want to achieve is not few lines of code , you may need to use a servlet and perform session management to persist the shopping cart. – Ravi Jain Jul 10 '12 at 06:01

1 Answers1

0

You can use session scoped spring beans for shopping cart. It means that this bean is created when a new HttpSession is created and preserved as long as the HttpSession is valid. Spring uses AOP to extract the sessionId from the httprequest and manage the lifecycle of the bean.

http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch04s04.html#beans-factory-scopes-global-session

[Example] http://web.archive.org/web/20080828160606/http://www.memestorm.com/blog/session-scoped-beans-in-spring-20/

In the action class of 'Add' activity, access the session object and add the selected item to the session cart object.

devang
  • 5,376
  • 7
  • 34
  • 49