Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Não consigo realizar o cadastro

Ao clicar no botão cadastrar, não acontece nada na página. Também não some as informações.

Segue o códico da classe FilmeController:

package br.com.alura.screenmatch.controller;
import br.com.alura.screenmatch.domain.filme.DadosCadastroFilme;
import br.com.alura.screenmatch.domain.filme.Filme;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/filmes") 

public class FilmeController {

private List<Filme> filmes = new ArrayList<>();

 @GetMapping("/formulario") 
 public String carregaPaginaFormulario(){

     return "filmes/formulario";

 }
@GetMapping 
public String carregaPaginaListagem(Model model){ 
     model.addAttribute("Lista" , filmes); 

    return "filmes/listagem";

}

 @PostMapping 
 public String cadastraFilme(DadosCadastroFilme dados){
     var filme = new Filme(dados);
     filmes.add(filme);

  return "filmes/listagem";

} }

Códico Classe formulário:

 <!DOCTYPE html>
 <html lang="en">
 <head>
<meta charset="UTF-8">
<title>Cadastro de filme</title>
</head>
<body>
 <h1> Cadastro de filme</h1>
<fomr method="post" action="/filmes">
<label for="nome">Nome:</label>
<input id="nome" name="nome">

<br/>

<label for="duracao">Duração (em minutos):</label>
<input id="duracao" name="duracao">

<br/>

<label for="ano">Ano de lançamento:</label>
<input id="ano" name="ano">

<br/>

<label for="genero">Gênero:</label>
<input id="genero" name="genero">

<br/>

<input type = "submit" value="Cadastrar">
</fomr>
</body>
</html>

Códico da classe listagem:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
 <meta charset="UTF-8">
<title>Lista de filmes</title>
</head>
<body>
<h1> Lista de filmes</h1>
<table>
<thread>
    <tr>
        <th>NOME</th>
        <th>DURAÇÃO</th>
        <th>ANO DE LANÇAMENTO</th>
        <th>GÊNERO</th>
    </tr>
</thread>
<tbody>
<tr th:each="filme : ${lista}">
  <td th:text="${filme.nome}"></td>
  <td th:text="${filme.duracaoEmMinutos}"></td>
  <td th:text="${filme.anoLancamento}"></td>
  <td th:text="${filme.genero}"></td>
 </tr>
 </tbody>
 </table>
</fomr>
 </body>
  </html>
3 respostas

Oi!

Você tem essa linha de código do método carregaPaginaListagem no controller:

model.addAttribute("Lista" , filmes); 

Nela você adicionou no model uma variável chamada Lista (com a letra L em maiúsculo). Mas na página listagem.html, o loop está assim:

<tr th:each="filme : ${lista}">

Ou seja, está percorrendo uma variável chamada lista (com a letra l em minúsculo) e com isso o Thymeleaf não encontra a variável e a tabela aparece vazia.

Altere para:

<tr th:each="filme : ${Lista}">

Veja se resolve o problema.

Realizei a alteração, mas ainda continua sem salvar as informações. Quando eu clico no botão cadastrar, não acontece nada.

solução!

Sua tag <form> está escrita de maneira incorreta:

<fomr method="post" action="/filmes">

Está invertida as letras m e r da palavra form. Corrija também no fechamento da tag

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