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

JSP + struts html:select

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

I am using struts 1.2 in my web application. I have to display one <html:select>.... <html:options> list

list will be build dynamically. User can select any option. base on the user's selection page will be refreshed (need to hit the database and display information in other fields on same page.) I need to know two things:

1) there is one submit button on that page and one action is associated with that submit button. i can call the associated action class once the user finish the form and click the submit button. When user select from list i need to call another action class.

I want to know how to call another action class, (when user select something from list), as list control is inside the same html:form tag.

2) When user selects some value from list i need to refresh the page and populate the other fields.

I would like to know, Lets say there are ten values and user has selected third option, how to keep selected that third option as page is already refreshed?

I am beginner in struts.. any sample code would be a great help for me

Thank you for time and help.

Ajay
 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To your first problem: you can associate N number of actions with a given JSP as long as you know how to handle the data in the corresponding Action classes. In your case, I would associate a JS function with the "onchange" event of the <select> element. Therein, I will simply overwrite the form's action using something like:

As for your second problem: you can always send back the value selected through a request attribute or form bean variable from the server and have it saved in a hidden textbox. At the end of the <form> section, write a small script like:


That should solve your problem to a good extent, I believe.
[ March 17, 2008: Message edited by: Anirvan Majumdar ]
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) there is one submit button on that page and one action is associated with that submit button. i can call the associated action class once the user finish the form and click the submit button. When user select from list i need to call another action class.




For you particular case you'll require to do a lot of JavaScript-ing.
Basically, you'll require to have some variable which will be set to true if it is list-action and false if it is normal submit. Now, on the click of submit button you'll require to check for that variable and appropriately set your action.



But, this is not a good way and not robust and may crash in some situations. I suggest to use one action class to handle multiple action requests.

What I mean is that an action class is capable of responding to multiple action events. In the execute method you can handle multiple action requests with the help of if-else, switch or some condition block.

2) When user selects some value from list i need to refresh the page and populate the other fields.

I would like to know, Lets say there are ten values and user has selected third option, how to keep selected that third option as page is already refreshed?



For this when you are re-rendering the response, you'll require to set the property pertaining to the select box to the one that was originally selected.

e.g when you try to get what property was selected you say something like

So, while re-rendering you'll need to say-


This will work perfectly fine. But, I suggest you use AJAX for this for better user interface.

Good Luck..!!
 
today's feeble attempt to support the empire
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic