Problems trying to save data coming from form

2

I can not get the HTML page to enter the name and save. Could someone 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 do not understand this error.

Thymeleaf documentation:

<input type="text" id="datePlanted" name="datePlanted" th:value="*{datePlanted}"
    
asked by anonymous 29.04.2017 / 15:30

2 answers

0

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

Do this:

<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 responsible for creating the required attributes with the given field, so you do not have to worry about the attributes id , name and value , will be created automatically

    
22.05.2017 / 02:15
1

In your HTML, try inserting the name tag, 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 the line:

livros.save(livro);

And make sure the book object is filled in.

    
22.05.2017 / 02:05