This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Struts / Ajax question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts / Ajax question" Watch "Struts / Ajax question" New topic
Author

Struts / Ajax question

Dennis Atkinson
Greenhorn

Joined: Aug 05, 2012
Posts: 2
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
Mitesh K Agrawal
Greenhorn

Joined: Aug 02, 2012
Posts: 5

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

Joined: Aug 05, 2012
Posts: 2
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

Joined: Aug 02, 2012
Posts: 5

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.
Manjesh Patil
Ranch Hand

Joined: Sep 24, 2010
Posts: 41

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
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 244
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:




 
Don't get me started about those stupid light bulbs.
 
subject: Struts / Ajax question
 
Similar Threads
Struts 1.2 ActionForward question
AJAX+STRUTS
can I change name of the execute() method in struts ?
ajax with struts
Question for Budi: "Struts 2 Design and Programming: A Tutorial "