Is it possible to use multiple params in the itemLabel of a form:select/form:option

19,916

I don't think so. Either have a getFullName() getter in your object returning the concatenation of last and first names, or display the options one by one, in a loop:

<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>

With concatenation performed by user.getFullName():

<form:select path="user"
   items="${user.userList}"
   itemValue="id"
   itemLabel="fullName">
</form:select>
Share:
19,916
kasdega
Author by

kasdega

Updated on June 05, 2022

Comments

  • kasdega
    kasdega almost 2 years

    I'm using Spring. I've got a JSP with a form:select in it displaying the list of users. In this situation the username or id won't mean much to the user so I need to show firstname lastname.

    I've tried:

    <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>
    

    But that gives me a big error. How can I make the itemLabels show lastname, firstname?

  • kasdega
    kasdega almost 13 years
    Of course its so simple why didn't I think of that! A getting in my object that returns the concatenated string. I think it's time for bed if I'm missing that.
  • sashok_bg
    sashok_bg about 9 years
    Not the most elegant way in my opinion. Try using spring Formatter. It provides an easy way of "transforming" your objects from strings in a form to a concrete object
  • kasdega
    kasdega almost 9 years
    @sashok_bg I'd love to see an example of what you're referring to. Please, show me how to use "spring Formatter" to do this, that'd be awesome!
  • sashok_bg
    sashok_bg almost 9 years
    @kasdega You can take a look at the official doc : docs.spring.io/spring/docs/current/spring-framework-referenc‌​e/… , or check out my test project repo on git hub github.com/sashokbg/schedule/tree/master/src/main/java/bg/…
  • mannedear
    mannedear over 5 years
    I'm able to display all values in the drop down now but how do I capture the selected drop down value in my spring controller?