Search code examples
javaspringthymeleaf

InitBinder doesn't call on submit


I'm learning Spring/Thymeleaf and I'm building a small project. Here is a piece of my code: This is my Controller:

@Controller
public class MovieController {

    private IMovieService movieService;
    private IGenreService genreService;

    @Autowired
    public MovieController(IMovieService movieService, IGenreService genreService) {

        this.movieService = movieService;
        this.genreService = genreService;
    }

    @InitBinder(value = "genres")
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Genre.class, new GenrePropertyEditor());
    }


    @RequestMapping(value = "/addNewMovie", method = RequestMethod.GET)
    public String addMovieForm(Model model) {
        model.addAttribute("movie", new Movie());
        model.addAttribute("genreList", genreService.findAll());

        return "movie-add/view";
    }

    @RequestMapping(value = "/addNewMovie", method = RequestMethod.POST)
    public String addMovie(@ModelAttribute Movie movie, Model model) {
        movieService.save(movie);

        return "redirect:allMovies";
    }
}

This is my Editor:

public class GenrePropertyEditor extends PropertyEditorSupport {

    @Autowired
    private GenreService genreService;

    @Override
    public void setAsText(String id) {
        final Genre part = genreService.findById(UUID.fromString(id));
        setValue(part);
    }

    @Override
    public String getAsText() {
        return ((Genre) getValue()).getId().toString();
    }
}

And this is my view:

<form action="#" th:action="@{/addNewMovie}" th:object="${movie}"
        method="post">
        <p>
            Title : <input type="text" th:field="*{title}" />
        </p>
        <p>
            Description : <input type="text" th:field="*{description}" />
        </p>
        <p>
            Year : <input type="text" th:field="*{year}" />
        </p>

        <select class="form-control" th:field="*{genres}" multiple="multiple">
            <option th:each="genre : ${genreList}"
                th:field="*{genres}"
                th:value="${genre.id}"
                th:text="${genre.genreInformation}">
                Action
            </option>
        </select>

        <p>
            <input type="submit" value="Submit" /> <input type="reset"
                value="Reset" />
        </p>

    </form>

When I call addMovie method it create a new movie. With Title, Description and year but without genre. I debug it and I discover: 1 When i Submit with two genres. It create a List of two nulls. 2 Init Binder method wasn't called.


Solution

  • Have you try to change from

     @InitBinder(value = "genres")
    

    to

     @InitBinder
    

    I think the method is not call due to the restriction on "genres".