gpt4 book ai didi

Java Spring Thymeleaf 将枚举集合绑定(bind)到复选框时出错

转载 作者:行者123 更新时间:2023-11-30 07:32:26 31 4
gpt4 key购买 nike

我正在使用 Java8、Spring Boot 1.2.3 和 Thymeleaf 编写应用程序。我想在 Accommodation 类中使用 AccommodationType(枚举)集合,并将它们显示为复选框,以便用户能够选择住宿中可用的 AccommodationType。

不幸的是,当我尝试显示 createAccommodation 页面时出现错误:

2016-03-09 23:07:09.176 ERROR 29740 --- [nio-8080-exec-7] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-7] Exception processing template "createAccommodation": Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputCheckboxFieldAttrProcessor' (createAccommodation:73)
2016-03-09 23:07:09.183 ERROR 29740 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputCheckboxFieldAttrProcessor' (createAccommodation:73)] with root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'types' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
at org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:396)
at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:323)
at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:289)
.....

我的模型,简化:

public enum AccommodationType {
singleRoom,
doubleRoom,
moreBeddedRoom,
apartment;
}

public class Accommodation {

@Id
private String id;

@NotEmpty
private String name;

@NotNull
private BigDecimal price;

private List<AccommodationType> types = new ArrayList<AccommodationType>();

.....

public List<AccommodationType> getTypes() {
return new ArrayList<AccommodationType>(types);
}

public void setTypes(List<AccommodationType> types) {
types = new ArrayList<AccommodationType>(types);
}
}

Controller :

@Controller
public class AccommodationController {

private final AccommodationRepository accommodationRepository;

@Autowired
public AccommodationController(
AccommodationRepository anAccommodationRepository) {
this.accommodationRepository = anAccommodationRepository;
}

@ModelAttribute("allTypes")
public List<AccommodationType> populateAccommodationTypes() {
return Arrays.asList(AccommodationType.values());
}

@RequestMapping(value = "/accommodation/create", method = RequestMethod.GET)
public ModelAndView showCreateAccommodationPage() {
return new ModelAndView("createAccommodation", "accommodation",
new Accommodation());
}

@RequestMapping(value = "/accommodation/create", method = RequestMethod.POST)
public String handleAccommodationCreate(
@Valid @ModelAttribute("accommodation") Accommodation anAccommodation,
BindingResult aBindingResult) {
if (aBindingResult.hasErrors()) {
return "createAccommodation";
}
accommodationRepository.save(anAccommodation);
return "redirect:/accommodation";
}
}

View 模板:

<form class="form-horizontal" role="accommodation" th:action="@{/accommodation/create}" method="post">
....
<div class="form-group">
<ul>
<li th:each="type : ${allTypes}">
<input type="checkbox" th:field="*{types}" th:value="${type}" />
<label th:for="${#ids.prev('types')}" th:text="#{${type}}"></label>
</li>
</ul>
</div>
....

有人知道我做错了什么吗?

最佳答案

问题是表单标记中缺少 th:object:

<form class="form-horizontal" th:object="${accommodation}"
th:action="@{/accommodation/create}" method="post">

关于Java Spring Thymeleaf 将枚举集合绑定(bind)到复选框时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35904100/

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