gpt4 book ai didi

java - Spring MVC - 将枚举填充到下拉列表

转载 作者:行者123 更新时间:2023-12-02 07:50:08 25 4
gpt4 key购买 nike

我有一个这样的表格:

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

Projectcriteria 和 StatusEnum 如下所示:

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

如何将 ProjectCriteria 类中的 StatusEnum 属性填充到 jsp 页面。我必须将什么放入 Controller 中?

非常感谢您的帮助。

最佳答案

在 Controller 方法中添加您的枚举值,该方法向相应页面发出请求,即

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

然后使用 foreach 迭代下拉菜单中的enumValues

关于java - Spring MVC - 将枚举填充到下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22704067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com