Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
提交表单时发生Spring启动错误_Spring_Spring Boot_Spring Data_Spring Data Jpa_Thymeleaf - Fatal编程技术网

提交表单时发生Spring启动错误

提交表单时发生Spring启动错误,spring,spring-boot,spring-data,spring-data-jpa,thymeleaf,Spring,Spring Boot,Spring Data,Spring Data Jpa,Thymeleaf,我正试图通过表单向数据库中添加一个表。正在创建的实体称为Album,它有两个字段:艺术家和流派。这两者都是独立的实体。这两个字段用@ManyToOne注释 @ManyToOne private Artist artist; @ManyToOne private Genre genre; 当我提交表单时,我得到的错误是: There was an unexpected error (type=Internal Server Error, status=500). Error during ex

我正试图通过表单向数据库中添加一个表。正在创建的实体称为Album,它有两个字段:艺术家和流派。这两者都是独立的实体。这两个字段用@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();
    }
}
以下代码是ThymileAF模板的一部分:

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

艺术家*
选择艺术家
艺术家
艺术家的错误
体裁*
选择类型
体裁
体裁错误

什么导致了这个错误?

首先,您可以考虑使用相同的映射为<代码> GET/POST < /代码>请求,作为一个标准,例如:

@GetMapping("/new")
...
@PostMapping("/new")
另外,
@有效的Album Album
参数应注释为
@ModelAttribute
。 若绑定结果有错误,则不应添加模型属性。(实际上,您不应该为
POST
方法添加任何模型属性。) 不应使用
albumService.save()
创建savedAlbum对象。
该方法应该是<代码>无效>代码>

首先,您可以考虑使用相同的映射,用于<代码> GET/POST < /代码>请求,作为一个标准,如:

@GetMapping("/new")
...
@PostMapping("/new")
另外,
@有效的Album Album
参数应注释为
@ModelAttribute
。 若绑定结果有错误,则不应添加模型属性。(实际上,您不应该为
POST
方法添加任何模型属性。) 不应使用
albumService.save()
创建savedAlbum对象。
该方法应该是
void

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

public class AlbumDto {
...
private long genreId;
private long artistId;

// Getters & Setters

}

然后,您可以将其转换为相册对象,在控制器中查找相应的
流派
艺术家
,在
相册
对象上设置它们,然后保存。

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

public class AlbumDto {
...
private long genreId;
private long artistId;

// Getters & Setters

}

然后,您可以将其转换为相册对象,在控制器中查找相应的
流派
艺术家
,在
相册
对象上设置它们,然后保存。

问题原来与存储库有关。我在扩展Crudepository,但是id是int类型的。一旦我更改了它,它就工作了。

问题原来与存储库有关。我正在扩展crudepository,但id是int类型。一旦我更改了它,它就工作了。

add.html在哪里,您可以添加proyect目录吗tree@JorgeL.Morla它在resources/album/add.html下,我非常怀疑这是项目树的问题,因为项目中的所有其他内容都正常工作。你能添加51,52,53行add.html吗,可能存在一些语法错误。其中是add.html,是否可以添加proyect目录tree@JorgeL.Morla它位于resources/album/add.html下,我非常怀疑这是项目树的问题,因为项目中的其他所有内容都正常工作。您可以添加51,52,53行add.html吗?可能存在一些语法错误。我添加了“流派”和“艺术家”模型属性,因为当我重定向回表单时,我希望它们保持加载在选择列表中。我还可以如何实现这一点?您
重定向
视图如果绑定结果有任何错误,该视图已经有“流派”和“艺术家”“属性。因此,您只需要设置下拉列表的相关值。我添加了“流派”和“艺术家”模型属性,因为我希望它们在重定向回表单时保持加载在选择列表中。我还可以怎样实现呢?您可以重定向相册/添加视图如果绑定结果有任何错误,该视图已经具有“流派”和“艺术家”属性。所以您只需要设置下拉列表的相关值。