简体   繁体   中英

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). Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement Caused by: java.sql.SQLIntegrityConstraintViolationException: Column 'movie_id' cannot be 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.

<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. You need to use th:object in your form in order to correctly send the respective class. First, let's add a new @ModelAttribute to your controller, so that your form can automatically map your Movie object in your form.

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.

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

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

Update 2

If what you want is to put the current iteration of a Thymeleaf list in your form, you can do the following.

<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. If you want the object formed in the frontend page form to be binded in a java object you could try this

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"%> )

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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