| Author |
Reimplementing parts of my app with DWR
|
Hussein Baghdadi
clojure forum advocate
Bartender
Joined: Nov 08, 2003
Posts: 3359
|
|
Hi. I'm trying to reimplement parts of my web app using DWR. ShoppingCart class has the following method : public void addToCart(BookItem bookItem) Here is a snippet from AddToCartAction : My problem is how to code the same previous code in JavaScript ? No way, right ? I have to create a new method with the following signature : public void addToCart(String isbn) Right ? [ January 24, 2007: Message edited by: Bear Bibeault ]
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56191
|
|
Your question is vague beyond comprehension. Are you talking about server-side Java methods or Javascript? Generally DWR creates the Javascript functions that serve as the proxy stub for the corresponding server-side Java methods.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56191
|
|
|
I also updated your title to be more accurate.
|
 |
Hussein Baghdadi
clojure forum advocate
Bartender
Joined: Nov 08, 2003
Posts: 3359
|
|
Sorry, I mean : Inside my Struts Action class, I'm able to get the Book from the session object and then create a BookItem. Check the signature please : public void addToCart(BookItem bookItem) Now, if I want to use DWR, my JavaScript code should call the previous method but I don't know how ? Yes, DWR will expose Cart object so I can call addToCart(BookItem) but how to access Book instance (from JavaScript) which it is stored in session scoped object and then creates a BookItem instance ? Sorry but Am I clear ? [ January 24, 2007: Message edited by: John Todd ]
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
I really think you need to not jump into it, but actually sit down an work with the examples. http://code.google.com/webtoolkit/documentation/examples/ It is like you just jumped into the middle of the ocean and you do not know how to swim. Eric
|
 |
Hussein Baghdadi
clojure forum advocate
Bartender
Joined: Nov 08, 2003
Posts: 3359
|
|
Thanks Eric but may I ask you why you referenced to GWT ? (I'm using DWR). Sorry if I missed something
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56191
|
|
Eric may have pointed you to the wrong tutorial, but his point is valid. With DWR, you do not define the Javascript proxy functions, DWR does that for you. You really need to get more familiar with how DWR works. If you can't find an online tutorial, you can obtain the Manning Ajax in Practice book in which I discuss DWR in chapter 4. The book is unpublished as of yet but can be obtained via MEAP (Manning Early Acces Program).
|
 |
Hussein Baghdadi
clojure forum advocate
Bartender
Joined: Nov 08, 2003
Posts: 3359
|
|
Guys, you absolutley have the right to kick my *** but ... I have been reading some tutorials about DWR (IBM dW, dev2Dev, java.net)
DWR is an engine that exposes methods of server-side Java objects to JavaScript code.
for each exposed class, DWR dynamically generates JavaScript to include in your Web page. The generated JavaScript contains stub functions that represent the corresponding methods on the Java class and also performs XMLHttpRequests behind the scenes.
With DWR, you do not define the Javascript proxy functions, DWR does that for you.
When I click on add to cart button, a JavaScript function should be executed, something like : Sorry, I feel I'm repeating myself but I'm unable to understand this. Well, kick me !
|
 |
Carol Enderlin
drifter
Ranch Hand
Joined: Oct 10, 2000
Posts: 1348
|
|
Why don't you tell us what steps you have taken to get to this point. What configuration have you done (web.xml, dwr.xml)? Do you see your class on the dwr test page? The test page shows the javascript to add to your page, did you do that? Did you work through these? http://getahead.ltd.uk/dwr/getstarted http://getahead.ltd.uk/dwr/browser/intro [ January 28, 2007: Message edited by: Carol Enderlin ]
|
 |
Hussein Baghdadi
clojure forum advocate
Bartender
Joined: Nov 08, 2003
Posts: 3359
|
|
Ok, I have a Struts action , AddToCartAction : I want to reimplement this using DWR, I don't want the previous action to be executed. When the user clicks on 'Add To Cart' button at details.jsp page, addToCartHandler( ) JavaScript function should run. I exposed my cart object under name Cart in dwr.xml : My whole problem is how to get the Book instance from the session object inside my JavaScript code (Shopping.js) ? After reading DWR docs (http://getahead.ltd.uk/dwr/server/javaapi), I got this : Creat a class : Then inside my JavaScript : Service.addTocart( ); So ??
|
 |
Carol Enderlin
drifter
Ranch Hand
Joined: Oct 10, 2000
Posts: 1348
|
|
Lots of questions or actually question marks. Have you tried something (I mean hooking up your javascript to actually call your class with the dwr-generated javascript)? What are you stuck on? Can you do something simpler with DWR? I don't think you answered any of my questions. Oh wait, you said you put your cart object in dwr.xml, but the code you showed is for a service class. My turn for a question mark...? Your javascript doesn't have access to the session, but your class does. [ January 28, 2007: Message edited by: Carol Enderlin ]
|
 |
Carol Enderlin
drifter
Ranch Hand
Joined: Oct 10, 2000
Posts: 1348
|
|
This page describes two ways to get servlet objects (e.g. session) in DWR-exposed methods: Accessing Servlet Objects 1. Use WebContextFactory to get WebContext. From WebContext you can get request, etc. 2. Add the object of interest to the signature "and upon a call of the method it will fill it in automagically."
|
 |
Hussein Baghdadi
clojure forum advocate
Bartender
Joined: Nov 08, 2003
Posts: 3359
|
|
Have you tried something (I mean hooking up your javascript to actually call your class with the dwr-generated javascript)? What are you stuck on?
Yes, using http://localhost:8080/store/dwr/ Sure, I can create a JavaScript version of BookItem with JSON but this is so helpful since the real application will get its BookItem from the session.
Oh wait, you said you put your cart object in dwr.xml, but the code you showed is for a service class.
Sorry, this is my fault Service class is a walk around way to my problem and has nothing to do with the original post (a solution).
Add the object of interest to the signature "and upon a call of the method it will fill it in automagically
Please correct me if I'm wrong but we can't use any object, just the standard HTTP servlet objects. [ January 30, 2007: Message edited by: John Todd ] [ January 30, 2007: Message edited by: John Todd ]
|
 |
Carol Enderlin
drifter
Ranch Hand
Joined: Oct 10, 2000
Posts: 1348
|
|
Originally posted by John Todd: Please correct me if I'm wrong but we can't use any object, just the standard HTTP servlet objects.
You can pass objects in. You have to add a converter to dwr.xml for the classes, for example javabean converter, just like you would if you were returning objects from a dwr call: <convert converter="bean" match="com.foo.MyClass"/> DWR has utility javascript to help populate the objects from form data. For tutorial with example see Using DWR (Direct Web Remoting) for Building AJAX Applications Beyond the object question, do you still have issues or questions? [ January 30, 2007: Message edited by: Carol Enderlin ]
|
 |
 |
|
|
subject: Reimplementing parts of my app with DWR
|
|
|