aspose file tools*
The moose likes Struts and the fly likes Drop Down Box in Struts - Select Default Value Implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Drop Down Box in Struts - Select Default Value Implementation" Watch "Drop Down Box in Struts - Select Default Value Implementation" New topic
Author

Drop Down Box in Struts - Select Default Value Implementation

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Ranchers,
I have the below Requirement.



how do i implement this requirement in Struts..Please guide and advice me....
Any tag has be used or specified in Struts to implement this..


Thanks and Regards
Deepak Lal



When The Going Gets Tougher,The Tougher gets Going
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Look at the select tag, there's a value attribute which lets you choose the option that's to be preselected...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507


Hi Ankit,
Thanks for the input.I'm stuck up again



The Above Says that we need a value to be compared for marking an option selected...so how can i implement that part of logic...can you please advice.....???
and the option to be preselected has to be validated against the value fetched from database.....so can you please advice on how to implement this logic in Struts.



Thanks and Regards
Deepak Lal
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

This is Struts 1 which I'm not very good at, but suppose the value attribute of the select tag goes like this


Now the option which has the value val1 will be selected by default...
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Moderators and Experts in Struts,

Need Help on this issue ::::



In the above, you are hardcoding the value attribute, but i need to dynamically set the default selected value of the Struts drop down list into the "value" Attribute.Could you please help me out with this.


Please find my code below....Please help me on how to proceed.


Thanks and Regards
Deepak Lal
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi All,
Please find my detailed requirement...

Requirement: i have 2 drop down list

1st dop down list is having countries --> Australia,India,UK
2nd drop down list is also having countries --> USA,Canada,Brazil

Both the drop down list are being displayed in jsp using logic iterate tag



im doing a logic iterate over this and 2 drop down list's are displayed correctly
the problem is now that i need the default value for the 1st dropdown and 2nd drop down to be selected on the basis of the value in database.

Assuming
1> Default value of 1st dropdown list is Australia ,which is fetched from database
2> Default value of 2nd dropdown list is Brazil ,which is fetched frm database

How should i modify the code to achieve this...because i have 2 dropdownlists being iterated using logic:iterrate tag.


Could you help me with this issue since i dont know how to proceed. i have read post saying we need to set the value attribute,but how do i set the value dynamically in the
<html:select .....> struts tag.....


Please help me




Deepak
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

What you need to know is "selected" attribute in <option> tag, look here:
http://www.w3schools.com/TAGS/att_option_selected.asp

Now, in your case, your using the struts <html:options collection="..." ../> element, e\which doesn't provide any method to preselcet the default value. So solutions may fit in your case:
1. On page load, call JS and and select that option
2. or simply use <html:option > tag and use the "selected" attribute.


HTH


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Sagar,
Thanks for the inputs.



On page load, call JS and and select that option ---> how should i implement this part. can you help me with this.
if you have an existing JS function can you paste it?





Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Well, you've to provide an ID attribute to the <select /> tag, like

and in JS,
(Note: please check the code for proper JS functions)


Now the only part is tricky about how will you send that default value(defVal) to JS,
1. On page load, call JS like this



Now, this is no more "struts" question, so better continue it in HTML-JavaScript section.
Aditya Keyal
Ranch Hand

Joined: Dec 01, 2008
Posts: 71
To select a value by default in the html:select tag there are 2 ways:

1) The combination of name and value should evaluate to the default selected value.

ex.

Here name is a bean in the request scope whose property value called propertyname should evaluate to the default selected value.

2) Have a default selected by using EL.



Here also the DefaultSelectedValueInRequest is the value set in the request scope.

For more details go through the literature of struts-tag for logic:select to understand more about it.


- Aditya Webservices Blog
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

hmm, good. my mind telling me this, but I ignore.
Thanks Aditya.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drop Down Box in Struts - Select Default Value Implementation