[英]why is my form not passing information in spring boot
您好,我正在以表格的形式传递信息,并且一切运行正常,但是当我填写表格时,它没有传递信息,并且出现此错误。
发生意外错误(类型=内部服务器错误,状态= 500)。 原因:org.hibernate.exception.ConstraintViolationException:无法执行语句原因:java.sql.SQLIntegrityConstraintViolationException:列'movie_id'不能为null
我正在使用的代码如下:
@PostMapping("/save")
public String save(Movie movie) {
savedMovie.save(movie);
return "redirect:/LatestMovies";
}
和
<form th:action="@{/save}" method="post" >
<p><input type="text" id="movie_id" name="movie_id" value="" /></p>
<p><input type="text" id="movie_name" name="movie_name" value="" /></p>
<p><input type="submit" value="save" /></p>
</form>
我相信所有其他代码都是正确的,因为如果我尝试呈现数据库信息,我没有问题。
更新
这是完整的html代码。
<div class="container">
<table class="table table-hover">
<tr>
<th>Id</th>
<th>Name</th>
</tr>
<tr th:each="LatestMovies : ${latestMovies}">
<td th:text="${LatestMovies.id}"></td>
<td th:text="${LatestMovies.movieName}"></td>
<td>
<form th:action="@{/save}" method="post" th:object="${newMovie}">
<p><input type="text" id="movie_id" th:field="*{movie_Id}"/></p>
<p><input type="text" id="movie_name" th:field="*{movie_Name}"/></p>
<p><input type="submit" value="save" /></p>
</form>
</td>
</tr>
</table>
您的控制器期望有一个Movie对象,但它接收到其他东西,然后产生一个空的Movie对象。 您需要在表单中使用th:object
才能正确发送相应的类。 首先,让我们向控制器添加一个新的@ModelAttribute
,以便您的表单可以自动在表单中映射Movie对象。
调节器
// In order to use th:object in a form, we must be able to map a new entity to that form.
// In this case we return a Movie entity.
@ModelAttribute(value = "newMovie")
public Movie newMovie() {return new Movie();}
现在,让我们更改表单,以便它实际上发送一个Movie对象。
<form th:action="@{/save}" method="post" th:object="${newMovie}">
<p><input type="text" id="movie_id" th:field="*{movie_id}"/></p>
<p><input type="text" id="movie_name" th:field="*{movie_name}"/></p>
<p><input type="submit" value="save" /></p>
</form>
请注意,我还更改了输入内容中的name
属性th:field
。 请记住,为了使此功能起作用,每个字段的名称必须与对象中的名称完全匹配。
更新
如果您想在不使用js
的情况下为表单设置默认值,并且由于您无法将th:field
与th:value
结合使用,则可以在控制器中设置对象的属性。
@ModelAttribute(value = "newMovie")
public Movie newMovie() {
Movie movie = new Movie();
movie.setName("Test");
return movie;
}
更新2
如果要在表单中放置Thymeleaf列表的当前迭代,则可以执行以下操作。
<div class="container">
<table class="table table-hover">
<tr>
<th>Id</th>
<th>Name</th>
</tr>
<tr th:each="LatestMovies : ${latestMovies}">
<td th:text="${LatestMovies.id}"></td>
<td th:text="${LatestMovies.movieName}"></td>
<td>
<form th:action="@{/save}" th:object="${LatestMovies}" method="post">
<p><input type="hidden" th:value="*{id}"/></p>
<p><input type="hidden" th:value="*{movieName}"/></p>
<p><input type="submit" value="Submit"/></p>
</form>
</td>
</tr>
</table>
您忘记了使用@RequestBody
批注标记方法参数。
发生这种情况是因为您尝试从表单发送到控制器的电影对象未正确映射 。 结果,通过尝试向其中插入一个非null值,您违反了movie表中movie_id的约束(我想PK不是null) 。 如果要将前端页面表单中形成的对象绑定到Java对象中,可以尝试执行此操作
头版表格
<form:form action="save" modelAttribute="movie" method="POST">
<form:label path = "movie_id"> Movie id</form:label>
<form:input path="movie_id" name="movie_id">
<form:label path = "movie_name"> Movie name</form:label>
<form:input path="movie_name" name="movie_name">
<button type="submit">save</button>
</form:form>
(您应该在页面上导入springframework表单taglib <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
))
保存控制器代码
@PostMapping("/save")
public String save(@ModelAttribute("movie") Movie movie) {
savedMovie.save(movie);
return "redirect:/LatestMovies";
}
当然,我假设您的对象具有类似如下所示的结构
电影课
public class Movie{
private String movie_id; // or int or long
private String movie_name;
//getters setters constructors ommitted
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.