Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring add default value to form:select

I'm developing a spring application, now I've added a dropdownlist to one of my jsp pages using:

<form:select multiple="single" path="users[y.count-1].X" items="${Y}" itemValue="id" itemLabel="name"/>

Now I'd like to add the default value "Nothing selected", however I can't seem to find how to do this. I've tried:

<form:select multiple="single" path="users[y.count-1].X" items="${Y}" itemValue="id" itemLabel="name">
   <form:option value="Nothing selected" />
</form:select>

But "Nothing selected" isn't displayed in my dropdownlist.

like image 657
Mike Avatar asked May 04 '10 14:05

Mike


People also ask

How to set a default value for select tag?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.

How do I change the default value in Java?

Remember, to get the default values, you do not need to assign values to the variable. static boolean val1; static double val2; static float val3; static int val4; static long val5; static String val6; Now to display the default values, you need to just print the variables.


2 Answers

You should be able to do

<form:select multiple="single" path="users[y.count-1].X" >
   <form:option value="Nothing selected" />
   <form:options items="${Y}"  itemValue="id" itemLabel="name" />
</form:select>
like image 128
Jacob Mattison Avatar answered Sep 19 '22 16:09

Jacob Mattison


Just add this line before your options:

<form:option selected="true" value="..." />
like image 43
zygimantus Avatar answered Sep 16 '22 16:09

zygimantus