Hello !
I'm developing a WebApp using this example where I:
1. Connect to MySQL and download data.
2. Populate dataTable with downloaded data using.
3. Fetch(view button) the record and display its details in a dialog.
Problem begins when I download the records and click on the viewButton(no matter what record) it displays in a dialog only the first downloaded record.
My problem is in line:
<f:setPropertyActionListener value="#{car}" target="#{tableBean.selectedCar}" />
The aplication pattern:
jsp -> managedBean -> sessionBean -> entityBean
My code:
JSP:
<h:form id="form">
<p:growl id="msgs" showDetail="true" />
<p:dataTable
id="dataTable"
selection="true"
value="#{cars.getCarList()}"
var="car"
style="font-size: 12px">
<f:facet name="header"> //header
<p:commandButton
value="Download all Cars"
id="button"
update="dataTable"
actionListener="#{car.getAllQuery()}"
/>
</f:facet>
<p:column>
<f:facet name="header">
<h:outputText value="id" />
</f:facet>
<h:outputText value="#{car.id}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{car.name}" />
</p:column>
<p:column style="width:4%">
<p:commandButton
id="selectButton"
update=":form:dataTable:tabView"
oncomplete="carDialog.show()"
title="View"
icon="ui-icon-search">
** <f:setPropertyActionListener
value="#{car}"
target="#{cars.getSelected(car)}"
/> //main problem**
</p:commandButton>
<p:dialog header="Details"
widgetVar="carDialog"
width="1000" height="700"
id="carDialog"
closable="true"
>
<p:tabView id="tabView" >
<p:tab
id="tab1"
title="Details" >
<h:panelGrid columns="2">
<p>Id: #{car.id}</p>
<p>Name: #{car.name}</p>
</h:panelGrid>
</p:tab>
<p:tab id="tab2" title="SomeText">
<h:panelGrid columns="2" cellpadding="10">
<h:outputText
id="tab2Text"
value="Some text"
/>
</h:panelGrid>
</p:tab>
</p:tabView>
</p:dialog>
</p:column>
</p:dataTable>
</h:form>
ManageBean:
@Named
@RequestScoped
public class Cars implements Serializable {
@EJB(name="carBean")
private CarBean carEJB;
private CarEntity car;
private List<CarEntity> carList;
Car(){
List<CarEntity> list = new ArrayList();
}
public List<CarEntity> getCarList(){
return carList;
}
public CarEntity getSelected(CarEntity car){
car = carEJB.get(car);
return car;
}
public void getAll(){
carList = carEJB.getCars();
}
}
SessionBean:
@Stateless
@LocalBean
public class CarBean {
@PersistenceContext(name="CarsSystemPU")
private EntityManager em;
public List<carEntity> getCars(){
Query query = em.createNamedQuery("CarsEntity.findAll");
return query.getResultList();
}
public CarEntity get(CarEntity car){
return em.find(CarEntity.class, car);
}
}
EntityBean:
@Entity
@Table(name = "cars")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "CarEntity.findAll", query = "SELECT p FROM CarEntity p"),
@NamedQuery(name = "CarEntity.findById", query = "SELECT p FROM CarEntity p WHERE p.id = :id"),
@NamedQuery(name = "CarEntity.findByName", query = "SELECT p FROM CarEntity p WHERE p.name = :name");
public class CarEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 11)
@Column(name = "id")
private String id;
@Size(max = 45)
@Column(name = "name")
private String name;
public CarEntity() {
}
public CarEntity(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}