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

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

public class WeekMovies {

private Long id;

private Movie movie;

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

    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"
            <legend class="center-block">
                Update Book Information * is a
                        required field

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

                <li th:each="weekmovie:${weekMovieList}">
                    <select th:value="${}"  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="${}" th:text="${movie.title}"> </option>


            <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>
Reply | Threaded
Open this post in threaded view

Re: How to update several entities with dropdown list

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)
public String getForm(Model model) {
        SomeDto someDto = new SomeDto();

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

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