简体   繁体   English

为什么我的表单无法在Spring Boot中传递信息

[英]why is my form not passing information in spring boot

Hello i am passing information in a form and everything runs fine but when i fill out the form it doesnt not pass the information and i am getting this error. 您好,我正在以表格的形式传递信息,并且一切运行正常,但是当我填写表格时,它没有传递信息,并且出现此错误。

There was an unexpected error (type=Internal Server Error, status=500). 发生意外错误(类型=内部服务器错误,状态= 500)。 Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement Caused by: java.sql.SQLIntegrityConstraintViolationException: Column 'movie_id' cannot be null 原因:org.hibernate.exception.ConstraintViolationException:无法执行语句原因:java.sql.SQLIntegrityConstraintViolationException:列'movie_id'不能为null

the code i am using is as folow: 我正在使用的代码如下:

@PostMapping("/save")
public String save(Movie movie) {
    savedMovie.save(movie);
    return "redirect:/LatestMovies";
}

And

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

i belive all other code is correct becuase if i try to render the db information i have no problem. 我相信所有其他代码都是正确的,因为如果我尝试呈现数据库信息,我没有问题。

Update 更新

This is the complete html code. 这是完整的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>

Your controller is expecting a Movie object, but it is receiving something else, which then produces a null Movie object. 您的控制器期望有一个Movie对象,但它接收到其他东西,然后产生一个空的Movie对象。 You need to use th:object in your form in order to correctly send the respective class. 您需要在表单中使用th:object才能正确发送相应的类。 First, let's add a new @ModelAttribute to your controller, so that your form can automatically map your Movie object in your form. 首先,让我们向控制器添加一个新的@ModelAttribute ,以便您的表单可以自动在表单中映射Movie对象。

Controller 调节器

// 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();}

Now, let's change your form, so that it actually sends a Movie object. 现在,让我们更改表单,以便它实际上发送一个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>

Note that I also changed the name attribute in your inputs, for th:field . 请注意,我还更改了输入内容中的name属性th:field Have in mind that in order for this to work, the name of each field must match exactly the names in your objects. 请记住,为了使此功能起作用,每个字段的名称必须与对象中的名称完全匹配。

Update 更新

In case you want to set a default value to your form, without using js and since you can't combine th:field with th:value , you could set the object's attribute in your controller. 如果您想在不使用js的情况下为表单设置默认值,并且由于您无法将th:fieldth:value结合使用,则可以在控制器中设置对象的属性。

@ModelAttribute(value = "newMovie")
public Movie newMovie() {
    Movie movie = new Movie();
    movie.setName("Test");
    return movie;
}

Update 2 更新2

If what you want is to put the current iteration of a Thymeleaf list in your form, you can do the following. 如果要在表单中放置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批注标记方法参数。

This happens because the movie object you are trying to send from the form to the controller is not mapped properly . 发生这种情况是因为您尝试从表单发送到控制器的电影对象未正确映射 This has as a result the constraint of the movie_id you have in your movies table (PK not null I guess) to be violated by trying to insert a not null value into it. 结果,通过尝试向其中插入一个非null值,您违反了movie表中movie_id的约束(我想PK不是null) If you want the object formed in the frontend page form to be binded in a java object you could try this 如果要将前端页面表单中形成的对象绑定到Java对象中,可以尝试执行此操作

front page form 头版表格

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

(you should import on your page the springframework form taglib <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> ) (您应该在页面上导入springframework表单taglib <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> ))

Save controller code 保存控制器代码

@PostMapping("/save")
public String save(@ModelAttribute("movie") Movie movie) {
savedMovie.save(movie);
return "redirect:/LatestMovies";
}

Of course I am assuming that your object has a similar structure like shown below 当然,我假设您的对象具有类似如下所示的结构

Movie class 电影课

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM