Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Como enviar vários objetos via POST para outro controller?

Conforme vou assistindo as aulas e estudando, estou desenvolvendo meu projeto de conclusão de curso da faculdade. Logo na home do meu projeto eu carrego uma lista de objetos (checkbox) do banco, utilizando o each do Thymeleaf, para o usuário selecionar o que for necessário. Como eu consigo passar todos os objetos selecionados pelo usuário para outro controller?

Controller da home:

@Controller
public class HomeController {

@Autowired
private RepositorieIngrediente RepositorieIngrediente;

@GetMapping("/")
public ModelAndView home() {
    ModelAndView mv = new ModelAndView();
    mv.addObject("ingredientes", RepositorieIngrediente.findAll());
    mv.setViewName("home/home");
    return mv;
}
}

Home.html:

<div class="row">
            <div class="teste col-md-6">
                <input type="text" class="form-control pesquisar" id="mySearch" onkeyup="myFunction()" placeholder="Pesquisar ...">
                <form method="POST" action="/ingredientes/listar">
                    <div class="form-group" th:each="ingrediente: ${ingredientes}">
                        <ul id="myMenu">
                            <li>
                                <input class="form-check-input" type="checkbox" name=?????? th:value="${ingrediente.id}" th:id="${ingrediente.nome}"> 
                                <label class="form-check-label" th:for="${ingrediente.nome}" th:text="${ingrediente.nome}"></label>
                            </li>
                        </ul>
                    </div>

            </div>
        </div>
        <button type="submit" class="btn btn-primary">Pesquisar</button>
        </form>
1 resposta
solução!

Fala Guilherme, tudo bem?

Basicamente, no html:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Form</h1>
    <form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
        <p>Id: <input type="text" th:field="*{id}" /></p>
        <p>Message: <input type="text" th:field="*{content}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>

No controller:

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String greetingForm(Model model) {
        model.addAttribute("greeting", new Greeting());
        return "greeting";
    }

    @PostMapping("/greeting")
    public String greetingSubmit(@ModelAttribute Greeting greeting) {
        return "result";
    }

}

https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#creating-a-form

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software