How to use input type using a List in thymeleaf?

I really don't know how to use a List of an object in a form using Spring MVC and Thymeleaf. If the attribute of the object isn't a List, I know how to code the input type, but using List I think that I need to write different.

I've created a class named "Pessoa" (Person) and another named "Endereço" (Address)

@Entity
public class Pessoa {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int idPessoa;
    @NotEmpty(message = "Nome da pessoa é obrigatório")
    private String nome;
    private String rg;
    @Enumerated(EnumType.STRING)
    private TipoEstado rgEstado;
    private String cpf;
    @DateTimeFormat(pattern="dd/MM/yyyy")
    @Temporal(TemporalType.DATE)    
    private Date dataNascimento;
    private String profissao;
    private String nomePai;
    private String nomeMae;
    @Enumerated(EnumType.STRING)
    private TipoEstadoCivil estadoCivil; //enum
    private String obs;
    @OneToMany(mappedBy = "pessoa", targetEntity = Endereco.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<Endereco> endereco;

Endereço (Address)

@Entity
public class Endereco {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long idEndereco;
    private String logradouro;
    private String numero;
    private String complemento;
    private String bairro;
    private String cep;
    @Enumerated(EnumType.STRING)
    private TipoEstado estado;
    @Enumerated(EnumType.STRING)
    private TipoEndereco tipoEndereco;
    private String cidade;
    private String pais;
     @ManyToOne
     @JoinColumn(name="idPessoa")
     private Pessoa pessoa;

I've implemented the getters and setters in both class.

My problem is when I want to do a input in the field "endereço"

For instance, when I do an input for "name", the code above works:

            <div class="form-group" th:classappend="${#fields.hasErrors('nome')} ? has-error">
                <label for="nome" class="col-sm-2 control-label">Nome</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" id="nome" th:field="*{nome}" />
                    </div>
            </div>

I've tried to do the same thing with Endereco (Address), which is a List of Address. For instance:

            <div class="form-group">
                <label for="nome" class="col-sm-2 control-label">Logradouro</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" id="logradouro" th:field="*{endereco.logradouro}" />
                    </div>
            </div>

I've realised that the attribute "nome" is a simple string from Pessoa, but the attribute "endereco" is a list of Endereco, also inserted in the class Pessoa. Maybe I need to code different using a list, but I really don't know when to start.

I don't know if is important show method in Controller, but is like this:

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

    ModelAndView mv = new ModelAndView(CADASTRO_VIEW);

    mv.addObject(new Pessoa());
    return mv;
}

Upvotes: 0

Views: 1625

Answers (2)

Elizaveta
Elizaveta

Reputation: 471

You can form select by thymeleaf like this:

<div class="form-group">
    <label class="col-sm-2 control-label">Endereco </label>
    <div class="col-sm-4">
        <select th:field="*{endereco}">
            <option th:each="e: ${endereco}"
                    th:value="${e.enderecoId}"
                    th:text="${e.logradouro} + ', ' + ${e.numero}"/>
        </select>
    </div>
</div>

Upvotes: 0

glytching
glytching

Reputation: 47905

Posting the OP's solution (taken from the comments above):

Sorry, probably i wasnt clear, I want to input the "endendeco" values in a form, I've use this solution and works well:

<div class="form-group"> 
  <label for="logradouro" class="col-sm-2 control-label">Logradouro</label>
  <div class="col-sm-4">
    <input type="text" class="form-control" id="logradouro" th:field="*{endereco[0].logradouro}" /> 
  </div> 
</div>

Upvotes: 1

Related Questions