why is my form not passing information in spring boot

BLH-Maxx :

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>

Alain Cruz :

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is my .putExtra not passing information?

Why is the URL not passing information to my view?

why is my javascript passing the wrong information?

Why does my browser send a GET request on a form where it should be sending a POST request[Spring-Boot]?

Spring boot thymeleaf field passing form inputs between pages

How can I get the information in the correct form (Spring Boot)

Why is spring boot not finding my beans?

Why isn't my form passing the id to the controller?

Why do I lose information after submit a form with Spring MVC?

Chrome extension passing form information to a PHP file

Why Redis Cache is not getting empty in my Spring Boot application?

Why doesn't my spring boot error controller work?

Why is my Spring Boot @Autowired MyBatis static mapper null?

Why is Spring Boot @Async dropping items in my List argument?

Why does Spring Boot not read the PathVariable that is defined on my interface resource?

Why does my Spring Boot web app not run completely in Gradle?

Why is my Spring Boot entity ID generation failing?

Spring Boot -- why doesn't the @ComponentScan work with my package combination?

Why my Spring boot web APP IS not working? please

Why is my spring boot stateless filter being called twice?

Why does my Spring Boot POST output a "Whitelabel Error Page"?

Why AOP does not work in my Spring Boot application?

Why isn't my spring boot @RequestMapping working?

Why my simple Spring Boot application fails to deploy on Heroku?

Why is my Spring Boot app with SQS Not Showing a JMSHealthIndicator?

Why If I upload a picture Spring Boot searches in my computer?

Spring inject my class only on my startup when I submit my form my variable is null why?

Why isn't my Redis cache working in my Spring-Boot/Spring-Data application?

Why is my Spring application run from my spring boot unit test