Send a list of objects to Spring using Thymeleaf

1

I have the following problem, I have an entity called Teacher and another called Class, teacher is with @OneToMany relationship to Class and when I try to save the marked classes in my checkbox in View the following error is displayed:

  

Error during execution of processor   'org.thymeleaf.spring4.processor.attr.SpringInputCheckboxFieldAttrProcessor'

     

java.lang.NumberFormatException: For input string: "QuintoA"

The entity teacher is shown below:

@Entity
public class Professor {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotBlank(message = "Nome é obrigatório")
@Column(length = 100, nullable = false)
private String nome;

@NotBlank(message = "CPF é obrigatório")
@NaturalId
@Column(length = 15, nullable = false)
private String cpf;

private PosGraduacao posGraduacao;

@Column(name = "desc_pos_graduacao", length = 150)
private String descricaoPosGraduacao;

@OneToMany(mappedBy="professor") 
private Collection<Turma> turmas;

// methods getters and setters ...

}

The entity Class is shown below:

@Entity
@Table(name = "turma")
public class Turma {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotBlank(message = "Nome é obrigatória")
@Size(max = 100, message = "O nome não pode conter mais de 100 caracteres")
@Column(length = 80, nullable = false)
private String nome;

@NotEmpty(message = "Horário inicial é obrigatório")
@Column(length = 6)
private String horarioInicial;

@NotEmpty(message = "Horário final é obrigatório")
@Column(length = 6)
private String horarioFinal;

@Enumerated(EnumType.STRING)
@Column(length = 15, nullable = false)
private Escolaridade escolaridade;

@NotNull(message = "Disciplina obrigatória")
@Enumerated(EnumType.STRING)
@Column(length = 20, nullable = false)
private Disciplina disciplina;

@ManyToOne
@JoinColumn(name = "professor_id")
private Professor professor;

// methods getters and setters ...

}

The TeacherController is shown below:

@Controller
@RequestMapping("/professor")
public class ProfessorController {

@Autowired
private TurmaService turmas;

@Autowired
private ProfessorService professores;

@RequestMapping("/novo")
public ModelAndView novo() {
    ModelAndView mv = new ModelAndView("CadastroProfessor");

    Professor professor = new Professor();
    professor.setTurmas(new ArrayList<Turma>());

    mv.addObject(professor);
    return mv;
}

@RequestMapping(method = RequestMethod.POST)
public String salvar(@Validated Professor professor, Errors errors, RedirectAttributes attributes) {

    if (errors.hasErrors()) {
        return "CadastroProfessor";
    }

    professores.salvar(professor);
    attributes.addFlashAttribute("mensagem", "Professor salvo com sucesso!");

    return "redirect:/professor/novo";
}

The View responsible for selecting the classes is shown below:

<div class="form-group" >

    <label for="turma" class="col-sm-2 control-label" >Turma que atua</label>

    <div class="col-sm-5" >                     

         <label class="checkbox-inline" th:each="turma : ${todasTurmas}" >
            <input type="checkbox" id="turma" th:value="${turma}" th:text="${turma.nome}" th:field="*{turmas}" />
         </label>

    </div>

</div>
    
asked by anonymous 09.02.2017 / 02:23

1 answer

2

The value must be the id attribute, as follows:

th:value="${turma.id}"
    
10.02.2017 / 17:23