0

i am trying to build a project in which a user submits a ticket and can edit it later after submitting . on create ticket page there is a drop down menu for selecting priority of the ticket with options - highest,high,medium,low . there is a table in database for priority options . when user click on a edit link corresponding to a ticket , user is forwarded to an edit page where different fields of the ticket are shown populated . for showing the selected option for priority menu i am doing this , as follows

<select name="priority">
 <c:forEach var="priority"items="${priorityList }">
    <c:if test="${tempTicket.priorityId == priority.priorityId}">
        <option value="${priority.priorityId }" selected="selected">${priority.priorityName}</option>
    </c:if>
   <option value="${priority.priorityId }">${priority.priorityName </option>
 </c:forEach>
</select>

In this the selected option is shown two times in the drop down on the edit page , how can this be stopped ? is there any other way that can fullfill the same purpose ? i have tried using two <c:if> .

Alok Mishra
  • 926
  • 13
  • 39
  • possible duplicate of [How do I do the equivalent of a java If-Else block using JSTL?](http://stackoverflow.com/questions/6219267/how-do-i-do-the-equivalent-of-a-java-if-else-block-using-jstl) – singhakash Apr 06 '15 at 09:02

3 Answers3

4

First solution:

<c:if test="${tempTicket.priorityId != priority.priorityId}">
    <option value="${priority.priorityId }">${priority.priorityName }</option>
</c:if>

Second solution:

<c:choose>
    <c:when test="${tempTicket.priorityId != priority.priorityId}>
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

Third (and best) solution:

<option value="${priority.priorityId }" 
        <c:if test="${tempTicket.priorityId == priority.priorityId}">selected="selected"</c:if>>
    ${priority.priorityName}
</option>
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

Else implementation in jstl is

choose tags

<c:choose>
  <c:when test="cond1">
    Condition 1
  </c:when>
  <c:otherwise>
    Condition 2 (if condition 1 is false)
  </c:otherwise>
</c:choose>
drgPP
  • 926
  • 2
  • 7
  • 22
1

Try this

<option value="${priority.priorityId }" 
${tempTicket.priorityId == priority.priorityId? "selected" : ""} 
>${priority.priorityName}</option>
Vartlok
  • 2,539
  • 3
  • 32
  • 46
  • I guess also will be helpfull for you Spring form: http://docs.spring.io/autorepo/docs/spring/3.2.x/spring-framework-reference/html/view.html It's help to avoid such situation. But not in all case can be applied. – Vartlok Apr 06 '15 at 09:13