弹簧形式选择选项

时间:2014-04-03 12:46:54

标签: java spring jsp spring-mvc

在我的JSP中有这个片段

<sf:select multiple="true" path="author" id="authors" size="7" >
  <sf:options items="${authors}" itemValue="name" itemLabel="surname"/>
</sf:select>

其中${authors}是来自DB的作者对象的ListAuthor拥有财产namesurname。 通过在JSP中更改itemLabel=,我只能在列表中显示作者的namesurname

如何在选项列表中的一个字符串中显示name surname

3 个答案:

答案 0 :(得分:1)

在jsp中包含此jstl标记:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

然后如果你想显示下拉选项,比如姓名 - 姓氏:

    <c:forEach items="${authors}" var="author">
        <sf:option  value="${author.name}">
            <c:out value="${author.name}"/> - <c:out value="${author.surname}"/>
        </sf:option>                     
    </c:forEach>

答案 1 :(得分:0)

在Author类中添加getDisplayName(),并通过表单中的displayName引用它

public String getDisplayName() {
    return name + " " + surname;
}

假设&#34;名称&#34;当然是名字......

答案 2 :(得分:0)

您可以通过使用c:forEach填充下拉列表中的每个选项来实现此目的。

见下文

<sf:select multiple="true" path="author" id="authors" size="7" >
 <c:forEach items="${authors}" var="author">
  <sf:options itemValue="author.name" itemLabel="author.surname"/>
</c:forEach>
</sf:select>

让我知道任何进一步的澄清......