Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Invoking Struts Action from javascript bypassing ActionForm

 
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am trying to achieve the following:
There is a jsp page with 3 fields. 2 of them are drop-down select fields (Target Audience & User). User field is initially disabled. Only if the Target Audience field has a pre-defined value selected will the User field become enabled. But the moment it is enabled, it should dynamically fetch a list of all users defined in the system from the database and display it in the dropdown. Finally, this entire jsp has to be submitted as a Struts form and invoke a Struts action.

I'm stuck with how to retrieve the list of users from the db and display it. Should that be done via an action invocation? If yes, then how do I invoke the action, because there is no requirement to click a button or something. The ideal way seems to be to somehow invoke the action from within the javascript that I am using to enable the User field.
Or is there a better method?

My application is using Struts 1.

Your ideas and suggestions will be of great help. Thanks!!!
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings ,

Basu wrote: it should dynamically fetch a list of all users defined in the system from the database and display it in the dropdown.


I suppose Ajax will be the solution to your problem . You can fire up an ajax request on the javascript event . Like e.g you can check via JavaScript to see if the Audience filed is populated or not and based on that you fire up an ajax request to a struts action that would fetch the list of users from DB. You can then use the data returned from the Ajax request to populate your drop down box. Use JQuery alongside ajax.
Take a look at JQuery Ajax .
Do post back for any further queries.
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You definitely need AJAX in this case if you wish to populate the select box based on some other select box.

AND you can use this ... <s:doubleselect>

cheers ... :-)

 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for the inputs. So is there no way I can achieve this without AJAX/jQuery? Is it not possible to achieve this through just javascript and struts?

I was trying to submit the outer form through a javascript. My jsp looks like this :



When I change the drop-down, and select Individual User, the decideEnabling() function is getting called. Inside that function, the alert "hi...after action" is also getting executed. But the document.forms[0].submit() doesn't seem to be working. An SOP statement in the execute() method of the action class is not getting printed, which means the action isn't getting called. Even the second alert is not executing. But I am not getting any errors either.
I find this strange because a document.forms[0].submit() works in another place in my application perfectly fine.
 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Souvvik,

Let me Introduce you a little to AJAX. What AJAX does is to make a request to your servlet or your struts action without submitting the current form.

Now lets suppose you use javascript and just javascript to submit your form. In this case the complete form will be submitted and the response will be rendered once again which might clear all the data that you entered previously. Now with AJAX only the required select field will be repopulated making your page look the same as it was before.

Moreover, AJAX uses less overhead as much lesser data is received and sent.

I previously introduced you to <s:doubleselect> as in that way you won't have to be bothered about AJAX as it makes this easier.

Cheers :-)
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lalit....thanks a lot for your post. The tag you named, is it a struts 2 tag? I followed your link and the documentation looks like its for struts 2 (also..the s prefix)
I'm using Struts 1.x for this. Any equivalent in 1.x version for this?
 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Basu,

Yes it's a struts2 tag. I didn't knew you were working on struts1.

I'm not sure about such an equivalent tag for struts1.

Enjoy googling :-) for that.

why don't you just use ajax to write your code. It would be a lot better and you'll also get to learn a lot.

:-)

 
Ranch Hand
Posts: 213
Eclipse IDE Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some sample code here on populating a select box list using jQuery, AJAX, and JSON. My project is using Struts 2, but this code can be easily modified to work in Struts 1.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic