I'm using JSF (Primefaces) in Liferay and have some problems.
I have list of people (dataTable) with Edit button for every row in dataTable. Users have property that differ one row from others, e.g. football, basketball, tennis, and every user have some of these categories. Depending on those properties, there is different page/form that needs to be filled. So, I have one ManagedBean (PendingRequest.java) that acts like dispatcher/switcher who recieves data about that person in a row and switches user to appropriate form. Form has backing bean (e.g. page signFootballer.xhtml has bean Footballer.java that have getters and setters for fields in page signFootballer.xhtml). So, user clicks on Edit button for some row in a table, method dispatchRequest starts running and checks for category (football,...) and depending on category and initialize new object of class Footballer and sets its properties to fill form (e.g. setName("John"), etc... because it is "edit user" functionality of application).
public String dispatchRequest(Person p) {
if (p.getCategory.equals("Tennis")) {
return "NOT_OK";
} else if (p.getCategory().equals("Basketball")) {
return "NOT_OK";
} else if (p.getCategory().equals("Football")) {
Footballer f = new Footballer();
f.setName("John");
f.setLastName("Doe");
return "OK";
} else {
return "NOT_OK";
}
}
After that, I'm switching to form signFootballer.xhtml via faces-config.xml. And it goes correctly, but problem is that my form is empty.
I'm aware this isn't correct approach but I need advice, can you suggest me what is the best way how to update some row from dataTable having in mind that I need to differ rows by category and then open appropriate page and corresponding backing bean that will fill form. Because I'm not sure it is possible by using this Dispatcher class.
EDIT: @BalusC, This is not real case, but just showing example to help you understand my problem. I have 3 different form, depending on cases for FootballPlayer, BasketballPlayer and TennisPlayer.
signFootballer.xhtml
<h:form enctype="multipart/form-data" id="form">
<div id="container">
<div class="divs">
<p style="margin-left: 22%; width:60%; font-size: 20px; font-weight: bold">Sign contract</p>
<table>
<tr>
<td>
<p:outputLabel for="name" value="First name:*" styleClass="f_text" />
<p:inputText id="name" value="#{footballer.name}" required="true" />
</td>
<td>
<p:outputLabel for="surname" value="Last name:" styleClass="f_text" />
<p:inputText id="surname" value="#{footballer.surname}" required="true" />
</td>
</tr>
</table>
</div>
<div class="submit">
<p:commandButton value="Send" id="submit" update="err_msg1 err_msg2 err_msg3" action="#{Footballer.submitForm}" styleClass="button" />
</div>
</div>
</h:form>
Footballer.java (additional variables and method are deleted)
@ManagedBean
@ViewScoped
public class Footballer {
private String name;
private String surname;
public void submitForm() {
// Validate data and save in database
}
// GETTERS AND SETTERS
pendingRequest.xhtml (it is list of request that hasn't been confirmed yet - from database)
<h:form id="form">
<h:panelGroup id="table-wrapper">
<p:dataTable id="dt" var="req" value="#{pendingRequest.requestList}">
<f:facet name="header">
Pregled prijava
</f:facet>
<p:column headerText="Id">
<h:outputText value="#{req.id}" />
</p:column>
<p:column headerText="Category">
<h:outputText value="#{req.category}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{req.name}" />
</p:column>
<p:column headerText="Prezime">
<h:outputText value="#{req.surname}" />
</p:column>
<p:column headerText="Delete/Edit" width="10%" style="text-align:center" >
<p:commandLink immediate="true" update=":form:table-wrapper" action="#{pendingRequest.deleteRecord(p)}"/>
<p:commandLink immediate="true" action="#{pendingRequest.dispatchRequest(p)}" style="margin-left:5px;"/>
</p:column>
</p:dataTable>
</h:panelGroup>
</h:form>
PendingRequests.java (class which method is run after user choose to edit some request from a list of pending requests)
@ManagedBean
@ViewScoped
public class PendingRequest {
public List<Requests> requestList = new ArrayList<Requests>(); // "Requests" is entity class generated from database
public List<Requests> getRequestList() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
//save example - without transaction
Session session = sessionFactory.openSession();
session.beginTransaction();
List<Requests> tempList = session.createCriteria(Requests.class).list();
session.getTransaction().commit();
return tempList;
}
public void setRequestList(List<Requests> requestList) {
this.requestList = requestList;
}
public void deleteRecord(Requests p) {
Session session = HibernateUtil.getSessionFactory().openSession();
try {
session.beginTransaction();
session.delete(p);
session.getTransaction().commit();
} catch (HibernateException ex) {
ex.printStackTrace();
session.getTransaction().rollback();
}
}
public void dispatchRequest(Requests p) {
if (p.getCategory().equals("Football")) {
//TRANSFER ID OF THAT REQUEST TO Footballer class to populate fields in signFootball.xhtml
} else if (p.getCategory.equals("Basketball")) {
//TRANSFER ID OF THAT REQUEST TO BasketballPlayer class to populate fields in signBasketballPlayer.xhtml
} else if (p.getCategory().equals("Tennis")) {
//TRANSFER ID OF THAT REQUEST TO TennisPlayer class to populate fields in signTennisPlayer.xhtml
} else {
System.out.println("OTHER...");
}
}
Method dispatchRequests is inspecting category (football, basketball, tennis) of request and depending on it, needs to open appropriate form and fill it with values. That is the same form as for new request creation, just now it would be filled with data from database.
So, if user clicks request for Footballer it needs to pass request id probably in Footballer class constructor? Because the fields need to be set before the page is rendered?
That part of this story isn't very clear to me. I'm new to JSF and I would appreciate very much if you could give me some suggestions how to do edit funcionality?