• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts / Ajax question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings all.

I am working on integrating Ajax (using jQuery) with a Struts 2 application.

I can send Ajax calls up to the server, and it all works well. But there is one thing I can't seem to figure out, if it is even possible.

In my Ajax call, I provide the URL like so:
This will invoke the "execute" method in the ProductListAction class. But how do I invoke a method OTHER THAN execute? Can this be done via Ajax without changing any Struts config files?

Thanks in advance,

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

As you are using struts 2 , definetlly you'll have struts.xml file where you can mention your desired action url, in which you can even mention which method of which Action class need to be called and which jsp need to be returned back as response.

For more clarification you can go to below url
Struts2 Example
Struts 2 Another example
 
Dennis Atkinson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mitesh K Agrawal wrote:Hi ,
As you are using struts 2 , definetlly you'll have struts.xml file where you can mention your desired action url, in which you can even mention which method of which Action class need to be called and which jsp need to be returned back as response.



Uh .... yes, of course I know I can change my struts.xml file and do this. But if I wanted a JSP, I would use normal Struts. I am trying to AVOID writing an entire jsp to the screen.

Let me explain how AJAX works: I make an AJAX call to the server, and the server sends me back some data. It does *NOT* send back a JSP.. It sends back some data that I can inject into the existing page without having to do a page refresh. That is the goal of AJAX, not writing a jsp file. All that works. So what I am trying to find out now is whether I can dynamically call a different method from my AJAX call rather than just calling execute. I can have the execute method examine the parameters I send to it, and based on the parameters, it can call a differenet method in its class, that I know. But can I dynamically call a different method? That's the question I am asking.

--Dennis
 
Mitesh K Agrawal
Greenhorn
Posts: 5
Android jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got you Dennis, and even I also know how AJAX works. The only difference is instead of using DATA i used the term JSP for the response. I have just given you example by considering JSP as a response. If you have gone through the link I provided you would have seen the code something like below

<action name="myactionurl" method ="mymethodname" class ="path to Action Class">
<result name="success">path to response file</result>
</action>

The above code will hit action class you have mentioend and calls the method provided in method attribute.
So in your ajax call you need to pass actionurl, which will return you the data present in response file.

If at all you dont want to mention response file and you need to send data directly from Action. You can modify the above code as below
<action name="myactionurl" method ="mymethodname" class ="path to Action Class">
</action>

The difference is there is no result tag. Now from your action using PrintWriter you can send the response which you can retrieve by your AJAX call.
 
Ranch Hand
Posts: 42
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Ajax call or by URL alone you cannot direct struts framework to invoke any method present in the Action class, as you have mentioned you have to make use of struts.xml file to instruct which method to be invoked unless you want default( execute) to be get executed.

-regards
Ma
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dennis,

I want sample code of Ajax + struts-2 + jsp with the explanation. I've requirement like, I'm Having a JSP page, Contains *User-Info* like name, age, address, country, zip, gender etc., (Using struts-2 and ajax tags). In the same page, I've HTML-Table contains these *User-Info* as headers. When the user clicks on submit button, All the data, what he entered in the form should populate on the Html table in the first row(always, without refreshing the page) and when he clicks on particular row in the table, the information should re-populate on the form( for these two process Action should call but page reload should not). The HTML-table contains maximum of 5 rows. If he entered sixth row, then pagination required. I want to do this in struts 2 - ajax. I'm new to Ajax and jQuery. I'm thinking to do this using ajax(server side), jQuery(client side). But I've no exact vision how to do this. Can you please tell me how to do this.

First thing Still I don't know is, javascript or jQuery really required? or ajax is more than enough?

I did Simple program(not working), In this program, I tried call action without refreshing the page, But I'm not getting. Please guide me.When I click on submit button, the page is getting refreshed.


struts.xml



jsp file



action class:




 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic