How to send a List from JSF to backing Bean - jsf

I am using a Datatable in JSF1.2 to populate the data received from Seam component/ using a List. The data is getting fetched when I use the list. But when, I want to make the Datatable editable so that the value I am changing on the JSF page can be sent back to the Seam Component/ Backing Bean the list is not passing value to the Seam component/Backing Bean.
I have tried but I am unable to pass the list to Seam component/Backing Bean again.
Below is the code.
JSF code:
<h:dataTable value="#{mainbean.findCustomerlist}" var="findCustomerList">
<h:column>
<f:facet name="header">
<h:outputText value="Sr No" />
</f:facet>
<h:outputText value="#{findCustomerList.id}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Company Name" />
</f:facet>
<h:inputText value="#{findCustomerList.companyName}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Account Number" />
</f:facet>
<h:inputText value="#{findCustomerList.accountNumber}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Contact Number" />
</f:facet>
<h:inputText value="#{findCustomerList.contactNumber}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Contact Name" />
</f:facet>
<h:inputText value="#{findCustomerList.contactName}" />
</h:column>
<br></br>
</h:dataTable>
<h:commandButton value="Update" type="submit" action="#{mainbean.modifyCustomer(findCustomerlist)}" />
Seam Component/Backing Bean Code:
private List<Customer> findCustomerlist=null;
public List<Customer> getFindCustomerlist() {
return findCustomerlist;
}
public void setFindCustomerlist(List<Customer> findCustomerlist) {
this.findCustomerlist = findCustomerlist;
}
public void searchCustomer(ActionEvent actionEvent)
{
findCustomerlist=session.findCustomer(customerName);
}
/* The searchCustomer function works fine and it returns the list to the JSF. But when I use the modifyCustomer function to retrieve the value from JSF then it is not working.*/
public void modifyCustomer(List<Customer> findCustomerlist)
{
session.updateCustomer(findCustomerlist);
System.out.println("Inside modifyCustomer");
System.out.println(findCustomerlist.get(0).getCompanyName());
}

I am not sure what Seam is doing about this, but in normal JSF this is unnecessary. Normally, JSF has already updated the list property during update model values phase. You just has to access it as a local bean property. I.e.
<h:commandButton value="Update" type="submit" action="#{mainbean.modifyCustomer}" />
with
public void modifyCustomer()
{
session.updateCustomer(findCustomerlist);
System.out.println("Inside modifyCustomer");
System.out.println(findCustomerlist.get(0).getCompanyName());
}
should already be enough.
You only need to ensure that the same list is preserved by the backing bean. The code which you've shown as far will fail when the bean is request scoped. If you're on JSF 2.0 you could put the bean in view scope. On JSF 1.x you need to either put bean in session scope (not recommended) or to preload the list in bean's constructor or #PostConstruct based on customerName.

Related

render column in jsf datatable first time only

as per my requirement I have to show a delete button end of row in datattable but restriction is to show only for first row, not in each row.
how to restrict it
belwo is my datatable
<h:dataTable id="datatable" value="#{relationBean.languageDTOList}" var="lang">
<h:column>
<f:facet name="header"> Relation Type Name</f:facet>
<h:outputText value="#{relationBean.relationName}" />
</h:column>
<h:column>
<f:facet name="header"> Value</f:facet>
<h:inputText />
</h:column>
<h:column>
<f:facet name="header">language</f:facet>
<h:outputText value="#{lang.languageName}" />
</h:column>
<h:column>
<f:facet name="header"> Delete</f:facet>
<p:commandLink rendered="" action="#{relationBean.deleteDataTable}" immediate="true" update="#form" process="#this">
<h:graphicImage value="../images/delete.png" />
<f:setPropertyActionListener target="#{relationBean.deleteId}" value="#{var}" />
</p:commandLink>
</h:column>
Bean.java its showing contents of bean
public List<LanguageDTO> getLanguageDTOList() {
System.out.println("RelationBean:getLanguageDTOList:Enter");
CountryList = new ArrayList<Country>();
try {
CountryList.add(countryService.getCountryByCode(city));
List<LanguageDTO> tempLangDTOlst=new ArrayList<LanguageDTO>();
for (Country lang : CountryList) {
LanguageDTO languageDTO = new LanguageDTO();
for (CountryLanguage CLang : lang.getCountryLanguage()) {
languageDTO = new LanguageDTO();
languageDTO.setLanguageName(CLang.getLanguage().getLanguageName());
languageDTO.setLanguageCode(CLang.getLanguage().getLanguageCode());
System.out.println(CLang.getLanguage().getLanguageName());
tempLangDTOlst.add(languageDTO);
}
}
setLanguageDTOList(tempLangDTOlst);
System.out.println("languageDTOList :"+languageDTOList);
} catch (Exception e) {
e.printStackTrace();
}
//BeanUtils.copyProperties(tempLangDTO,languageDTOList);
System.out.println("--------------------Start -------------");
for (LanguageDTO iterable_element : languageDTOList) {
System.out.println(iterable_element.getLanguageName());
}
System.out.println("-------------------- End -------------");
System.out.println("RelationBean:getLanguageDTOList:Exit");
return languageDTOList;
}
Bind the <h:dataTable> to the view via binding, which will reference an UIData component instance and in the rendered attribute just check if UIData#getRowIndex() equals to 0.
<h:dataTable binding="#{table}" ...>
<h:column>
<p:commandLink ... rendered="#{table.rowIndex eq 0}">
(note: do not bind it to a bean like #{bean.table}! the code is complete as-is)
See also:
How does the 'binding' attribute work in JSF? When and how should it be used?
JSF component binding without bean property
You can test if the current element is the same as the first element of your list:
<p:commandLink rendered="#{lang eq relationBean.languageDTOList.get(0)}"> ...
This would works only if languageDTOList is a List.
If you were using p:dataTable instead of the standard JSF DataTable, you could use the rowIndexVar variable to test if your are at the first iteration.

navigation when bean returns objects

Beginner question concerning changing the information in a view (and not going to a different .xhtml page). My .xhtml displays a datatable (which shows a list of people), but I want the user to be able to narrow the amount of info displayed by typing in the start of someones name into an inputtext and then clicking the commandbutton. Both the commandbutton and the datatable execute the same bean method, but if the inputtext has data the sql uses Like (this uses jdbc). My form is below, the problem is that I get an "Unable to find matching navigation case with from-view-id" because my managed bean returns a list of objects, not "success" or "failure". Also, the whole form seems inefficient. This seems like a fairly common scenario - calling managed bean's method that returns objects and not navigation instructions. How do I get rid of the error messages? Is there a better way to do this?
Here is code:
<h:head>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>
<h:body>
<h1>MavenWeb</h1>
<h:form>
<h:panelGrid columns="3">
Select a customer:
<h:inputText id="listName" value="#{customer.listName}"
size="20" >
</h:inputText>
<h:commandButton value="Submit"
action="#customer.getCustomerList()}" />
</h:panelGrid>
<h:dataTable value="#{customer.getCustomerList()}" var="c"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>
<h:column>
<f:facet name="header">
Customer ID
</f:facet>
#{c.customerID}
</h:column>
<h:column>
<f:facet name="header">
Name
</f:facet>
#{c.name}
</h:column>
<h:column>
<f:facet name="header">
Address
</f:facet>
#{c.address}
</h:column>
<h:column>
<f:facet name="header">
Created Date
</f:facet>
#{c.created_date}
</h:column>
</h:dataTable>
</h:form>
</h:body>
First, make sure your managed bean has ViewScope, so only the form values will be affected on every request while you're in the same view. Second, you can add ajax behavior to your <h:commandButton> and render the <h:datatable> with the new values. Third, never put business logic inside your attribute getters, because the JSF framework could make 2 or more calls of those getters (depending on your design).
Using these rules, you can remake your code like this:
#ViewScoped
#ManagedBean
public class Customer {
private String listName;
private List<CustomerDTO> customerList;
public Customer() {
listName = "";
customerList = null; //maybe you can initialize your list here
}
//getters and setters...
//look that this method returns a void (no need to return a navigation rule)
public void fillCustomerList() {
//your method/way to fill the customerList goes here...
//I'll just put a code example
customerList = new ArrayList<CustomerDTO>();
customerList.add(new CustomerDTO(1, "Luiggi Mendoza", "Lima", new Date());
customerList.add(new CustomerDTO(2, "StackOverflow", "Web", new Date());
}
}
The code fragment for your page
<h:form>
<h:panelGrid columns="3">
Select a customer:
<h:inputText id="listName" value="#{customer.listName}" size="20" />
<h:commandButton value="Submit" action="#customer.fillCustomerList}">
<f:ajax execute="#this" render="dtMyDataTable" />
</h:commandButton>
</h:panelGrid>
<h:dataTable id="dtMyDataTable" value="#{customer.customerList}" var="c"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row">
<h:column>
<f:facet name="header">
Customer ID
</f:facet>
#{c.customerID}
</h:column>
<h:column>
<f:facet name="header">
Name
</f:facet>
#{c.name}
</h:column>
<h:column>
<f:facet name="header">
Address
</f:facet>
#{c.address}
</h:column>
<h:column>
<f:facet name="header">
Created Date
</f:facet>
#{c.created_date}
</h:column>
</h:dataTable>
</h:form>
More info on this matter:
Does view scope bean survive Navigation JSF
Learning JSF2: Ajax in JSF – using f:ajax tag

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]}" />

how to pass values from jsf-jsp

How can I pass user entered values from/to JSP where I have used JSF as framework? Here is my code:
<h:dataTable id="dt1" value="#{Ack.list}" var="ack" >
<h:column>
<f:facet name="header">
<h:outputText style=""value=" Number" />
</f:facet>
<h:outputText style="" value="#{ack.number}" ></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Acknowledgement"/>
</f:facet>
<h:inputText value="" > </h:inputText>
</h:column>
<h:column>
<h:outputText value=""/>
<h:commandButton action="" value="Submit"></h:commandButton>
</h:column>
</h:dataTable>
I want to enter some value in the textfield provided for "acknowledgement" and click on submit button. How can I send the user entered "acknowledgment" on button click?
First add a property acknowledgement to the class Ack (at least, the class which represents each item of the datatable, as declared in var attribute of the table).
public class Ack {
private String acknowledgement;
// Add/generate getter and setter.
}
Then bind the input field to that.
<h:inputText value="#{ack.acknowledgement}" />
Ensure that the table is already placed inside a <h:form> and I'd also move the command button outside the table, one button is enough. Bind its action to a backing bean method.
<h:form>
<h:dataTable ...>
...
</h:dataTable>
<h:commandButton action="#{Ack.submit}" value="Submit" />
</h:form>
Finally define the method in the bean. At that point, JSF has already set the submitted values. You just have to save them in the DB or something.
public String submit() {
ackDAO.save(list);
return "outcome";
}

Exception thrown while accessing HTML DataTable in backing bean

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>

Resources