开发者

Spring MVC - Reference Data

Here is the scenario: I have something like this..

<form:select path="somePath" .....>
   <form:option value="" label="Please Select"/>
   <form:options items="${students}" itemValue="id" itemLabel="name"/>
</form:select>

This dropdown list works fine.

But how can I display name of a particular student? I wan to do something like this:

 &l开发者_Go百科t;c:out value="${students[id].name}"/>

Can any one help me with the syntax?

Thanks


I assume that ${students} is an array or list of student objects. As such, it's not indexed by id and can't be directly accessed that way.

Options include:

1) Include your collection of students as a map from id to student object; your items attribute then becomes ${students.values}, and you can then look up an individual student as ${students[id]}.

2) Or, keep it as a list and then iterate through your list and find the one where the id matches:

 <c:forEach var="student" items="${students}">
    <c:if test="${student.id==id}">
        <c:out value="${student.name}" />
    </c:if>
 </c:forEach>

3) Or, lastly, if you know from the beginning which student you care about, include that student separately in the reference data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜