Aravind
Aravind

Reputation: 79

Selecting a default value for list in JSP

I am trying to select a default value for my list in my JSP code. I could get the list but when I want a default value if a condition is satified, I could not get the value selected. Below is the code.

I am comparing two values

${creditDebitAccountViewModel.expirationMonth}
${expirationMonth}


<select id="expirationMonth" ">
    <c:forEach items="${creditDebitAccountViewModel.listExpirationMonth}" var="expirationMonth">
    <c:choose>
        <c:when test="${creditDebitAccountViewModel.expirationMonth eq expirationMonth}">
           <option  value="expirationMonth" selected>${expirationMonth}</option>
        </c:when>    
        <c:otherwise>
           <option value="expirationMonth">${expirationMonth}</option>
        </c:otherwise>
    </c:choose>
    </c:forEach>  
 </select>

Upvotes: 0

Views: 822

Answers (1)

Zachary Craig
Zachary Craig

Reputation: 2220

Is the option actually getting the selected attribute if that condition evaluates to true?

Try and make sure that the condition isn't the problem, and if it isn't, try editing your selected attribute.

It's also worth noting your select element has a trailing quote after the id attribute, which may be making your HTML invalid.

<select id="expirationMonth" ">
                             ^

Address these problems and your code should be valid, in theory.

Depending on if your page is HTML or XHTML, you may also need to switch

<option value="Lorem ipsum" name="placeholder_text" selected="selected">
    Lorem ipsum
</option>

Upvotes: 1

Related Questions