Spring Bind's with Collections

0

I have not been able to find a solution on how to solve this question yet, after researching and searching I found something about how I can bind elements from my form to a collection in a POJO. But first I would like to explain the business rule of the module in which I am developing to help the reader to orient himself, so even to indicate another solution if necessary.

I have a form in which inside it I can register "passengers of travel" and later popular a table for when it is submitted to collection to be persisted in bank. Below the image for easy viewing:

SoonIcreatedmycustomerentityinwhichitwillcontainapassengerset:

Customer.java

@EntitypublicclassCustomerimplementsSerializable{privatestaticfinallongserialVersionUID=1L;@Id@GeneratedValue(strategy=GenerationType.IDENTITY)@Column(name="id_customer")
    private Long idCustomer;

    @Column(name="birth_date")
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @Temporal(TemporalType.DATE)
    private Date birthDate;

    @Column(name="email")
    private String email;

    @Column(name="first_name")
    private String firstName;

    @Column(name="gender")
    private String gender;

    @Column(name="last_name")
    private String lastName;

    @OneToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="fk_document")
    private Document document;


    @OneToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="fk_customerPhone")
    private CustomerPhone customerPhone;


    @OneToMany(cascade=CascadeType.PERSIST)
    @JoinColumn(name="fk_customer")
    private Set<Passenger> passenger = new HashSet<Passenger>();

    @OneToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="fk_customerAddress")
    private CustomerAddress customerAddress;


    public Customer() {
    }
    //Getters and Setters
}

Passenger.java

@Entity
public class Passenger implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_passengers")
    private Long id;

    @Column(name="birth_date")
    private String birthDate;

    @Column(name="p_email")
    private String email;

    @Column(name="p_first_name")
    private String firstName;

    @Column(name="p_last_name")
    private String lastName;

    @Column(name="main_tel")
    private String mainTel;


    @Enumerated(EnumType.STRING)
    @Column(name="family_bond")
    private FamilyBond familyBond;


    @OneToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="fk_documentPassenger")
    private DocumentPassenger documentPassenger;


    public Passenger() {
    }
    //Getter and Setter

}

My controller:

@Controller
@Transactional(propagation = Propagation.REQUIRED)
@RequestMapping("/auth")
public class CustomerController {

    @Autowired
    public CustomerFacade customerFacade;

    @RequestMapping("/customer")
    public ModelAndView getMenuService(Model model){
        List<Country> countriesList = customerFacade.getCountriesList();    
        model.addAttribute("countriesList", countriesList);

        return new ModelAndView("customer/newCustomer", "customer", new Customer());
    }

Well, now that the beast catches! Because I'm trying to bind the collection with some components inside my form in the view.

After a bit of searching I found this approach:

<f:form id="transition-duration-demo" class="transition-form" modelAttribute="customer" method="post">
   <div class="aling-form col-sm-12 nest text" style="padding-top:25px">
   <c:forEach items="${customer.passenger}" var="passenger" varStatus="status">

   <div class="box02">
      <f:input id="passenger-name" placeholder="Nome do Passageiro" type="text" path="passenger[${status.index}].firstName" class="form-control"/>
   </div>
   <!--Continua-->
</f:form>

Well, the components do not appear on the screen, but I think the reason for my list is zero!

Can anyone help me with this question, what am I doing in the right way? Has anyone ever used the Spring AutoPopulatingList and can you give me some information?

    
asked by anonymous 14.10.2014 / 20:51

1 answer

1

Well, after the help of a friend of the GUJ forum and a little research I came to the conclusion that to popular my list would have the possibility to do it two ways, AJAX or removing my modal from my form main and creating another form invoking an action in my controller and thus populating a List .

I chose to submit a separate form because I do not have much privacy with AJAX calls yet. Here is the implementation:

                                                                             × Close                 Sign up New Passenger                              

            <div class="aling-form col-sm-12 nest text" style="padding-top:25px">           

                <div class="box01">
                    <select id="familyBond" class="form-control" name="familyBond">
                        <option value="null" label="-- Vinculo do Passageiro --" />
                        <c:forEach items="${listOfBondNames}" var="bond">
                            <option value="">${bond.value}</option>
                        </c:forEach>
                    </select>
                </div>

                    <div class="box02">
                        <input id="passenger-name" placeholder="Nome do Passageiro" name="firstName" type="text" class="form-control"/>
                    </div>

                    <div class="box01">
                        <input id="passenger-lastName" placeholder="Sobrenome do Passageiro" type="text" name="lastName" class="form-control"/>
                    </div>

                    <div class="box02">
                        <div class="input-group ">
                            <span class="input-group-addon btn-success"><i class="fa fontawesome-envelope-alt"></i></span>
                            <input id="passenger-email" placeholder="E-mail" type="text"  name="email" class="form-control"/>
                        </div>
                    </div>

                    <div class="box01">
                        <div class="input-group ">
                            <span class="input-group-addon btn-success"><i class="fa fa-phone-square"></i></span>
                            <input id="passenger-phone" placeholder="Telefone Principal" type="text" name="mainTel" class="form-control"/>
                        </div>
                    </div>

                    <div class="box02">
                        <div class="input-group ">
                            <span class="input-group-addon btn-success"><i class="fa fa-calendar"></i></span>
                            <input id="passenger-birth" placeholder="Data de Nascimento" type="text" name="birthDate" class="form-control"/>
                        </div>
                    </div>

                    <div class="box01">
                        <input id="passenger-rg" placeholder="RG" type="text" name="documentPassenger.rg" class="form-control" style="margin:0px;"/>
                    </div>

                    <div class="box02">
                        <input id="passenger-cpf" placeholder="CPF" type="text" name="documentPassenger.cpf" class="form-control"/>
                    </div>
                    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />

                </div>

          </div>
          <div class="modal-footer clear" style="margin:0px;">
            <button type="button" class="btn btn-danger" data-dismiss="modal">Cancelar</button>
            <button type="submit" class="btn btn-primary">Cadastrar</button>
          </div>
          </form>
        </div>
      </div>
    </div>

Soon he calls the action:

private List<Passenger> passengerList = new ArrayList<Passenger>();

    @RequestMapping(value="/addPassenger", method=RequestMethod.POST)
    public ModelAndView addPassenger(Passenger passenger, Model model){
        passengerList.add(passenger);

        ModelAndView mv = new ModelAndView("service/newService");
        mv.addObject("passengerList", passengerList);
        mv.addObject("customer", new Customer());
        initializeComponents(model);
       return mv;
    }

With this approach I have my passengerList populated for when to submit my main form I apanes setar the list in the bean.

    
22.10.2014 / 14:02