Am working on ViewScoped annotation in jsf but am not getting correct output - jsf-2

I am working on #ViewScoped annotation in JSF2.0. Below is the code what I had written. It is not working as I expected. Please review it and let me know if any changes are required to get the exact output that what am thinking of.
Here is my Controller SearchController.java
package com.s2tech.jsfapp.controllers;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import com.s2tech.jsfapp.helper.SearchHelper;
import com.s2tech.jsfapp.managedbean.SearchManagedBean;
#ManagedBean(name ="searchController")
#ViewScoped
public class SearchController implements Serializable{
public SearchController(){
private static final long serialVersionUID = 1L;
SearchHelper searchHelper;
private List<SearchManagedBean> employeeList=new ArrayList<SearchManagedBean>();
public List<SearchManagedBean> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List<SearchManagedBean> employeeList) {
this.employeeList = employeeList;
}
public void getEmployee(SearchManagedBean searchManagedBean) {
searchHelper=new SearchHelper();
employeeList = searchHelper.getEmployee(searchManagedBean);
//return "search.xhtml";
//return Constants.searchPage;
// return null;
}
Below is my view search.xhtml
<h:body style="background-color:skyblue;">
<ui:include src="mainMenu.xhtml"></ui:include>
<h:form prependId="false">
<table>
<tr style="display:block;margin-top:10px;width:100%;margin-left:40%">
<td style="width:40%">
<p:outputLabel for="firstName" value="First Name:" style="font-weight:bold" />
<p:inputText id="firstName" value="#{searchManagedBean.firstName}" required="true" requiredMessage="please enter First Name"> </p:inputText>
<p:message id="firstNameMsg" for="firstName" showDetail="true" ></p:message>
</td>
<td style="width: 20%"></td>
<td style="width:40%;">
<p:outputLabel for="lastName" value="Last Name:" style="font-weight:bold"/>
<p:inputText id="lastName" value="#{searchManagedBean.lastName}" required="true" requiredMessage="please enter Last Name" />
<p:message id="lastNameMsg" for="lastName" showDetail="true" ></p:message>
</td>
</tr>
<tr style="display:block;margin-top:10px;width:100%;margin-left:78%">
<td style="width:40%"> <p:commandButton value="Search" action="#{searchController.getEmployee(searchManagedBean)}" update="firstNameMsg lastNameMsg emailMsg phoneMsg dataTable"/></td>
</tr>
</table>
</h:form>
<p:dataTable id="dataTable" var="emp" rendered="#{not empty searchController.employeeList}" value="#{searchController.employeeList}" rowStyleClass="table table-striped table-hover table-bordered" resizableColumns="true" resizeMode="expand" scrollWidth="50%" scrollHeight="150" >
<p:column headerText="First Name">
<h:outputText value="#{emp.firstName}"/>
</p:column>
<p:column headerText="Last Name">
<h:outputText value="#{emp.lastName}"/>
</p:column>
<p:column headerText="Email">
<h:outputText value="#{emp.email}"/>
</p:column>
<p:column headerText="Phone">
<h:outputText value="#{emp.phone}"/>
</p:column>
<p:column headerText="Edit">
<p:commandLink value="Edit" action="#{searchController.getEditForm(emp)}" style="margin-left:40px;color:blue;"></p:commandLink>
</p:column>
</p:dataTable>
</h:body>
</html>
My question is that after entering first name and last name, when I click on submit button it need to hit controller's getEmployee() method and it needs to display data table values from controller's getEmployeeList(). Here eventhough using ViewScoped bean Iam not getting output. Any suggestions??

Related

Primefaces confirmDialog doesn't show up after one call

I have a problem with PrimeFaces <p:confirmDialog>, when I click on the <p:commandLink> that must show up the dialog and choose 'No', everything is fine. But when I click on yes,everything works just the first time.
When the process is over and I click again on any <p:commandLink>, the dialog doesn't show up unless I refresh the whole page.I had try everything but I cannot figure out what can be the problem.
<h:form>
<p:dataTable border="0" rules="all" value="#{userBean.users}" var="user" autoUpdate="true" styleClass="table table-hover" paginator="true" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="6,10,15" >
<p:column>
<f:facet name="header">
<h:outputText value="Login" />
</f:facet>
<h:outputText value="#{user.login}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="" />
</f:facet>
<p:commandLink class="badge bg-red marge-left" onclick='PF("cdu#{user.id}").show()' title="Supprimer un utilisateur"><span class="fa fa-trash-o"/></p:commandLink>
<p:confirmDialog ajax="true" message="Voulez vous vraiment supprimer l'utilisateur '#{user.login}'" closable="true" header="Confirmation" severity="alert" widgetVar="cdu#{user.id}" >
<p:commandButton value="oui" actionListener="#{userBean.deleteUser}" update="#form" onclick='PF("cdu#{user.id}").hide()' styleClass="btn btn-primary"/>
<p:commandButton value="non" onclick='PF("cdu#{user.id}").hide()' type="button" styleClass="btn btn-danger" />
</p:confirmDialog>
</p:column>
</p:dataTable>
</h:form>
I had finally resolved my problem. I changed the <p:confirmDialog> by a global one and it works.
<h:form>
<p:dataTable border="0" rules="all" value="#{userBean.users}" var="user" autoUpdate="true" styleClass="table table-hover">
<p:column>
<f:facet name="header">
<h:outputText value="Login" />
</f:facet>
<h:outputText value="#{user.login}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Nom" />
</f:facet>
<h:outputText value="#{user.firstName} #{user.lastName}" />
</p:column>
<p:column style="width:200px;">
<f:facet name="header">
<h:outputText value="Email" />
</f:facet>
<h:outputText value="#{user.email}" />
</p:column>
<p:column >
<f:facet name="header">
<h:outputText value="" />
</f:facet>
<p:commandLink class="badge bg-blue marge-left" action="#{userBean.getUserRowToEdit}" title="Editer un utilisateur" ><span class="fa fa-edit"/></p:commandLink>
<p:commandLink class="glyphicon glyphicon-trash icon-trash" actionListener="#{userBean.deleteUser}" title="Supprimer un utilisateur" update="#form">
<p:confirm header="Confirmation" message="Voulez-vous vraiment supprimer l'utilisateur?" icon="ui-icon-alert"/>
</p:commandLink>
</p:column>
</p:dataTable>
<p:confirmDialog global="true">
<p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check"/>
<p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close"/>
</p:confirmDialog>
</h:form>
I tried your code and repaired it a little. Hope it will work for you too.
-I removed ajax="true"
-usersBean.deleteUser function need to know which user to so -> deleteusersBean.deleteUser(user)
Under is my code that i use to test your case. A few things are changed due to existing beans i have. This code is tested on latest primefaces(5.2).
my xhtml file:
<p:column>
<f:facet name="header">
<h:outputText value="" />
</f:facet>
<p:commandLink class="badge bg-red marge-left" value="delete" onclick='PF("cdu#{user.userId}").show()' title="Supprimer un utilisateur"><span class="fa fa-trash-o"/></p:commandLink>
<p:confirmDialog message="Voulez vous vraiment supprimer l'utilisateur '#{user.name}'" closable="true" header="Confirmation" severity="alert" widgetVar="cdu#{user.userId}" >
<p:commandButton value="oui" actionListener="#{loginBean.deleteUser(user)}" update="#form" onclick='PF("cdu#{user.userId}").hide()' styleClass="btn btn-primary"/>
<p:commandButton value="non" onclick='PF("cdu#{user.userId}").hide()' type="button" styleClass="btn btn-danger" />
</p:confirmDialog>
</p:column>
</p:dataTable>
</h:form>
my loginBean:
public class LoginBean implements Serializable {
private static final long serialVersionUID = 6188046073588310656L;
private List<User> users= new ArrayList<User>();
public LoginBean() {
}
public List<User> getUsers() {
if(users.size()==0){
for (int i= 0; i<3; i++){
User u = new User();
u.setUserId(i);
u.setName("noob"+i);
users.add(u);
}
}
return users;
}
public void deleteUser(User user){
users.remove(user);
}
}

Initializing primefaces datatable rowkey

I have a primefaces datatable that I'm populating with properties of objects in an ArrayList once the user clicks a search button. I need to add a column with a radio button so that the user can select a row of the table to update. I'd like to set the rowkey to be the metadataKey property of my objects, but I get the following error:
rowKey="#{result.metadataKey}": Property 'metadataKey' not found on
type java.lang.String
I believed that this was caused by the metadataKey not existing at the time the table is rendered. To remedy it, I set the table to render only after the user had completed their search, but this resulted in the same error.
Here is the relevant html:
<h:form id="dataForm">
<p:panel styleClass="centered">
<p:messages id="message" showDetails="true" autoUpdate="true" />
<p:outputLabel id="searchLabel" value="Search for Metadata: " />
<p:inputText id="search"
value="#{updateMetadataItemsBean.searchTerm}" />
<p:commandButton id="searchButton" value="Search"
action="#{updateMetadataItemsBean.search}"
update="dataForm:metadataitems, message, dataForm:dataPanel" />
<br />
<br />
<br />
</p:panel>
<p:panel styleClass="center" id="dataPanel" >
<p:dataTable id="metadataitems" var="result"
value="#{updateMetadataItemsBean.results}" styleClass="center"
resizableColumns="true"
selection="updateMetadataItemsBean.selectedMetadataItem"
rowKey="#{result.metadataKey}" rendered="#{updateMetadataItemsBean.show}">
<p:column headerText="Select Item to Update" selectionMode="single"></p:column>
<p:column headerText="Metadata Key">
<h:outputText value="#{result.metadataKey}" />
</p:column>
<p:column headerText="Map Name">
<h:outputText value="#{result.mapName}" />
</p:column>
<p:column headerText="Metadata Name">
<h:outputText value="#{result.metadataName}" />
</p:column>
<p:column headerText="Metadata Value">
<h:outputText value="#{result.metadataValue}" />
</p:column>
<p:column headerText="WTX Adapter">
<h:outputText value="#{result.wtxAdapter}" />
</p:column>
<p:column headerText="Card Number">
<h:outputText value="#{result.cardNumber}" />
</p:column>
<p:column headerText="Create Date TS">
<h:outputText value="#{result.createDateTS}" />
</p:column>
</p:dataTable>
<br />
<br />
<br />
</p:panel>
This is the relevant code from the bean:
public class UpdateMetadataItemsBean implements Serializable{
public static final long serialVersionUID = 4;
private String metadataKey;
private String mapName;
private String metadataName;
private String metadataValue;
private String wtxAdapter;
private String cardNumber;
private String createDateTS;
private String searchTerm;
private String selectedDataKey;
private boolean show = false;
private MetadataItems selectedMetadataItem;
private ArrayList<MetadataItems> results = new ArrayList<MetadataItems>();
[getters and setters]
public void search() {
Search s = new Search();
setResults(s.search(searchTerm));
setShow(true);
}
Thanks in advance to anyone who can point me in the right direction.
Alter
selection="updateMetadataItemsBean.selectedMetadataItem"
to
selection="#{updateMetadataItemsBean.selectedMetadataItem}".

sorting in p:datatable behave unusual

I am using sorting in p:datatable, and last column is edit .
its working fine till sorting is pressed. as soon as I do sorting it behave unusual.
On editing it giving wrong object.
belwo is my datatable.
<h:form>
<h:inputHidden value="#{countryBean.initList}" />
<p:dataTable id="existingCountry1" var="countryLang" value="#{countryBean.myexistingCountryList}" style="width: 100%" styleClass="role_detail_section"
rowStyleClass="activity_white, activity_blue" cellspacing="0" cellpadding="0" border="0" rows="6" paginator="true">
<p:column width="30%" headerText="Country " sortBy="countryName">
<h:outputLabel value="${countryLang.countryName}" />
</p:column>
<p:column width="30%" headerText="Country Code " >
<h:outputLabel value="${countryLang.countryCode}" />
</p:column>
<p:column headerText="Edit" >
<p:commandLink id="editCommandLinkId"
action="#{countryBean.editCountryByCountryCode(countryLang.countryCode,true)}" title="Edit" styleClass="edit_icon" onstart="statusDialog.show();" onsuccess="statusDialog.hide();"/>
</p:column>
</p:dataTable>
suppose I edited for US, then it will show data for AU like that.
for sorting I am using belwo technique
private ArrayList<Country> myexistingCountryList;
public String getInitList() {
myexistingCountryList= getExistingCountryList();
return null;
}
public ArrayList<Country> getExistingCountryList() {
try {
existingCountryList = new ArrayList<Country>();
existingCountryList.addAll(getCountryService().getExistingCountry());
} catch (ServiceException e) {
e.printStackTrace();
errorLogger.error("Error while getExistingCountryList in service layer", e);
}
There are to many mistakes in your code.
Instead of $ use #
Instead of sortBy="countryName" write sortBy="#{countryLang.countryName}"
So basicly
change this
<p:column width="30%" headerText="Country " sortBy="countryName">
<h:outputLabel value="${countryLang.countryName}" />
</p:column>
to this
<p:column width="30%" headerText="Country " sortBy="#{countryLang.countryName}">
<h:outputLabel value="#{countryLang.countryName}" />
</p:column>

How to display contents in reverse order in <h:dataTable>

I am accepting data in Array and displaying it in the same screen using <h:dataTable>.
How can display contents in reverse order, i.e. show the latest entry in the first row?
Below is code can i display nonDox.non_List in reverse order
<h:body>
<h:panelGroup rendered="#{not empty dataBase}">
<h:form id="nonDoxScanForm">
<f:event listener="#{nonDox.validate_AccNo}" type="postValidate" />
<table border="0" class="InnerBox" width="55%">
<tr><th colspan="7" align="Center" class="clsTitle">Non Dox Add</th></tr>
<tr>
<td>Delivery Date</td>
<td >Consginee (F2) </td>
<td>Weight (F3)</td>
<td>SubBranch Code(F4)</td>
<td >Consignment No (F10)</td>
<td align="Center">(F12)</td>
</tr>
<tr>
<td> <p:calendar value="#{nonDox.delDate}" id="fDat" /> </td>
<td><h:inputText size="20" id="fcon" value="#{nonDox.consignee}" /> </td>
<td><h:inputText size="20" id="fweig" value="#{nonDox.weight}" >
</h:inputText> </td>
<td><h:inputText size="20" id="fsub" value="#{nonDox.subBranchCode}" >
<f:validateLength maximum="3"></f:validateLength>
</h:inputText> </td>`enter code here`
<td><h:inputText size="10" id="acno" value="#{nonDox.accNo}" onfocus="this.select()" >
</h:inputText>
</td>
<td> <h:commandButton value="Add" id="fAdd" action="#{nonDox.addAction}" onclick="return validate();" />
</td>
<td> <h:commandButton value="Save" action="#{nonDox.saveAction}" >
</h:commandButton>
</td>
</tr>
<tr>
<td colspan="7">
<h:message for="acno"/>
</td>
</tr>
</table>
<h:dataTable value="#{ nonDox.non_List}" var="o"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row" width="55%"
>
<h:column>
<f:facet name="header">Del Date</f:facet>
#{o.cor_Date.substring(0, 10)}
</h:column>
<h:column>
<f:facet name="header">consignee</f:facet>
#{o.consignee}
</h:column>
<h:column>
<f:facet name="header">Weight</f:facet>
#{o.weight}
</h:column>
<h:column>
<f:facet name="header">Rate</f:facet>
#{o.rate}
</h:column>
<h:column>
<f:facet name="header">subBranchCode</f:facet>
#{o.subBranchCode}
</h:column>
<h:column>
<f:facet name="header" >POD No</f:facet>
#{o.accNo}
</h:column>
<h:column>
<f:facet name="header">Action</f:facet>
<h:commandLink value="Delete" action="#{nonDox.deleteAction(o)}" />
</h:column>
</h:dataTable>
</h:form>
<h:panelGroup >
<h:panelGroup rendered="#{empty dataBase}">
<h1>Session Expired</h1>
<h:link outcome="Login" target="CommonContent" >Login</h:link>
</h:panelGroup>
</h:body>
If you're using an List as in new ArrayList<Item>(), it's easy: use Collections#reverse():
private List<Item> data;
#PostConstruct
public void init() {
data = service.list();
Collections.reverse(data);
}
// ...
Just reference it in <h:dataTable value="#{bean.data}"> the usual way.
If you're using an array as in Item[], then you'd need to convert it to List first with help of Arrays#asList(), so that you can feed it to Collections#reverse():
private List<Item> data;
#PostConstruct
public void init() {
Item[] items = service.array();
data = Arrays.asList(items);
Collections.reverse(data);
}
// ...
Or just rewrite the service method in such way that you don't need to perform this conversion step in the backing bean.
Key is, you shouldn't expect to perform this job in the view. You should just prepare the model in such way that it's exactly as what the view expects. The <h:dataTable> itself doesn't provide any facilities like that.

Backing bean method is getting null from inputs

This project is made with Primefaces 3.5, JSF 2.0, Oracle database.
The view:
<h:form id="schedule" class="schedule">
// more JSF and forms and dialogs
<p:dialog widgetVar="patientDialog" dynamic="true" header="Find Patient" showEffect="clip" hideEffect="explode" modal="true" >
<h:form id="frmPatient">
<h:panelGrid id="findPatient" columns="1">
<h:panelGrid id="findPatientParameters" columns="2">
<h:outputLabel value="Firstname: " />
<p:inputText id="name" label="name" size="15" value="#{waitinglistBean.name}" />
<h:outputLabel value="Patient Code: " />
<p:inputText id="pcode" label="pcode" size="15" value="#{waitinglistBean.pcode}" />
<br />
<p:commandButton value="Search" process="name" actionListener="#{waitinglistBean.patients}" update="patients"/>
</h:panelGrid>
<h:panelGrid id="findPatientDetails" columns="1">
<p:dataTable id="patients" var="patient" resizableColumns="true" scrollable="true" scrollWidth="1250"
scrollHeight="150" value="#{waitinglistBean.patients}"
rowKey="#{patient.PCode}" selection="#{waitinglistBean.selectedPatient}"
selectionMode="single" >
<f:facet name="header">
Click "Select" button after selecting a row to select patient.
</f:facet>
<p:column width="150" headerText="Code">
#{patient.PCode}
</p:column>
<p:column width="150" headerText="Family Name">
#{patient.PLname}
</p:column>
<p:column width="150" headerText="First Name">
#{patient.PFname}
</p:column>
<p:column headerText="Sex" >
#{patient.PSex}
</p:column>
<p:column headerText="Birthdate">
#{patient.PBorn}
</p:column>
<p:column headerText="Street">
#{patient.PStreet}
</p:column>
<f:facet name="footer">
<p:commandButton id="viewButton" value="Select" icon="ui-icon-search"
oncomplete="patientDialog.hide()"
update=":frmDialogs:frmWaiting:tabView:patientDetails"
process="patients"/>
</f:facet>
</p:dataTable>
</h:panelGrid>
</h:panelGrid>
</h:form>
</p:dialog>
<p:dialog widgetVar="patientDialog" dynamic="true" header="Find Patient" showEffect="clip" hideEffect="explode" modal="true" >
<h:form id="frmPatient">
<h:panelGrid id="findPatient" columns="1">
<h:panelGrid id="findPatientParameters" columns="2">
<h:outputLabel value="Firstname: " />
<p:inputText id="name" label="name" size="15" value="#{waitinglistBean.name}" />
<h:outputLabel value="Patient Code: " />
<p:inputText id="pcode" label="pcode" size="15" value="#{waitinglistBean.pcode}" />
<br />
<p:commandButton value="Search" process="name" actionListener="#{waitinglistBean.patients}" update="patients"/>
</h:panelGrid>
<h:panelGrid id="findPatientDetails" columns="1">
<p:dataTable id="patients" var="patient" resizableColumns="true" scrollable="true" scrollWidth="1250"
scrollHeight="150" value="#{waitinglistBean.patients}"
rowKey="#{patient.PCode}" selection="#{waitinglistBean.selectedPatient}"
selectionMode="single" >
<f:facet name="header">
Click "Select" button after selecting a row to select patient.
</f:facet>
<p:column width="150" headerText="Code">
#{patient.PCode}
</p:column>
<p:column width="150" headerText="Family Name">
#{patient.PLname}
</p:column>
<p:column width="150" headerText="First Name">
#{patient.PFname}
</p:column>
<p:column headerText="Sex" >
#{patient.PSex}
</p:column>
<p:column headerText="Birthdate">
#{patient.PBorn}
</p:column>
<p:column headerText="Street">
#{patient.PStreet}
</p:column>
<f:facet name="footer">
<p:commandButton id="viewButton" value="Select" icon="ui-icon-search"
oncomplete="patientDialog.hide()"
update=":frmDialogs:frmWaiting:tabView:patientDetails"
process="patients"/>
</f:facet>
</p:dataTable>
</h:panelGrid>
</h:panelGrid>
</h:form>
</p:dialog>
// more JSF and dialogshizzle
</h:form>
The backing bean:
#ManagedBean(name="waitinglistBean")
#ViewScoped
public class WaitinglistBean implements Serializable
{
private Patients patient;
private OrWaitinglist orWaitinglist;
private List<Patients> patients = new ArrayList<Patients>();
private String name;
private Integer pcode;
public WaitinglistBean() {
}
public void setSelectedPatient(Patients patient) {
this.patient = patient;
}
public Patients getSelectedPatient() {
return patient;
}
public OrWaitinglist getOrWaitinglist() {
return orWaitinglist;
}
public void setOrWaitinglist(OrWaitinglist orWaitinglist) {
this.orWaitinglist = orWaitinglist;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPcode() {
return pcode;
}
public void setPcode(Integer pcode) {
this.pcode = pcode;
}
public List<Patients> getPatients() {
PatientsDao patientsDao = new PatientsDaoImpl();
patients = patientsDao.getSearchPatient(name, pcode);
System.out.println(name + " " + pcode);
return patients;
}
public void createOrWaitinglist()
{
orWaitinglist.setPatients(patient);
}
So first a user enter a string: "Firstname" and an Integer: "Patient Code".
When he hits the "Search" button it fires the actionListener="#{patientBean.patients}
inside that commandbutton. The method needs 2 parameter, name and pcode which have their own setters/getters.
When I look in console it shows me the printout of these 2 parameters, and they are twice "null", so that means the parameters from the JSF pages were not passed to the backing bean, although the value of the inputText has been set to the backingbean set method: value="#{patientBean.name}"
My question now is why do I get these null-values. I've read several posts which use these setters, but still I get null-values.
after edit i still have the same problem;
for send value of input component in h:panelgrid to the backingbean use somthing like this:
<p:commandButton value="Search" process="#(frmPatient:findPatientParameters :input)"
actionListener="#{patientBean.patients}" update="patients"/>

Resources