IntelliJ Java IDE
The moose likes Struts and the fly likes Default Value in html:select Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Application Frameworks » Struts
Reply Bookmark "Default Value in html:select" Watch "Default Value in html:select" New topic
Author

Default Value in html:select

Alister Pate
Ranch Hand

Joined: Sep 13, 2002
Posts: 34
Hey Guys

Does anyone have any thoughts on how to make a default value for a select?

I don't trust my users to look at a dropdown which seems to have a value selected already (and doesn't) and think to select a value.

If you see what I mean... My dropdowns look as though they have a default value, but actually don't. If you don't select a value from them, it just seems to return null or empty string or something.

Any thoughts?

Alister
Ankur Srivastava
Ranch Hand

Joined: May 11, 2004
Posts: 62
I hope this will answer your query.

Suppose you have dropdown which displays names of users. The name of the field is userName. In your form bean you will be having a field with the name userName and another field namesOfUsers which will be the collection of users which will be displayed in the dropdown. Now if you set the Default value in the actionform in the userName field. It will be displayed as the selected value.

<html:select property="userName">

<html ptionsCollection property="namesOfUsers" />

</html:select>
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 23635

Alister,
Take a look at Struts' validation. You can specify that that field can't be null/empty. Struts will then check in both javascript and on the server side that the user selected a value.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Certs: SCEA Part 1, Part 2 & 3 & Core Spring 3, OCAJP
Alister Pate
Ranch Hand

Joined: Sep 13, 2002
Posts: 34
Thanks for that.

Actually, it seems it was problems with the displaying of my results which was causing the problem, not with select at all.

That's the problem with having a fountain of all knowledge at my fingertips: it encourages me to ask first, search for an answer second.

 
 
subject: Default Value in html:select
 
Threads others viewed
Default element value is not populating in JAXB Objects
Nulls in columns or not? Frequent DB design standards issue.
Struts html:radio Question
Problem playing sound from application
Struts value into a javascript function
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com