How to update several entities with dropdown list

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to update several entities with dropdown list

cemal94
This post was updated on .

So i need to update the entities in my table with the selected values from dropdown lists from thymeleaf.

What i want to do is update the movie column based on the selections from dropdown list. But i have more than one dropdown list for each WeekMovie entity.

I cant figure out how to update them all.

My WeekMovies Class

@Entity
public class WeekMovies {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@ManyToOne
@JoinColumn(name="movie_id")
private Movie movie;

@ManyToOne
@JoinColumn(name="saloon_id")
private Saloon saloon;

//getter setters.
My Controller

@RequestMapping(value = "/update",method= RequestMethod.GET)
public String updateweekmovies(Model model){
    List<WeekMovies> weekMoviesList = weekMovieService.findAll();
    List<Movie> movieList= movieService.findAll();
    model.addAttribute("weekMovieList",weekMoviesList);
    model.addAttribute("movieList",movieList);


    return "updateweekmovies";

}

@RequestMapping(value = "/update",method= RequestMethod.POST)
public String updateweekPost(Model model){


    return "updateweekmovies";

}
My Thymeleaf

div class="container">
<div class="row">
    <form class="form-horizontal" th:action="@{/week/update}" method="post"
          enctype="multipart/form-data">
        <fieldset>
            <legend class="center-block">
                Update Book Information * is a
                        required field

            </legend>
           
            <div class="form-group">
                <label class="col-md-2 control-label" for="weekmovie">*
                    Category</label>
                <div class="col-md-8">

                <li th:each="weekmovie:${weekMovieList}">
                    <select th:value="${weekmovie.movie}"  id="weekmovie" name="weekmovie" class="form-control">
                        <option value="" selected="selected" disabled="disabled">Please
                            select an option...</option>
                        <option th:each="movie:${movieList}" th:value="${movie.id}" th:text="${movie.title}"> </option>

                    </select>
                </li>
                </div>
            </div>


            <div class="form-group">
                <div class="col-md-2"></div>
                <div class="col-md-8">
                    <button type="submit" class="btn btn-success">Update Movie</button>
                </div>
            </div>
        </fieldset>
    </form>
</div>
Reply | Threaded
Open this post in threaded view
|

Re: How to update several entities with dropdown list

Reiju
1. U need to create some DTO with list-field
public class SomeDto {
        private List<WeekMovie> weekMovies;

        //getters, setters, etc
}

2. Controller (set entities for update in your DTO field)
@GetMapping("/test/form")
public String getForm(Model model) {
        SomeDto someDto = new SomeDto();
        someDto.setWeekMovies(weekMovieService.findAll());

        model.addAttribute("someDto", someDto);
        model.addAttribute("movies", movieService.findAll());
        return "form";
}

3. HTML
<form method="post" th:action="${'/test/form'}" th:object="${someDto}">
    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>MOVIE</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="weekMovie, status : ${someDto.weekMovies}">
            <td>
                <select th:field="*{weekMovies[__${status.index}__].movie.id}">
                    <option th:each="movie : ${movies}"
                                th:value="${movie.id}" th:text="${movie.name}"></option>
                </select>
            </td>
        </tr>
        </tbody>
    </table>
    <button type="submit">Save</button>
</form>