aspose file tools*
The moose likes Struts and the fly likes Calling an action using DWR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Calling an action using DWR" Watch "Calling an action using DWR" New topic
Author

Calling an action using DWR

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

My application has the infamous AddToShoppingCart action (wich takes an isbn as a parameter , adds it to the shopping cart , put simple message in the request scope and then redisplay the page).
Now I want to call this action using DWR but StrutsCreator is not useful here , so any tips or tricks ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I use DWR to implement AJAX in my applications and really like it. Calling an Action class from DWR is not really a good fit, though. There's nothing stopping you from declaring an Action class's execute method in DWR and having DWR call it. It doesn't really make much sense to do so, though, since an Action class wasn't designed to operate that way.

My suggestion would be to refactor your application a bit. Take the logic you're interested in from your Action class and put it in a method in some other utility class or Business Delegate class. Then call that method from both your Action class and DWR.


Merrill
Consultant, Sima Solutions
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

What do you think of this util method :
public void addBookToCart(HttpServletRequest req) {
// get session object
// add object to cart
// add message to req
}
In this case, the method has a dependency on HttpServletRequest , is it ok (from good design view point) ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
First of all, you're going to need to pass in some information about the book. I'd suggest a Book object. In your client-side JavaScript you can create a JavaScript object that has the same properties as your Java Book object and DWR will convert it for you.

As you may know, when you specify a parameter type of HttpServletRequest or HttpSession, the DWR servlet automatically populates these parameters when it calls your method.

Secondly, since you're communicating directly with the HTML page and not refreshing it, it doesn't do any good to put a message in the session. You need to pass back a message to be displayed immediately.

With these modifications, your method would now look something like this:


As far as the dependency on the HttpServletRequest object goes, you don't have a choice. If you're going to put something in the session, how else are you going to do it? From an architectural point of view, this method belongs in the controller space, rather than the model space. Therefore, dependencies on view-specific objects like HttpServletRequest are OK.
[ October 08, 2006: Message edited by: Merrill Higginson ]
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Why to pass Book object as a parameter ?
HttpServletRequest already contains a parameter called isbn (request.getParameter("isbn")).
In addition , my Struts action are under Spring control , so I have to use Spring creator ?
Secondly, since you're communicating directly with the HTML page and not refreshing it

May I ask you what does exactly mean ?
<Please note, I'm trying to construct a tutorial about using DWR with many frameworks like Spring, Struts, Hibernate and JSF , and I will post it here after it is complete so forgive me if I'm bothering you>
Thanks for help
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
DWR uses a completely different paradigm than traditional web applications. When using DWR, you don't pass values through the HttpServletRequest. You pass values through parameters, just as you would in calling a Java method directly. It's almost as though DWR opens up a way to call a Java method directly from a JavaScript function.

Furthermore, with DWR and AJAX It's no longer a matter of submitting a page, having the server do something, and then having the server send back a new page.

With AJAX, you write JavaScript code that initiates a call to the server that returns a response directly to the HTML web page. The page is not refreshed. Any changes made to the page have to by done by JavaScript manipulation of the Document Object Model (DOM).

The example you gave of putting something in a shopping cart is not a good one because the same function can be done just as efficiently and easily using the traditional approach. DWR/AJAX is best used for things that cannot be done as efficiently with the traditional approach, such as populating the options of a second select box, based on the value selected in the first.

I'd suggest you spend a little more time with the DWR documentation and examples. If you're going to write a tutorial on DWR, you're going to need a thorough understanding of it, and to get that you'll need to work through some more examples and other tutorials.

This link is a good one to get started with.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling an action using DWR