Mazmart
Mazmart

Reputation: 2813

How to collect multiple related properties in a single backing bean property?

Is there a way to replace this in backing bean

private int room1ad
private int room1ch
private int room1ch1
private int room1ch2
private int room1ch3
private int room1ch4
// getters & setters

and this in the view

<h:form>
  <h:selectOneMenu value="#{hotelsController.room1ad}">
      <f:selectItem itemLabel="1" itemValue="1"/>
      <f:selectItem itemLabel="2" itemValue="2"/>
      <f:selectItem itemLabel="3" itemValue="3"/>
  </h:selectOneMenu>
  <h:selectOneMenu value="#{hotelsController.room1ch}">
      <f:selectItem itemLabel="1" itemValue="1"/>
      <f:selectItem itemLabel="2" itemValue="2"/>
      <f:selectItem itemLabel="3" itemValue="3"/>
  </h:selectOneMenu>
  <h:selectOneMenu value="#{hotelsController.room1ch1}">
      <f:selectItem itemLabel="1" itemValue="1"/>
      <f:selectItem itemLabel="2" itemValue="2"/>
      <f:selectItem itemLabel="3" itemValue="3"/>
 </h:selectOneMenu>
 ......
 </h:form>

This doesn't look so bad, but I have 10 rooms in one backing bean.

I thought about something like this in backing bean

//BB
private Room room1

And the view basically the same, but it would create object after submition so the way it works instead of having 6 ints for each room in BB I would only have x Room classes inside and XHTML form would make directly POJO instead of accessing individually each int.

Upvotes: 0

Views: 165

Answers (1)

mrembisz
mrembisz

Reputation: 12880

EL supports lists and properties on POJOs, so you could easily use it:

public List<Room> getRooms();

and xhtml:

<ui:repeat value="#{hotelsController.rooms}" var="room">
  <h:selectOneMenu value="#{room.ad}">
      <f:selectItem itemLabel="1" itemValue="1"/>
      <f:selectItem itemLabel="2" itemValue="2"/>
      <f:selectItem itemLabel="3" itemValue="3"/>
  </h:selectOneMenu>
  .
  .
</ui:repeat>

Upvotes: 2

Related Questions