• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Ajax and struts integration

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can anyone suggest how to integrate Ajax and struts. Is there a plugin for struts


Thanks....
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Struts was made for the traditional "display a page, submit a form" style of web application, and as such doesn't really fit well with AJAX. I've found that for some tasks, an AJAX approach works better, and for others, the traditional approach works better. There are other frameworks that were built specifically for AJAX, such as The AJAX tag library and DWR.

I believe the best approach is to use Struts when a traditional approach is called for and another framework when AJAX is called for. This link talks about how to integrate DWR and Struts.
 
Pradeep Swaminathan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Merrill,
Actually, I am planning to implement Ajax in only in two pages.
Is it necessary to go for a framework for this� or without it.

If I am not using a framework, what should the action return�
a null actionforward ?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to take control of what the Action class returns, just use the HTTPServletRequest object's PrintWriter, and format the response however you want. You'll then return null from the execute() method to tell Struts you've taken care of the response yourself, and not to forward to any other JSP.
 
Pradeep Swaminathan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Merrill...
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am curious if others have successfully integrated Ajax into their Struts applications. It does not seem that difficult, but I could see where some trial and error would have to occur to develop some best practices.

It seems like DispatchAction might be a good approach so that you do not have to create a new Action class for every piece of data that you want to retrieve. I could see where some of the wildcard features could help with creating Action Mapping entries (is that 1.2 or 1.3?). What issues have other hit? Did you find that you had to duplicate code (once in your main action and once in the ajax action)?

BTW, I was at a conference recently and the JavaScript library name Prototype seemed to be pretty popular. It has a number of functions that make Ajax easier.

- Brent
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brent,

Here's a few more details about my experience with AJAX.

Right from the start, I could see that Action classes just weren't a very good fit for AJAX. There's a lot of unnecessary stuff in them that AJAX doesn't need to worry about. Also, the whole idea of returning an ActionForward doesn't fit at all, since AJAX returns an XML response, rather than a JSP. This left me finding ways around the Struts framework, such as returning a null ActionForward and finding my own way to return a response. When I find myself doing work to get around a framework, that's a sign that it doesn't fit, and sends me searching for something else.

I already had some objects following the Business Delegate pattern, so at first I tried calling methods from these objects directly from the DWR framework. This worked great, but then I began to realize something: There was no security on these methods. Anyone with some savvy could construct an HTML page with some JavaScript and access any of the methods I had exposed to the DWR framework.

I then created some wrappers for these Business Delegates. These wrappers just verify that the users are valid, logged-on users, and they have authority to do what they're trying to do and then passes the call on to the Business Delegate. I now call the wrappers from AJAX, and that seems to solve the security problem.

Another reason I went with the DWR framework for AJAX rather than trying to pound the square peg of AJAX into the round hole of Struts is that DWR totally does all the low level AJAX dirty work for me. All I have to do is define which classes and methods I want DWR to call, and which beans might be passed by those calls, and DWR provides me with a JavaScript function that has the same signature as my Java method. When I call that function, DWR handles all the marshalling and unmarshalling of XML, and magically calls my Java method on the server, returning the response to a JavaScript function that I supply. If I try to call a Struts Action class through AJAX, I have to do everything myself.

I find that this mix of Struts and DWR works pretty well. Both Struts and DWR call some of the same Business Delegate classes, so there's really not much duplication of code. Security checking is about the only area where code is duplicated, but later I may re-factor some of my security checking code into common classes that can be called by either Struts or DWR.

I still firmly believe that most web application problems are best solved by the traditional "display a page, read a form" approach, and of course Struts works great for those pages. There are a few problems that AJAX handles much better, and for those tasks, I use AJAX and DWR.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info Merrill!! Anybody else?

- Brent
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill,

Nice to see your answers.

By using XML i want to read only the message text values which i am writtening from Action class.

And return mapping.findForward(null).

But i am getting NULL value if i read request.responseXML in JavaScript.

What could be the problem.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you defining a callback method? This is where AJAX is a little different than what we're used to.

After you've called the server, you have to do something like this:


Note: in order for the above code to work, request has to be a global variable.
[ May 18, 2006: Message edited by: Merrill Higginson ]
 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill,

I am sure i am calling the call back method like this.

request.onreadystatechange = function() { replyMethod(); };
request.send(null);

function replyMethod()
{
if(request.readyState == 4)
{
if(request.status == 200)
{ //1
alert(request.responseXML);
//2
alert(request.responseText);
}
}
}

//1 is giving Null
//2 is giving XML content

which i have written from Action class.
 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill,

Thanks for great response.


But according to my requirement i want to set one message in action class.

And i want to read that message in JavaScript , depending on that message

value i want to popup a dialog box to show that message.

If we can do this thing by using request.responseText , then u can tell me

how to set that value in html and how to read that in javascript by using

request.responseText object.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have never tried using the responseText property. I've always used responseXML.

One important thing to check is that in your action, you must code:
response.setContentType("text/xml");

The best way to do it is like this:

var elements request.responseXML.getElementsByTagName('myTag');

the variable elements is now an array containing all the elements in the XML with <myTag>. If it's just a simple tag with String content, you get the value with .childNodes[0].nodeValue. If an element has child elements, you can again use getElementsByTagName() to drill down and get those child elements.

Once you have the information, you use JavaScript's Document Object Model (DOM) to change the document the way you want it. If you don't know how the JavaScript DOM works, you are going to have to spend some time studying it before you can use it.
[ May 19, 2006: Message edited by: Merrill Higginson ]
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Merill, Now I am really curious to see how responseXML works though I am doing my things with responseText.

I tried all different ways. As you mentioned I set response.setContentType("text/xml"); but still I get responseXMl as null.

I am doing very basic thing like this
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<Catagories>"+"xxxx" +"</Catagories>");

I get reponseXML null but resposeText with what I set.
I also tried adding ("<?XML version=\"1.0\" encoding=\"UTF-8\"?>");

I am returning null from action mthod.
In my jsp I just alert responseText and responseXML.
Any clue on what I am missing.

Thanks for your suggestion.
[ May 19, 2006: Message edited by: Vani D Bandargal ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the rule the XMLHttpRequest object is supposed to follow:

It will first try to parse the data returned from the server into an XML DOM. If it succeeds, the responseXML property will contain the DOM for the returned XML. If it fails, the responseText property will contain the text returned. Therefore, if the XML you create is malformed in any way, the responseText property will be populated instead of the responseXML property. As to why the XMLHttpRequest object wasn't able to parse your XML, I'm afraid I don't know.
 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vani and Merrill,

Thank you very much for both of your replies.

I used to read both of your answers daily.

As Vani said , i think her and my problem is same. It seems the problem

could be XMLHttpRequest is not supporting by the IE browser, if i am not

wrong.

We will do one thing Merrill, kindely send total JSP and Action class

code whatever you are using. By comparing with your code we can come to

the decision , what mistakes we are doing in our code.


Please expecting code from you for comparision.

Vani please be in touch with Merrill to get the code, which can help

to produce the request.responseXML
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's some code for a basic "Hello World" AJAX/Struts page. I've tested it, and it works on my system. I'm running WebSphere Application Server Version 6 and Microsoft Internet Explorer version 6.0.

testAjax.jsp

TestAjaxAction


struts-config.xml
<action path="/testAjax" type="com.mycompany.actions.TestAjaxAction">
</action>
 
"To do good, you actually have to do something." -- Yvon Chouinard
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic