【问题标题】:Spring Boot error while submitting form提交表单时出现Spring Boot错误
【发布时间】: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


【解决方案1】:

这个问题原来与存储库有关。我正在扩展 CrudRepository,但 id 是 int 类型。一旦我改变了它,它就起作用了。

【讨论】:

    【解决方案2】:

    我建议不要直接发布到您的数据库对象。您应该创建一个 DTO 类,比如 AlbumDto,它将像这样映射类:

    public class AlbumDto {
    ...
    private long genreId;
    private long artistId;
    
    // Getters & Setters
    
    }
    

    然后您可以将其转换为您的 Album 对象,在您的控制器中查找相应的 GenreArtist,将它们设置在 Album 对象上,然后保存。

    【讨论】:

      【解决方案3】:

      首先,您可以考虑对GET/POST 请求使用相同的映射作为标准,例如:

      @GetMapping("/new")
      ...
      @PostMapping("/new")
      

      @Valid Album album 参数也应该注释为@ModelAttribute。 如果绑定结果有错误,则不应添加模型属性。 (实际上,您不应该为 POST 方法添加任何模型属性。) 您不应使用albumService.save() 创建该savedAlbum 对象。 那个方法应该是void

      【讨论】:

      • 我添加了“流派”和“艺术家”模型属性,因为我希望它们在我重定向回表单时保持加载在选择列表中。我还能如何做到这一点?
      • redirectalbum/add查看绑定结果是否有错误,该视图已经有“流派”和“艺术家”属性。所以你只需要设置下拉菜单的相关值。
      猜你喜欢
      • 2018-12-03
      • 2011-07-28
      • 2014-02-25
      • 2018-11-24
      • 2017-08-24
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多