1
resposta

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'form' available as request attribute

Boa tarde Rodrigo! estou tentando fazer um formulário de cadastro usando html, bootstrap, thymeleaf e spring boot, contudo estou recebendo esse erro direto. dentro do projeto coloquei a minha página index.html dentro de scr/mais/resources/templates meu código está assim:


<form action="#" th:action="@{/pessoa/cadastro}" th:object="${form}" method="POST" class="row gy-2 gx-3 align-items-center">
                                <div class="row mb-3">
                                      <svg xmlns="http://www.w3.org/2000/svg" width="35" height="35" fill="currentColor" class="bi bi-file-earmark-text col-sm-2 col-form-label col-form-label-sm titulo_branco" viewBox="0 0 16 16">
                                          <path d="M5.5 7a.5.5 0 0 0 0 1h5a.5.5 0 0 0 0-1h-5zM5 9.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5zm0 2a.5.5 0 0 1 .5-.5h2a.5.5 0 0 1 0 1h-2a.5.5 0 0 1-.5-.5z"/>
                                          <path d="M9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V4.5L9.5 0zm0 1v2A1.5 1.5 0 0 0 11 4.5h2V14a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h5.5z"/>
                                    </svg>
                                      <div class="col-sm-10">
                                        <input type="text" th:field="*{nome}" id="nome" placeholder="Nome">
                                      </div>
                                </div>
                                <div class="row mb-3">
                                     <svg xmlns="http://www.w3.org/2000/svg" width="35" height="35" fill="currentColor" class="bi bi-envelope col-sm-2 col-form-label col-form-label-sm titulo_branco" viewBox="0 0 16 16">
                                        <path d="M0 4a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V4Zm2-1a1 1 0 0 0-1 1v.217l7 4.2 7-4.2V4a1 1 0 0 0-1-1H2Zm13 2.383-4.708 2.825L15 11.105V5.383Zm-.034 6.876-5.64-3.471L8 9.583l-1.326-.795-5.64 3.47A1 1 0 0 0 2 13h12a1 1 0 0 0 .966-.741ZM1 11.105l4.708-2.897L1 5.383v5.722Z"/>
                                     </svg>
                                      <div class="col-sm-10">
                                         <input type="email" id="email" th:field="*{email}" placeholder="email@seuemail.com.br">
                                      </div>
                                </div>
                                <div class="row mb-3">
                                     <svg xmlns="http://www.w3.org/2000/svg" width="35" height="35" fill="currentColor" class="bi bi-telephone col-sm-2 col-form-label col-form-label-sm titulo_branco" viewBox="0 0 16 16">
                                      <path d="M3.654 1.328a.678.678 0 0 0-1.015-.063L1.605 2.3c-.483.484-.661 1.169-.45 1.77a17.568 17.568 0 0 0 4.168 6.608 17.569 17.569 0 0 0 6.608 4.168c.601.211 1.286.033 1.77-.45l1.034-1.034a.678.678 0 0 0-.063-1.015l-2.307-1.794a.678.678 0 0 0-.58-.122l-2.19.547a1.745 1.745 0 0 1-1.657-.459L5.482 8.062a1.745 1.745 0 0 1-.46-1.657l.548-2.19a.678.678 0 0 0-.122-.58L3.654 1.328zM1.884.511a1.745 1.745 0 0 1 2.612.163L6.29 2.98c.329.423.445.974.315 1.494l-.547 2.19a.678.678 0 0 0 .178.643l2.457 2.457a.678.678 0 0 0 .644.178l2.189-.547a1.745 1.745 0 0 1 1.494.315l2.306 1.794c.829.645.905 1.87.163 2.611l-1.034 1.034c-.74.74-1.846 1.065-2.877.702a18.634 18.634 0 0 1-7.01-4.42 18.634 18.634 0 0 1-4.42-7.009c-.362-1.03-.037-2.137.703-2.877L1.885.511z"/>
                                    </svg>
                                      <div class="col-sm-10">
                                        <input type="text" id="telefone" th:field="*{telefone}" placeholder="(99) 9999-9999">
                                      </div>
                                </div>
                                  <div class="row mb-3">
                                      <div class="col-sm-5">
                                        <button type="submit" class="btn btn-secondary mb-3 ">Enviar</button>
                                      </div>
                                </div>
                            </form>

package br.com.findvet.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import br.com.findvet.controller.form.PessoaTemporariaForm;
import br.com.findvet.service.PessoaTemporariaService;

@Controller
public class PessoaTemporariaController {

    @Autowired
    private PessoaTemporariaService pessoaService;

    @RequestMapping(value="/pessoa/cadastro" , method=RequestMethod.POST)
    public String cadastrar(@ModelAttribute("form") PessoaTemporariaForm form) {
        pessoaService.cadastrar(form);
        return "/";
    }

}
1 resposta

Oi Marcson,

Acho que na sua tag form o atributo th:object deveria ser: th:object="${pessoaTemporariaForm}" que é o nome da classe que tem os campos.

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