File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drop Down Box in Struts - Select Default Value Implementation

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the select tag, there's a value attribute which lets you choose the option that's to be preselected...
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 561
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 561
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm, good. my mind telling me this, but I ignore.
Thanks Aditya.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic