how to use lazy loading, not putting business logic in the getter - jsf

i learned through searching that i shouldn't be putting the business logic in the getters but i really dont know how to fix it. I read Why JSF calls getters multiple timesand i used but i still dont get the result that i want .
<h:form id="form">
<h:panelGrid columns="2" cellpadding="5">
<h:outputText value="Nom personnel : " style="font-size: 18px" />
<p:selectOneMenu value="#{personnelBean.nom}" >
<f:selectItem itemValue="#{null}" itemLabel="--Séléctionner Nom" />
<f:selectItems value="#{personnelBean.listePersonnels}" var="personnel"
itemValue="#{personnel.nom}" itemLabel="#{personnel.nom}" />
<f:ajax listener="#{personnelBean.submit()}" render="display"/>
</p:selectOneMenu>
</h:panelGrid>
<h:panelGrid columns="2" cellpadding="5">
<h:outputText value="Prénom personnel : " style="font-size: 18px" />
<p:selectOneMenu value="#{personnel1Bean.cin}" id="display" >
<f:selectItem itemValue="#{null}" itemLabel="--Séléctionner Nom" />
<f:selectItems value="#{personnel1Bean.listePersonnels}" var="personnel"
itemValue="#{personnel.cin}" itemLabel="#{personnel.prenom}" />
<f:ajax listener="#{personnel1Bean.submit()}" render="dis"/>
</p:selectOneMenu>
</h:panelGrid>
<h:outputText value="Nombre d'enfants : "/>
<h:outputText id="dis" value="#{personnel1Bean.nombreEnfants}" />
</h:form>
Personnel1Bean.java
private Integer loadNombreEnfants() {
String cin = submit();
System.out.println("CIN" + cin);
Personnel p = personnelMetier.getPersonnel(cin);
nombreEnfants = p.getNombreEnfants();
System.out.println(nombreEnfants);
return nombreEnfants;
}
public Integer getNombreEnfants() {
return nombreEnfants;
}
public void ajaxListener(AjaxBehaviorEvent event) {
nombreEnfants = loadNombreEnfants();
}

Related

Having trouble presenting an error free screen with Multiple SelectOneMenu drop-downs that are dependent on the previous choice

Newby here. Would appreciate some direction or assistance. Thanks in advance.
If the user clicks the Submit button before all choices have been made the screen error's with a NullPointerException.
HTML
<p:panel header="Wip-Emails Employee Assignment List">
<p:panelGrid id="pg1" columns="4" styleClass="veaGrid: semialert"
cellpadding="5">
<h:outputText value="WIP Departments:" />
<p:selectOneMenu style="width:300px" value="#{wipe.wipDepartments}"
var="dpt" id="wipDpt" converter="omnifaces.SelectItemsConverter"
required="true" requiredMessage="Please select a Department" >
<f:selectItem itemLabel="1st - Select a Department" noSelectionOption="true" itemValue="#{null}"/>
<f:selectItems value="#{wipe.wipdepartments}" var="dpts"
itemLabel="#{dpts.chf_dept_cd} - #{dpts.chf_dept_des}"
itemValue="#{dpts}" />
<p:column>
<h:outputText value="#{dpt.chf_dept_cd}" />
</p:column>
<p:column>
<h:outputText value="#{dpt.chf_dept_des}" />
</p:column>
<f:ajax listener="#{wipe.loadEmployeeList}" render="wipUsr" />
</p:selectOneMenu>
<h:outputText value="Department Employees:" />
<p:selectOneMenu style="width:300px" value="#{wipe.wipUsers}"
var="usr" id="wipUsr" converter="omnifaces.SelectItemsConverter"
required="true" requiredMessage="Please select an Employee">
<f:selectItem itemLabel="2nd - Select an Employee" noSelectionOption="true" itemValue="#{null}"/>
<f:selectItems value="#{wipe.wipusers}" var="usr"
itemLabel="#{usr.sec_usr_id} - #{usr.cicl_lng_nm}"
itemValue="#{usr}" />
<p:column>
<h:outputText value="#{usr.sec_usr_id}" />
</p:column>
<p:column>
<h:outputText value="#{usr.cicl_lng_nm}" />
</p:column>
<f:ajax listener="#{wipe.loadSystemGroupCodes}" render="wipGrp" />
</p:selectOneMenu>
<h:outputText value="System Groups:" />
<p:selectOneMenu style="width:300px" value="#{wipe.wipGroup}"
var="grp" id="wipGrp" converter="omnifaces.SelectItemsConverter"
required="true" requiredMessage="Please select a System Code">
<f:selectItem itemLabel="3rd - Select a System Code" noSelectionOption="true" itemValue="#{null}"/>
<f:selectItems value="#{wipe.wipgroups}" var="grp"
itemLabel="#{grp.ctc_tag_cd} - #{grp.tag_grp_cd}"
itemValue="#{grp}" />
<p:column>
<h:outputText value="#{grp.ctc_tag_cd}" />
</p:column>
<p:column>
<h:outputText value="#{grp.tag_grp_cd}" />
</p:column>
<f:ajax listener="#{wipe.loadwipActivityIDs}" render="wipacy" />
</p:selectOneMenu>
<h:outputText value="Activity Reference IDs" />
<p:selectOneMenu style="width:300px" value="#{wipe.wipActivityId}"
var="acy" id="wipacy" converter="omnifaces.SelectItemsConverter"
required="true" requiredMessage="Please select a Reference ID">
<f:selectItem itemLabel="4th - Select a Reference ID" noSelectionOption="true" itemValue="#{null}"/>
<f:selectItems value="#{wipe.wipactivityids}" var="acy"
itemLabel="#{acy.acy_id} - #{acy.acy_txt}" itemValue="#{acy}" />
<p:column>
<h:outputText value="#{acy.acy_id}" />
</p:column>
<p:column>
<h:outputText value="#{acy.acy_txt}" />
</p:column>
</p:selectOneMenu>
</p:panelGrid>
<p:commandButton value="ADD"
action="#{wipe.addEmailAssignment}"
update=":form1:messages"
process="pg1"
icon="ui-icon-check" />
</p:panel>
Bean
private ArrayList<WipDepartments> wipdepartments;
private WipDepartments wipDepartments;
private ArrayList<WipUsers> wipusers;
private WipUsers wipUsers;
private ArrayList<WipGroups> wipgroups;
private WipGroups wipGroup;
private ArrayList<WipActivityID> wipactivityids;
private WipActivityID wipActivityId;
private String wipGroupID;
private String userID;
private String activityID;
private String clientID;
private String deptId;
--
public void loadEmployeeList(AjaxBehaviorEvent event) {
WipDepartments wipDept = wipDepartments;
setWipusers(BillingUtilsDAO.getWipUsers(wipDept.getChf_dept_id()));
}
public void loadSystemGroupCodes(AjaxBehaviorEvent event){
setWipgroups(BillingUtilsDAO.getWipGroups());
}
public void loadwipActivityIDs(AjaxBehaviorEvent event){
WipGroups wipgrp = wipGroup;
setWipactivityids(BillingUtilsDAO.getWipActivityID(wipgrp.getTag_grp_cd(), wipgrp.getCtc_tag_cd()));
}

confirmDialog with data from SelectOneMenu

I want to show the data selected previously (from p:selectOneMenu) by user before to submit.
i use "p:SelectOneMenu", when i click Submit i have the values in the "#Postconstruct method".
<h:form>
<h:panelGrid id="grid" columns="2">
<p:selectOneMenu id="atmo" value="#{selectOneMenuView.ac_mo}"
required="true" style="width:125px">
<p:ajax listener="#{selectOneMenuView.verif_acmo}"/>
<f:selectItem itemLabel="Select One" itemValue="nothing" />
<f:selectItem itemLabel="Yes" itemValue="Yes" />
<f:selectItem itemLabel="No" itemValue="No" />
</p:selectOneMenu>
<p:selectOneMenu id="nb" value="#{selectOneMenuView.bless}" converter="nbConverter"
panelStyle="width:80%" effect="fade" required="true"
var="b" style="width:720px" filter="true" filterMatchMode="contains">
<f:selectItem itemLabel="Bless ..." itemValue="#{null}" />
<f:selectItems value="#{selectOneMenuView.blessures}" var="bless"
itemLabel="#{bless.code_nb} || #{bless.libelle_nb}" itemValue="#{bless}"/>
<p:column style="width:10%">
<h:outputText value="#{b.code_nb}" />
</p:column>
<p:column>
<h:outputText value="#{b.libelle_nb}" />
</p:column>
</p:selectOneMenu>
</h:panelGrid>
<p:commandButton value="Submit" actionListener="#{selectOneMenuView.btn}" update="grid growl"
style="margin-left:25px" icon="ui-icon-check" oncomplete="PF('confirmDlg').show()">
<p:confirm header="Confirmation" message="Are you sure ?" icon="ui-icon-alert" />
</p:commandButton>
<p:confirmDialog global="true" showEffect="fade" hideEffect="fade" widgetVar="confirmDlg" >
<p:outputLabel value="value N1 : #{selectOneMenuView.ac_mo}"/>
<p:outputLabel value="value N2 : #{selectOneMenuView.bless.libelle_nb}"/>
<p:commandButton value="Confirm" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="Cancel" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
</p:panel>
</h:form>
i'm working with "#RequestScoped" from "javax.faces.bean.RequestScoped" in my managedbean.
NB : after validation (submit) i have the right values. so setters and getters works perfectly
NB 2 : same problem with "#ViewScoped" and "#SessionScoped"
this is the code of managedbean
#ManagedBean
#SessionScoped
public class SelectOneMenuView {
// ---- begin : ac mo
private String ac_mo;
// Getter and Setter of ac_mo
// ---- end : ac mo
// ----- Begin Blessure
private Nb bless;
private List<Nb> blessures;
#ManagedProperty("#{blessureBean}")
private NbBean nbbean;
// getters and setters of Blessure's elements
// ----- end Blessure
// ------ begin : postconstruct
#PostConstruct
public void init() {
blessures = nbbean.getBlessures();
this.ac_mo = "nothing";
}
// ------ end of postconstruct
public void btn()
{
// some stuff
}
}
How will that work with a request scoped bean? Please change your bean to view scoped so that the data is retained between requests.
solved with this :
<p:selectOneMenu>
... do something ...
<p:ajax event="change" update="confirmdialog" />
</p:selectonemenu>

p:commandButton inside p:dialog does not work

I have this problem and I search here and some pages on Google and none of the answers worked for me.
I have the following dialog:
<!-- Dialog Fechar Pedido -->
<p:dialog header="Fechar Pedido" widgetVar="clsPedido" id="clsPedido" minWidth="550">
<h:form prependId="true" id="frmClsPedido">
<p:panel id="pnlDialogClsPedido" header="Informe a forma de pagamento">
<h:panelGrid id="grdClsPedido" columns="2" cellpadding="5">
<p:outputLabel value="Valor Total:." />
<p:outputLabel value="R$ #{pedidoMB.totalPedido}" size="5" style="color: red;">
<f:convertNumber currencySymbol="R$" integerOnly="true" pattern="#0.00" locale="pt_BR" minFractionDigits="1" minIntegerDigits="1" />
</p:outputLabel>
<p:outputLabel value="Valor Recebido:." />
<pe:inputNumber id="vlrRecebido" value="#{pedidoMB.vlrRecebido}" minValue="0" required="true" onblur="returnValue(this.value)" requiredMessage="Informe o valor recebido!" decimalPlaces="2" decimalSeparator="," thousandSeparator="." />
<p:outputLabel value="Pagamento em:." />
<p:selectOneRadio id="frmPagamento" value="#{pedidoMB.frmPagamento}" onchange="daTroco(this.value);" required="true" requiredMessage="Informe a forma de pagamento!">
<f:selectItem itemLabel="Dinheiro" itemValue="DIN" />
<f:selectItem itemLabel="Débito" itemValue="DEB" />
<f:selectItem itemLabel="Crédito" itemValue="CRED" />
<f:selectItem itemLabel="Vale Refeição" itemValue="REF" />
</p:selectOneRadio>
<p:outputLabel value="Valor Troco:." />
<p:inputText value="#{pedidoMB.vlrTroco}" size="5" style="color: blue;" id="vlrTroco" widgetVar="vlrTroco" readonly="true">
<f:convertNumber currencySymbol="R$" integerOnly="true" pattern="#0.00" locale="pt_BR" minFractionDigits="1" minIntegerDigits="1" />
</p:inputText>
</h:panelGrid>
<h:messages></h:messages>
<div align="right">
<p:commandButton icon="ui-icon-disk" actionListener="#{pedidoMB.doFecharPedido}" />
</div>
</p:panel>
</h:form>
</p:dialog>
And I have the following method at my mbean:
public void doFecharPedido(ActionEvent event) {
if (getId() != null) {
Pedido p = getService().findById(getId());
getService().fecharPedido(getVlrRecebido(), getVlrTroco(), p);
}
}
I already tried removing the mbean ActionEvent but nothing seems to work.
Why not simply write
<p:commandButton icon="ui-icon-disk" action="#{pedidoMB.doFecharPedido}" />
and
public void doFecharPedido() {
if(getId() != null) {
Pedido p = getService().findById(getId());
getService().fecharPedido(getVlrRecebido(), getVlrTroco(), p);
}
}
?
The problem was a validation error. For some reason the inputNumber from PF extension does not accept a paste value or some other form of automatic fill of the field.

JSF buttons doesn't send data to bean

I have some JSF page and some bean, in cae that it is editing curent ellement i use update otherwice i use save methods.
I tryed to debug and think methods on buttons never executes
Here is my JSF page:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>Manage panel</title>
</h:head>
<h:body>
<h3>Add/edit patient</h3>
<h:form>
<c:set var="patient" value="#{manageBean.patient}" />
<c:choose>
<c:when test="#{patient!=null}">
<p:panel id="panel1" header="Patient" style="margin-bottom:10px;">
<h:panelGrid columns="2">
<h:outputLabel for="name" value="First name" />
<p:inputText id="firstName" required="true" value="#{patient.firstName}" />
<h:outputLabel for="name" value="Family name" />
<p:inputText id="familyName" required="true" value="#{patient.familyName}" />
<h:outputLabel for="name" value="Sex" />
<p:selectOneMenu id="sex" value="#{patient.sex}">
<f:selectItems value="#{manageBean.sex}"/>
</p:selectOneMenu>
<h:outputLabel for="name" value="Birthday date" />
<p:calendar value="#{patient.birthdayDate}" mode="inline" id="birthdayDate"/>
<h:outputLabel for="name" value="Nationality" />
<p:selectOneMenu id="nationality" value="#{patient.nationality}">
<f:selectItems value="#{manageBean.nationality}"/>
</p:selectOneMenu>
<h:outputLabel for="name" value="Adress" />
<p:inputText id="adress" required="true" value="#{patient.adress}" />
<h:outputLabel for="name" value="Phone number" />
<p:inputMask id="phoneNumber" required="true" value="#{patient.phoneNumber}" mask="(999) 999-9999"/>
</h:panelGrid>
</p:panel>
<p:commandButton value="Update" type="submit" action="#{manageBean.update(patient)}" />
</c:when>
<c:otherwise>
<p:panel id="panel2" header="Patient" style="margin-bottom:10px;">
<h:panelGrid columns="2">
<h:outputLabel for="name" value="First name" />
<p:inputText id="firstName" required="true" value="" />
<h:outputLabel for="name" value="Family name" />
<p:inputText id="familyName" required="true" value="" />
<h:outputLabel for="name" value="Sex" />
<p:selectOneMenu id="sex" value="">
<f:selectItems value="#{manageBean.sex}"/>
</p:selectOneMenu>
<h:outputLabel for="name" value="Birthday date" />
<p:calendar mode="inline" id="birthdayDate"/>
<h:outputLabel for="name" value="Nationality" />
<p:selectOneMenu id="nationality" value="">
<f:selectItems value="#{manageBean.nationality}"/>
</p:selectOneMenu>
<h:outputLabel for="name" value="Adress" />
<p:inputText id="adress" required="true" value="" />
<h:outputLabel for="name" value="Phone number" />
<p:inputMask id="phoneNumber" required="true" value="" mask="(999) 999-9999"/>
</h:panelGrid>
</p:panel>
<p:commandButton value="Save" action="#{manageBean.save(firstName, familyName, sex, birthdayDate, nationality, adress, phoneNumber)}" />
</c:otherwise>
</c:choose>
</h:form>
</h:body>
</html>
Here is my bean methods:
public String update(Patient patient) {
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.update(patient);
tx.commit();
} catch (HibernateException ex) {
if (tx != null) {
tx.rollback();
}
ex.printStackTrace();
} finally {
session.close();
}
return "go_home";
}
public String save(String fitstName, String familyName, Sex sex, Date birthdayDate, Nationality nationallity, String adress, String phoneNumber){
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Patient patient = new Patient();
patient.setFirstName(fitstName);
patient.setFamilyName(familyName);
patient.setSex(sex);
patient.setBirthdayDate(birthdayDate);
patient.setNationality(nationallity);
patient.setAdress(adress);
patient.setPhoneNumber(phoneNumber);
session.save(patient);
tx.commit();
} catch (HibernateException ex) {
if (tx != null) {
tx.rollback();
}
ex.printStackTrace();
} finally {
session.close();
}
return "go_home";
}
Once u give the value attribute in your input tags it ll directly bind the given values to the java class. Create the class variables in ur java class and bind the input tags. EX:-
In your bean class create variables like
private String firstName
and create the getter and setter methods
<f:view>
<h3>Add/edit patient</h3>
<h:form>
<p:panel id="panel1" header="Patient" style="margin-bottom:10px;">
<h:panelGrid columns="2">
<h:outputLabel for="name" value="First name" />
<p:inputText id="firstName" required="true" value="#{manageBean.firstName}" />
<h:outputLabel for="name" value="Family name" />
<p:inputText id="familyName" required="true" value="#{manageBean.familyName}" />
<h:outputLabel for="name" value="Sex" />
<p:selectOneMenu id="sex" value="#{manageBean.sex}">
<f:selectItems value="#{manageBean.sex}"/>
</p:selectOneMenu>
<h:outputLabel for="name" value="Birthday date" />
<p:calendar value="#{manageBean.birthdayDate}" mode="inline" id="birthdayDate"/>
<h:outputLabel for="name" value="Nationality" />
<p:selectOneMenu id="nationality" value="#{manageBean.nationality}">
<f:selectItems value="#{manageBean.nationality}"/>
</p:selectOneMenu>
<h:outputLabel for="name" value="Adress" />
<p:inputText id="adress" required="true" value="#{manageBean.adress}" />
<h:outputLabel for="name" value="Phone number" />
<p:inputMask id="phoneNumber" required="true" value="#{manageBean.phoneNumber}" mask="(999) 999-9999"/>
</h:panelGrid>
</p:panel>
<p:commandButton value="Update" type="submit" action="#{manageBean.update}" />
</h:form>
Create a new jsp page and copy-paste these codes inside body tag.
In your java class create the variables and getter setter methods.
Update should be a method which returns a string.

selecting item from primeface's selectOneMenu not working

I'm having problem with getting the selected item from a selectOneMenu.Here is my JSF code:
<h:form id="mainfrm">
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="Basic Usage: " />
<p:selectOneMenu id="domaine" value="#{projet.currentDomaines}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{projet.initDomaines()}" var="d" itemValue="#{d}" itemLabel="#{d.libelleDomaine}" />
<p:ajax update="formEquipe" process="mainfrm" event="change" />
</p:selectOneMenu>
</h:panelGrid>
</h:form>
<h:form id="formEquipe">
<h:panelGrid id="display" columns="2" cellpadding="4">
<f:facet name="header">
<p:graphicImage value="/images/cars/xxxx.jpg"/>
</f:facet>
<h:outputText value="Domaine name :" />
<h:outputText value="#{projet.currentDomaines.libelleDomaine}"/>
<h:outputText value="Director :" />
<h:outputText value="#{projet.currentDomaines.nomDirecteur}" />
</h:panelGrid>
</h:form>
it seems like everything is right but i must be missing something... so i tested by changing the currentDomaines (object type Domaines) by text (String) and it worked, and here is the code :
<h:form id="mainfrm">
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="Basic Usage: " />
<p:selectOneMenu id="domaine" value="#{projet.text}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{projet.initDomaines()}" var="d" itemValue="#{d.libelleDomaine}" itemLabel="#{d.libelleDomaine}" />
<p:ajax update="formEquipe" process="mainfrm" event="change" />
</p:selectOneMenu>
</h:panelGrid>
</h:form>
<h:form id="formEquipe">
<h:panelGrid id="display" columns="2" cellpadding="4">
<f:facet name="header">
<p:graphicImage value="/images/cars/xxxx.jpg"/>
</f:facet>
<h:outputText value="Domaine name :" />
<h:outputText value="#{projet.text/>
</h:panelGrid>
</h:form>
and here is my backing bean:
public class ProjetsBean implements Serializable {
private DomainesService domainesService;
private Domaines currentDomaines;
private String text;
/////////////// setters & getters \\\\\\\\\\\\\\\\\\\
public void setCurrentDomaines(Domaines currentDomaines) {
this.currentDomaines=currentDomaines;
}
public Domaines getCurrentDomaines() {
return currentDomaines;
}
public void setText(String text) {
this.text=text;
}
public Integer getText() {
return text;
}
///////////////// Méthodes \\\\\\\\\\\\\\\
#PostConstruct
public List<Domaines> initDomaines() {
return domainesService.getAllDomaines();
}
}
The selection from a html selectbox will always be returned to the server as string. If you want to use objects in h:selectOneMenu you need a converter.
There is a comprehensive tutorial on that topic: "Objects in h:selectOneMenu".
For the most cases you can do it without an converter, but this is not wrong.
Here is an example:
<h:selectOneMenu value="#{bean.selectedObject.id}">
<f:selectItems value="#{bean.listOfObjects}" var="item" itemLabel="#{item.title}" itemValue="#{item.id}" />
</h:selectOneMenu>
Another idea is to use the hashcode instead of the ID (but in both: itemValue and value).

Resources