Exception thrown while accessing HTML DataTable in backing bean - jsf

I am building a simple application in JSF with the CrUD functionality. I am trying to implement edit functionality using the tomahawk component .I am unable to retrieve the selected row in my backing bean.
Here's my JSP file snip:
<t:dataTable id="data"
binding="#{selectOneRowList.dataTable}"
styleClass="scrollerTable"
headerClass="standardTable_Header"
footerClass="standardTable_Header"
rowClasses="standardTable_Row1,standardTable_Row2"
columnClasses="standardTable_Column,standardTable_ColumnCentered,standardTable_Column"
var="car"
value="#{selectOneRowList.list}"
sortColumn="#{selectOneRowList.sortColumn}"
sortAscending="#{selectOneRowList.sortAscending}"
preserveDataModel="false"
preserveSort="true"
preserveRowStates="true"
rows="10"
>
<h:column>
<f:facet name="header">
<h:outputText value="Select"/>
</f:facet>
<t:selectOneRow groupName="selection" id="hugo" value="#{selectOneRowList.selectedRowIndex}"
onchange="submit();" immediate="true"
valueChangeListener="#{selectOneRowList.processRowSelection}"/>
</h:column>
<h:column>
<f:facet name="header">
</f:facet>
<h:outputText value="#{car.id}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Cars" />
</f:facet>
<h:outputText value="#{car.type}" />
</h:column>
<t:column sortable="true">
<f:facet name="header">
<h:outputText value="Color" />
</f:facet>
<h:outputText value="#{car.color}" />
</t:column>
</t:dataTable>
Here's my backing bean SelectOneRowList.java :
public void editCar(ActionEvent event) {
System.out.println("Row number ## " + _selectedRowIndex.toString() + " selected!");
System.out.println("Datatable ::"+ dataTable);
System.out.println("Row Count ::" + dataTable.getRowCount());
dataItem = (SimpleCar) getDataTable().getRowData();
//selectedCar = (SimpleCar) _list.get(Integer.parseInt(_selectedRowIndex.toString()));
selectedCar = (SimpleCar) dataTable.getRowData();
System.out.println(dataTable.getRowData());
}
My DTO{Data Transfer Object} which is SimpleCar.java contains the variables ID, type, color and their respective setters/getters.
The dataItem variable is of type "SimpleCar". The dataTable is of type HTMLDataTable. I am able to get the the first 3 SOP's but the 4th SOP isn't printed. I receive the following exception on the server :
javax.faces.el.EvaluationException: Exception while invoking expression #{selectOneRowList.editCar}
org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:156)
javax.faces.component.UICommand.broadcast(UICommand.java:89)
javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:97)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:171)
org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:95)
org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:70)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:139)
org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:341)
On click of the edit button the editCar method in my backing bean is invoked. I need to get the data of the selected row in my backing bean. Why is the exception occurring ?
The above example is taken from the tomawhawk examples WAR distributed on the website. I have gone through many links including the ones on BalusC but none of them have helped.
Regards,

To use DataTable.getRowData() you would have to put the button tied to editCar() within the data table. So, on your JSP would need to add another column, such as:
...
<t:column sortable="true">
<f:facet name="header">
<h:outputText value="Color" />
</f:facet>
<h:outputText value="#{car.color}" />
</t:column>
<t:column sortable="false">
<h:commandButton value="Edit" action="#{selectOneRowList.editCar}" />
</t:column>
</t:dataTable>

Related

How can i transfer a column value to my managed bean?

I am using a datatable in JSF:
<h:dataTable var="dataItem" value="#{operationsBean.creneauxMedecin}" border="1">
<f:facet name="header">
<h:outputText value="Rendez-vous de #{operationsBean.medecin.titre} #{operationsBean.medecin.prenom} #{operationsBean.medecin.nom} le #{operationsBean.txtJour}"></h:outputText>
</f:facet>
<h:column>
<f:facet name="header">
<h:outputText value="Créneau horaire"></h:outputText>
</f:facet>
<h:outputText id="id" value="#{dataItem.hdebut}h#{dataItem.mdebut}-#{dataItem.hfin}h#{dataItem.mfin}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Client"/>
</f:facet>
<h:outputText value="#{operationsBean.clt }"/>
</h:column>
</h:dataTable>
Please how can i transfer a column's(((dataItem.id))) value to my managed bean ?
N.B dataItem is a datatable row that contains(id,version,hdebut,mdebut....)
You have to implement an action listener method in your managed bean and call that via a command link and pass a param as part of the tag, if you google you can see many examples.
<h:commandLink value="click me" actionListener="#{managedbean.actionListenerMethod}">
<f:param name="paramInternalId" value="#{dataTableIter.id}" />
</h:commandLink>
http://docs.oracle.com/javaee/5/tutorial/doc/bnaqd.html#bnaqj

Binding a subtable inside a datatable with JSF

My product is using JSF with Primefaces 2.2. What I need is to process a sub-datatable inside another datatable. The code is as below.
<h:form>
<p:dataTable id="myInvoiceTable"
value="#{globalBean.invoices}" var="invoice">
<p:column>
<f:facet name="header">
<h:outputText value="Bill ID" />
</f:facet>
<h:outputText value="#{invoice.billId}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Item" />
</f:facet>
<h:outputText value="#{invoice.item}" />
</p:column>
<p:rowExpansion>
<h:selectOneMenu id="reasons" value="#{invoiceAction.reason}">
<f:selectItem itemLabel="Reason 1" itemValue="Reason 1"></f:selectItem>
<f:selectItem itemLabel="Reason 2" itemValue="Reason 2"></f:selectItem>
</h:selectOneMenu>
<p:inputText id="activity_amount" value="#{invoiceAction.activity_amount}"/>
<p:commandButton value="PROCESS" action="invoice_process" update="actions_table">
</p:commandButton>
<p:dataTable id="actions_table" value="#{invoice.actions}" var="invAction">
<p:column>
<h:outputText value="#{invAction.reason}" />
</p:column>
<p:column>
<h:outputText value="#{invAction.activity_amount}" />
</p:column>
</p:dataTable>
</p:rowExpansion>
</p:dataTable>
Here's my bean.
public class Invoice {
private int billId;
private String item;
private List<InvoiceAction> actions;
// Getter & setter here
}
public class InvoiceAction {
private String reason;
private double activity_amount;
private int billId;
// Getter & setter here;
}
But when the HTML is generated, it's not processed as desired. Only the select menu in the last row of datatable can receive the data chosen by user, others can't. Because all select menus in all rows are using the same object (#{invoiceAction}), and the bean just gets the value of the last component in the page. I knew the root cause but I don't know how to solve it. How to let they submit in the specified row?
First of all, I think your Invoice and InvoiceAction classes look just like normal Java objects. I feel that they should not be Managed Bean.
Besides, you're right that your #{invoiceAction} is not correctly binded to one of the actions in List<InvoiceAction> actions. Since you are using nested <p:dataTable>, I think you can take advantage of <p:cellEditor>. It would be something like this:
<p:rowExpansion>
<p:dataTable id="actions_table" value="#{invoice.actions}" var="invAction">
<p:column>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{invAction.reason}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu id="reasons" value="#{invoiceAction.reason}">
<f:selectItem itemLabel="Reason 1" itemValue="Reason 1" />
<f:selectItem itemLabel="Reason 2" itemValue="Reason 2" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{invAction.activity_amount}" />
</f:facet>
<f:facet name="input">
<p:inputText id="activity_amount" value="#{invoiceAction.activity_amount}" />
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
<p:commandButton value="PROCESS" action="invoice_process" update="actions_table" />
</p:rowExpansion>

Make h:dataTable cell editable by using h:selectbooleanCheckbox linked to HashMap in the bean

I went through this question from SO
How to use <h:selectBooleanCheckbox> in <h:dataTable> to select multiple rows?
Using the single checkbox as shown in above question i want to find out whether i can make h:datatable cell editable so that user can edit all the rows and columns at once and submit
Here is part of bean class
public class bean {
private List<Group> GroupList;
private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();
public void setChecked(Map<Long, Boolean> checked) {
this.checked = checked;
}
public Map<Long, Boolean> getChecked() {
return checked;
}
}
And here is my JSF page
<h:dataTable id="editTable" styleClass = "listtable" value="#{bean.GroupList}" var="group" border="1" first="0" rows="8" width="75%" frame="hsides" rules="all" cellpadding="5" headerClass="tableheading" rowClasses="firstrow, secondrow">
<f:facet name="header">
<h:outputText value="Groups"></h:outputText>
</f:facet>
<h:column>
<f:facet name="header">
<h:outputText value="GroupId"></h:outputText>
</f:facet>
<h:outputText value="#{group.Id}" rendered=""></h:outputText>
<h:inputText value="#{group.Id}" rendered=""/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="GroupName"></h:outputText>
</f:facet>
<h:outputText value="#{group.Name}" rendered=""></h:outputText>
<h:inputText value="#{group.Name}" rendered=""/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Check to Enable/Disable"></h:outputText>
</f:facet>
<h:selectBooleanCheckbox value="#{bean.checked[group.Id]}" />
</h:column>
</h:dataTable>
What should be kept in rendered attribute so that when it is checked h:inputtext is rendered and when not checked h:outputtext is rendered?
Just bind to the same property. It returns a Boolean anyway. You can use ! or not to negate it.
<h:outputText value="#{group.Id}" rendered="#{!bean.checked[group.Id]}" />
<h:inputText value="#{group.Id}" rendered="#{bean.checked[group.Id]}" />
...
<h:outputText value="#{group.Name}" rendered="#{!bean.checked[group.Id]}" />
<h:inputText value="#{group.Name}" rendered="#{bean.checked[group.Id]}" />

Null Pointer exception while getting request parameters

<h:dataTable id="dt1" value="#{StudentMark.stuList}" var="stuList" bgcolor="#9AC8E6" border="10" cellpadding="5" cellspacing="3" rows="18" width="120%" dir="LTR" frame="hsides>
<h:column>
<f:facet name="header">
<h:outputText style=""value="Student Number" />
</f:facet>
<h:outputText style="" value="#{stuList.stuNumber}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Date"/>
</f:facet>
<h:outputText value="#{stuList.date}">
<f:convertDateTime type="date" pattern="dd-MM-yyyy"/></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
<h:outputText value="#{stuList.stuName}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Division"/>
</f:facet>
<h:outputText value="#{stuList.division}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Annual Marks"/>
</f:facet>
<h:outputText value="#{stuList.annualMark}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Student History"/>
</f:facet>
<h:form>
<h:commandButton id = "historyBtn" value="Student History" action="#{stuBean.showHistory}">
<f:param name="sNumber" value="#{stuBean.stuNumber}" />
<f:param name="sName" value="#{stuBean.stuName}" />
</h:commandButton></h:form>
</h:column>
</h:dataTable>
When i try to get the parameters passed using
FacesContext context = FacesContext.getCurrentInstance();
Map requestMap = context.getExternalContext().getRequestParameterMap();
String studentNum = (String) requestMap.get("sNumber");
String studentName = (String) requestMap.get("sName");
its showing "null" in studentName and studentNum thereby resulting with a null pointer exception.
Any ideas to resolve this???
I've resolved my issue.
The mistake made was I've trying to pass the parameters at the page load itself,tried making the param value as value="#{stuList.stuNumber}" and got the error resolved.
<h:form>
<h:commandLink id = "historyBtn" value="Student History" action="#{stuBean.showHistory}">
<f:param name="sNumber" value="#{stuList.stuNumber}" />
<f:param name="sName" value="#{stuList.stuName}" />
</h:commandLink></h:form>
Thanks for all the professionals for their time.
You are mixing POST and GET requests. h:form and h:commandbutton are for POST requests. f:param is for GET requests. If you want a GET request use h:button or h:link
For a quick overview of GET support look here it also explains how you can bind the parameter to a bean without having to go to the request parameter map.
I think f:param works only in h:commandLink and h:outputLink but not in h:commandButton.

JSF - Pagination implementation using Tomahawk

I am trying to implement Pagination in JSF using Tomahawk. When the page is loaded, the table in the first page is populated with data. But, when I click on Next or Prev the table is empty. I don't see any javascript error in the browser. Following is the code from the jsp. Please let me know if I am missing something:
<t:dataTable id="tableid" value="#{SiteSearchCriteria.siteList}"
var="item" border="1" rows="5" rowIndexVar="rowIndex"
rowClasses="trobg1,trobg2">
<h:column>
<f:facet name="header">
<h:outputText value="siteName"></h:outputText>
</f:facet>
<h:outputLabel value="#{item.siteName}"></h:outputLabel>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="siteCLLI"></h:outputText>
</f:facet>
<h:outputLabel value="#{item.siteCLLI}"></h:outputLabel>
</h:column>
</t:dataTable>
<t:dataScroller id="dataScrollerId" for="tableid" fastStep="10"
pageIndexVar="pageIndex" renderFacetsIfSinglePage="true"
pageCountVar="pageCount" paginator="true" paginatorMaxPages="9"
immediate="true">
<f:facet name="first">
<t:outputText value="First"></t:outputText>
</f:facet>
<f:facet name="last">
<t:outputText value="Last"></t:outputText>
</f:facet>
<f:facet name="previous">
<t:outputText value="Previous"></t:outputText>
</f:facet>
<f:facet name="next">
<t:outputText value="Next"></t:outputText>
</f:facet>
</t:dataScroller>
The backing bean that I was using was in "request" scope. Hence, the data was not visible when I clicked on "Next". Changed the scope of backing bean to "Session" and things started to work.

Resources