我有一个具有枚举属性的域对象,我想显示一个下拉列表,其中包含该对象表单中所有可能的枚举值.想象一下以下对象:
public class Ticket {
private Long id;
private String title;
private State state;
// Getters & setters
public static enum State {
OPEN, IN_WORK, FINISHED
}
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我有一个方法,为这个对象呈现一个表单:
@RequestMapping("/tickets/new")
public String showNewTicketForm(@ModelAttribute Ticket ticket) {
return "tickets/new";
}
Run Code Online (Sandbox Code Playgroud)
模板看起来像这样:
<form th:action="@{/tickets}" method="post" th:object="${ticket}">
<input type="text" th:field="*{title}" />
<select></select>
</form>
Run Code Online (Sandbox Code Playgroud)
以后它应该转换成这样的东西:
<form action="/tickets" method="post">
<input type="text" name="title" />
<select name="state">
<option>OPEN</option>
<option>IN_WORK</option>
<option>FINISHED</option>
</select>
</form>
Run Code Online (Sandbox Code Playgroud)
如何创建选择标签?所选值也应自动映射到票证,以便我可以在控制器中执行以下操作:
@RequestMapping(value = "/tickets", method = RequestMethod.POST)
public String createTicket(@Valid Ticket ticket) {
service.createTicket(ticket);
return …Run Code Online (Sandbox Code Playgroud)