【问题标题】:Is it possible to use multiple params in the itemLabel of a form:select/form:option是否可以在表单的 itemLabel 中使用多个参数:选择/表单:选项
【发布时间】:2011-10-31 21:44:32
【问题描述】:

我正在使用 Spring。我有一个带有表单的 JSP:在其中选择显示用户列表。在这种情况下,用户名或 id 对用户来说意义不大,所以我需要显示 firstname lastname。

我试过了:

<form:select id="userSelect" name="userId" path="user.id">
    <option value="">Select to Edit</option>
    <form:options items="${user.userList}" itemValue="id" itemLabel="lastname firstname" />
</form:select>

但这给了我一个很大的错误。如何让 itemLabels 显示姓氏、名字?

【问题讨论】:

    标签: spring jsp spring-mvc jstl


    【解决方案1】:

    我不这么认为。在您的对象中有一个 getFullName() getter 返回姓氏和名字的串联,或者在循环中逐个显示选项:

    <form:select id="userSelect" name="userId" path="user.id">
        <option value="">Select to Edit</option>
        <c:forEach var="theUser" items="${user.userList}">
            <form:option value="${theUser.id}"><c:out value="${theUser.lastname} ${theUser.firstname}"/></form:option>
        </c:forEach>
    </form:select>
    

    user.getFullName() 执行连接:

    <form:select path="user"
       items="${user.userList}"
       itemValue="id"
       itemLabel="fullName">
    </form:select>
    

    【讨论】:

    • 当然这么简单,我怎么没想到呢!进入我的对象,返回连接的字符串。如果我错过了,我想是时候睡觉了。
    • 在我看来不是最优雅的方式。尝试使用弹簧格式化程序。它提供了一种将对象从表单中的字符串“转换”为具体对象的简单方法
    • @sashok_bg 我很想看看你所指的例子。请告诉我如何使用“spring Formatter”来做到这一点,那太棒了!
    • @kasdega 你可以看看官方文档:docs.spring.io/spring/docs/current/spring-framework-reference/…,或者查看我在 git hub 上的测试项目 repo github.com/sashokbg/schedule/tree/master/src/main/java/bg/…
    • 我现在可以在下拉列表中显示所有值,但是如何在我的 spring 控制器中捕获选定的下拉值?
    猜你喜欢
    • 1970-01-01
    • 2020-09-04
    • 2017-10-04
    • 2018-12-25
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    相关资源
    最近更新 更多