aspose file tools*
The moose likes JSP and the fly likes JSP + struts html:select Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP + struts html:select" Watch "JSP + struts html:select" New topic
Author

JSP + struts html:select

Ajay Tickle
Greenhorn

Joined: Mar 16, 2008
Posts: 17
HI,

I am using struts 1.2 in my web application. I have to display one <html:select>.... <htmlptions> 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
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
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 ]
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

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..!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP + struts html:select
 
Similar Threads
Struts 1.1: Help needed with html:select, html:options, html:optionsCollection
Hi Experts, Big problem
JSP + struts html:select
Retaining selected value in html:select on page refresh
form values lost in request scope