Problems trying to save data from the form

Asked

Viewed 145 times

2

I am not getting through the HTML page to insert the name and save. Could anyone explain what is happening?

@Entity
public class Livro {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String nome;

    @ManyToMany
    private List<Autor> autor;


    @ManyToOne
    private Professor professor;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }
}

My controller:

@Controller
@RequestMapping("/livros")
public class LivroController {


    @Autowired
    private Livros livros;

    @RequestMapping("/novo")
    public ModelAndView novo(){

    return new ModelAndView("Livro");
    }


    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView salvar(Livro livro){
    ModelAndView mv= new ModelAndView("Livro");
    livros.save(livro);

    return mv;
}

HTML page:

<section layout:fragment="conteudo01">
<form action="from-horizontal" method="POST" th:action="@{/livros/novo}" th:object="${livros}">
   <div class="panel panel-default">
      <h1 class="panel-tittle">Casastro de professor</h1>
   </div>
   <div class="panel-body">
      <label for="nome" class="col-sm-1 contrl-label">Matricula:</label>
      <div class="col-sm-2">
         <input type="text" class="from-control"   id="nome"  th:value="*{nome}"/>
      </div>
   </div>
   <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
         <button type="submit" class="btn btn-default">Sign in</button>
      </div>
   </div>
</form>

Error:

Caused by: org.attoparser.Parseexception: Exception evaluating Springel Expression: "name" (template: "Book" - line 31, col 54)

I don’t understand this mistake.

Documentation of the Thymeleaf:

<input type="text" id="datePlanted" name="datePlanted" th:value="*{datePlanted}"

2 answers

1

In your HTML, try to insert the tag name, following example:

<div class="col-sm-2">
     <input type="text" class="from-control" id="nome" name="nome" th:value="*{nome}"/>
</div>

Another detail, try debugging on the line:

livros.save(livro);

And make sure the book object is filled in with the name.

0


You have not added the attribute name as mentioned by @Isaias, another way to fill in the information of a input is using the th:field

Do so:

<section layout:fragment="conteudo01">
<form action="from-horizontal" method="POST" th:action="@{/livros/novo}" th:object="${livros}">
   <div class="panel panel-default">
      <h1 class="panel-tittle">Casastro de professor</h1>
   </div>
   <div class="panel-body">
      <label for="nome" class="col-sm-1 contrl-label">Matricula:</label>
      <div class="col-sm-2">
         <input type="text" class="from-control"  th:field="*{nome}"/>
      </div>
   </div>
   <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
         <button type="submit" class="btn btn-default">Sign in</button>
      </div>
   </div>
</form>

Thymeleaf will be in charge of creating the necessary attributes with the informed field, so you don’t have to worry about the attributes id, name and value, will be created automatically

Browser other questions tagged

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