Required Parameter is not present in Spring MVC design with spring boot using Thymeleaf

Asked

Viewed 66 times

0

I am generating a form in the html code of my project and using Thymeleaf to create it. Some form fields are input type because the user needs to enter it, others are select type because there is already that data in the database and the user must select one. The code compiles but shows error that parameters are not present, however I cannot see the error snippet.

I’m basing myself on the documentation: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

Part of html code (select only):

<div>
                    <label for = "professor">Professor</label>
                    <select name = "professor">
                        <option th:each = "professor : ${professor}"
                            th:value = "${professor.prof}"
                            th:selected = "${professor.prof == selecionado.prof}"
                            th:text ="${professor.prof}">
                    </select>   
            </div>

            <div>
                    <label for = "disciplina">Disciplina</label>
                    <select name = "disciplina">
                        <option th:each = "disciplina : ${disc}"
                            th:value = "${disc.id_disciplina}"
                            th:selected = "${disc.id_disciplina}"
                            th:text ="${disc.id_disciplina}">
                    </select>   
            </div>

Chunk of code that is mapped to the path in question:

@GetMapping(value = "")
    @ResponseBody
    public ModelAndView cadastraTurma(@RequestParam ("professor") Long prof, @RequestParam ("disciplina") String id_disciplina){
        ModelAndView turmasCadastro = new ModelAndView("/turmasCadastro");
        Professor professor = new Professor();
        professor = profDao.getOne(prof);
        Disciplina disc = new Disciplina();
        disc = discDao.getOne(id_disciplina);
        turmasCadastro.addObject("professor", professor);
        turmasCadastro.addObject("selecionado", new Professor());
        turmasCadastro.addObject("disc", disc);
        return turmasCadastro;
    }
  • 1

    Hello Juliana, could show error message, maybe it would make it easier to understand the problem?

  • Thank you! To solve the problem I changed the parameter of the cadastraTurma method to no parameters and the method that is associated with it that performs the POST has as parameter the Professor object the annotation @Modelattribute

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.