confirmDialog with data from SelectOneMenu - jsf

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>

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()));
}

How to hide/show selectOneMenu in JSF? [duplicate]

This question already has an answer here:
Ajax update/render does not work on a component which has rendered attribute
(1 answer)
Closed 7 years ago.
I have an inputText and a selectOneRadio component, when form load selectOneRadio should be hidden. When user select inputText, I want to show the selectOneRadio component. I have made the selectOneRadio hidden in #PostConstruct, which should be display on select inputText.
<h:panelGrid id="panelgrid">
<p:panel id="panel" >
<h:outputLabel value="Name: " for="name" />
<p:inputText id="name" value="#{userBean.name}" immediate="true">
<p:ajax event="onselect" update="city" listener="#{userBean.showName}" />
</p:inputText>
<p:selectOneRadio id="city" value="#{userBean.city}" layout="grid" columns="3" rendered="#{userBean.displayName}" >
<f:selectItems value="#{userBean.cities}" var="c" itemLabel="#{city}" itemValue="#{city}" />
</p:selectOneRadio>
</p:panel>
</h:panelGrid>
The bean code is like:
#PostConstruct
public void init() {
displayName = false;
}
public boolean isShowName() {
return true;
}
...
But some how this is not working. I'm using JSF2.0 with primefaces 5.2.
I found the solution.
<h:form id="frm">
<h:panelGrid id="panelgrid">
<p:panel id="panel" >
<h:outputLabel value="Name: " for="name" />
<p:inputText id="name" value="#{userBean.name}" immediate="true" onkeypress="#{userBean.showName}">
<p:ajax event="change" update="frm:panelgrid" listener="#{userBean.inputcangeeListener}" />
</p:inputText>
</p:panel>
<p:panel id="p2" rendered="#{userBean.displayName}">
<p:selectOneRadio id="city" value="#{userBean.city}" layout="grid" columns="3">
<f:selectItems value="#{userBean.cities}" var="c" itemLabel="#{city}" itemValue="#{city}" />
</p:selectOneRadio>
</p:panel>
</h:panelGrid>
</h:form>
And my managed bean looks like-
#PostConstruct
public void init() {
displayName = false;
}
public boolean isShowName(){
return displayName;
}
public void inputcangeeListener(javax.faces.event.AjaxBehaviorEvent changeEvent){
setDisplayName(true);
cities = new ArrayList<>();
cities.add("pune");
cities.add("KOL");
}
Thanks for your response.

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

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();
}

Value of selectone menu passed through Autocomplete not not being recieved in managed bean

I have a datable to which i can dynamically add rows on click the add button.
<p:dataTable id="ListTable" styleClass="hide-column-names"
value="#{sManagedBean.View.pList}"
var="p" rowIndexVar="rowIndex">
<p:column>
<p:inputText id="pInput" required="#{not empty param[saveButton.clientId]}" value="#{p.name}" />
</p:column>
<p:column>
<p:selectOneMenu id="pSs" value="#{p.sp}">
<p:ajax update="pComplete" global="false" />
<f:selectItem itemLabel="Select" itemValue="0"/>
<f:selectItems value="#{sManagedBean.pList}" var="sp" itemValue="#{sp.id}" itemLabel="#{sp.title}" />
</p:selectOneMenu>
</p:column>
<p:column>
<p:autoComplete id="pComplete" value="#{p.ptitle}" required="#{not empty param[saveButton.clientId]}" completeMethod="#{sManagedBean.pComplete}" global="false" minQueryLength="3">
<p:ajax event="query" global="false" />
<f:attribute name="filter" value="#{p.sp}" />
</p:autoComplete>
</p:column>
<p:column>
<p:commandLink value="Delete" id="delete"
actionListener="#{sManagedBean.removeFromList}"
update="ListTable pSs"
rendered="#sManagedBean.oView.ptList.size()>'1'}">
<f:param name="rowToRemove" value="#{rowIndex}" />
</p:commandLink>
</p:column>
</p:dataTable>
<h:outputText/>
<p:commandLink id="addPLink" value="Add"
style="font-weight:bold;"
actionListener="#{sManagedBean.AddP}"
update="ListTable growlQ" global="false">
<h:graphicImage value="../images/addbutton.jpg" />
</p:commandLink>
The managed Bean is
public List<String> pComplete(int query) {
FacesContext context = FacesContext.getCurrentInstance();
int sId = Integer.parseInt(UIComponent.getCurrentComponent(context).getAttributes().get("filter").toString());
System.out.println("s " + sId);
Session session = null;
List<String> result = null;
try {
session = hibernateTemplate.getSessionFactory().openSession();
result = session.createSQLQuery("select title from pb where title like : alphabets and sId =:sId").setString("alphabets", query + "%").setInteger("sId", sId + 1).list();
} catch (Exception e) {
log.warn("FL Warning", e);
}
return result;
}
but the value passed through autocomplete is always zero.
I am not able to get the value of selectone menu of a corresponding row.
This should do the work:
<p:column>
<p:selectOneMenu id="pSs" value="#{p.species}">
<p:ajax update="pComplete" global="false" />
<f:selectItem itemLabel="Select" itemValue="0"/>
<f:selectItems value="#{sManagedBean.pList}" var="sp" itemValue="#{sp.id}" itemLabel="#{sp.title}" />
</p:selectOneMenu>
</p:column>
<p:column>
<p:autoComplete id="pComplete" value="#{p.ptitle}" required="#{not empty param[saveButton.clientId]}" completeMethod="#{sManagedBean.pComplete}" minQueryLength="3">
<p:ajax event="query" global="false" />
<f:attribute name="filter" value="#{p.species}" />
</p:autoComplete>
</p:column>
I got the answer i added ajax in between the selectone menu
<p:ajax event="change" update="pComplete"/>

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