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?