lazyCat
lazyCat

Reputation: 121

How to keep a value as selected in Spring form:options?

<form:select path="timer" id="time">

            <form:option value="0">09:00 AM</form:option>
            <form:option value="1">10:00 AM</form:option>
            <form:option value="2">11:00 AM</form:option>
            <form:option value="4">01:00 PM</form:option>
            <form:option value="3">12:00 PM</form:option>
            <form:option value="5">02:00 PM</form:option>
            <form:option value="6">03:00 PM</form:option>
            <form:option value="7">04:00 PM</form:option>
            <form:option value="8">05:00 PM</form:option>
            <form:option value="9">06:00 PM</form:option>
        </form:select>

How to keep 03:00 PM as selected? This code is placed in my jsp.

Upvotes: 0

Views: 1858

Answers (1)

CupawnTae
CupawnTae

Reputation: 14580

Spring will automatically make the correct <option> selected if the value for timer in your model matches the value attribute for that <form:option>.

So it depends how you're populating your model and what your form is bound to but the fundamental answer is set timer to 6 in your model.

E.g. you might have code like this in your controller:

model.addAttribute("timer",6);

Upvotes: 1

Related Questions