【发布时间】:2018-06-22 22:48:47
【问题描述】:
我正在尝试通过表单将表添加到数据库中。正在创建的实体称为专辑,它有 2 个字段,艺术家和流派。这两个都是独立的实体。这两个字段用@ManyToOne 注释
@ManyToOne
private Artist artist;
@ManyToOne
private Genre genre;
当我提交表单时,这是我得到的错误:
There was an unexpected error (type=Internal Server Error, status=500).
Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringOptionFieldAttrProcessor' (album/add:52)
以下代码是我的控制器的一部分:
@RequestMapping({"/add", "/add/"})
public String adminAlbumAdd(Model model) {
model.addAttribute("album", new Album());
model.addAttribute("artists", artistService.list());
model.addAttribute("genres", genreService.list());
return "album/add";
}
@RequestMapping( value = "/save", method = RequestMethod.POST )
public String save(@Valid Album album, BindingResult bindingResult, Model model) {
if(bindingResult.hasErrors()) {
model.addAttribute("artists", artistService.list());
model.addAttribute("genres", genreService.list());
return "album/add";
} else {
Album savedAlbum = albumService.save(album);
return "redirect:/album/view/" + savedAlbum.getAlbumId();
}
}
以下代码是 thymeleaf 模板的一部分:
<div th:class="form-group" th:classappend="${#fields.hasErrors('artist')}? 'has-error'">
<label class="col-sm-2 control-label">Artist <span class="required">*</span></label>
<div class="col-md-10">
<select class="form-control" th:field="*{artist}">
<option value="">Select Artist</option>
<option th:each="artist : ${artists}" th:value="${artist.artistId}" th:text="${artist.artistFirstName + ' ' + artist.artistFirstName}">Artists</option>
</select>
<span th:if="${#fields.hasErrors('artist')}" th:errors="*{artist}" th:class="help-block">Artist Errors</span>
</div>
</div>
<div th:class="form-group" th:classappend="${#fields.hasErrors('genre')}? 'has-error'">
<label class="col-sm-2 control-label">Genre <span class="required">*</span></label>
<div class="col-md-10">
<select class="form-control" th:field="*{genre}">
<option value="">Select Genre</option>
<option th:each="genre : ${genres}" th:value="${genre.genreName}" th:text="${genre.genreName}">Genres</option>
</select>
<span th:if="${#fields.hasErrors('genre')}" th:errors="*{genre}" th:class="help-block">Genre Errors</span>
</div>
</div>
是什么导致了这个错误?
【问题讨论】:
-
add.html在哪里,你能添加项目目录树吗
-
@JorgeL.Morla 它在 resources/album/add.html 下我非常怀疑这是项目树的问题,因为项目中的其他所有内容都运行良好
-
add.html可以加51,52,53行吗,可能有语法错误。
标签: spring spring-boot spring-data spring-data-jpa thymeleaf