1
I have this controller:
@PostMapping("/salvar")
public String salvar(@Valid OrdemServico ordemServico, BindingResult result,
RedirectAttributes attr) {
if (result.hasErrors()){
return "ordemServico/cadastro";
}
service.save(ordemServico);
attr.addFlashAttribute("success", "Ordem de Servico inserido com sucesso");
return "redirect:/os/cadastrar";
}
in the Thymeleaf template:
<form th:action="@{/os/salvar}"
th:object="${ordemServico}" method="POST">
<div class="form-row">
<div class="form-group col-md-4">
<label for="cliente">Cliente</label>
<select id="cliente" class="form-control" th:field="*{cliente}"
th:classappend="${#fields.hasErrors('cliente')} ? 'is-invalid'">
<option value="">Selecione...</option>
<option th:each="cliente : ${clientes}" th:value="${cliente}"
th:text="${cliente.nome}">Cliente</option>
</select>
<div class="invalid-feedback">
<span th:errors="*{cliente}"></span>
</div>
</div>
</div>
The goal is to register a service order, which relates to a Customer(entity tbm)
public class OrdemServico{
@Id
@GenericGenerator(name="seq_id", strategy="com.web.mja.mja.domain.CodigoOSGenerator")
@GeneratedValue(generator="seq_id")
@Column(unique = true, nullable = false, length = 10)
private String codigo;
@Column
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
private Date dataEntrada;
@Column
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
private Date dataAtualizacao;
@ManyToOne
@JoinColumn(name = "cliente_id_fk")
private Cliente cliente;
@NotNull
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private TipoEquipamento tipo;
@NotNull
@Enumerated(EnumType.STRING)
private Marca marca;
But when choosing a client in the template, fill in other fields and send the request. It gives conversation error. Go a String instead of the chosen client object.
error: Failed to Convert Property value of type java.lang.String to required type com.web.mja.mja.Domain.Client for Property client; nested Exception is java.lang.Illegalstateexception: Cannot Convert value of type java.lang.String to required type com.web.mja.mja.Domain.Client for Property client: no matching Editors or Conversion Strategy found