Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - populate enum to dropdownlist

I have a form like this :

<form:form method="POST" action="searchProjects" commandName="projectcriteria">
    <table>
        <tr>
            <td class="label"><spring:message code="number" /></td>
            <td><form:input path="number" /></td>
            <td class="label"><spring:message code="customer" /></td>
            <td><form:input path="customer" /></td>
        </tr>
        <tr>
            <td class="label"><spring:message code="name" /></td>
            <td><form:input path="name" /></td>
            <td class="label"><spring:message code="status" /></td>
            <td>
                <form:select path="status">
                    <option value=""><spring:message code="please_select" /></option>
                    <c:forEach var="enum" items="${allStatus}">
                        <option value="${enum}"><spring:message code="${enum.statusEnum}" /></option>
                    </c:forEach>
                </form:select>
            </td>
        </tr>
        <tr>
            <td colspan="4" style="text-align: center;">
                <input type="submit" value="<spring:message code="search"/>" />
                <input type="button" value="<spring:message code="reset_criteria"/>" />     
            </td>
        </tr>
    </table>
</form:form>

The Projectcriteria and the StatusEnum are like this:

public enum StatusEnum {
    INV("Invalidate"),
    TOV("Validate"),
    VAL("Validated"),
    FIN("Finished");

    private String name;

    private StatusEnum(String name) {
        this.name = name;
    }

    public String getStatusEnum() {
        return this.name;
    }
}

public class ProjectCriteria {
    private long number;

    private String name;
    private String customer;
    private StatusEnum status;

    /**
     * @return the number
     */
    public long getNumber() {
        return number;
    }

    /**
    * @param number the number to set
    */
    public void setNumber(long number) {
        this.number = number;
    }
    /**
    * @return the name
    */
    public String getName() {
        return name;
    }
    /**
    * @param name the name to set
    */
    public void setName(String name) {
        this.name = name;
    }
    /**
    * @return the customer
    */
    public String getCustomer() {
        return customer;
    }
    /**
    * @param customer the customer to set
    */
    public void setCustomer(String customer) {
        this.customer = customer;
    }
    /**
    * @return the status
    */
    public StatusEnum getStatus() {
        return status;
    }
    /**
    * @param status the status to set
    */
    public void setStatus(StatusEnum status) {
        this.status = status;
    }
}

How do I populate the StatusEnum attribute in the ProjectCriteria class to the jsp page. What do i have to put into the Controller?

Thanks a lot for any help.

like image 464
xtiger Avatar asked Mar 28 '14 04:03

xtiger


2 Answers

Add your enum values in Controller method which giving request to respective page i.e.

model.addAttribute("enumValues",StatusEnum.value());

Then iterate enumValues in dropdown menu using foreach.

like image 153
Harshal Patil Avatar answered Sep 28 '22 11:09

Harshal Patil


You could try this (sorry code not tested):

<c:forEach var="enum" items="${StatusEnum.values()}">
    <option value="${enum}"><spring:message code="${enum.name}" /></option>
</c:forEach>

Note: if your enum class has a package name, you might have to include the fully qualified class name in the items attribute. You might be able to pass the enum as a model attribute from your controller as well (?).

like image 36
nickdos Avatar answered Sep 28 '22 09:09

nickdos