selecting item from primeface's selectOneMenu not working - jsf

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).

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>

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

JSF - PrimeFaces , How to get the value of a booleanCheckbox / booleanButton in a list

I have a jsf page that I can't update the value of some checkboxes in a list of userDamain objects, in fact this is a part of my jsf page :
<h:outputText value="USER's rights list: "></h:outputText>
<p:dataTable id="userss" var="userr" value="#{userMB.userListe}">
<p:column>
<f:facet name="header"><h:outputText value="FirstName" /></f:facet>
<h:outputText value="#{userr.firstName}" />
</p:column>
<p:column>
<h:panelGrid columns="1" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="Basic Usage: " />
<p:selectBooleanCheckbox value="#{userr.deletee}" immediate="true" actionListener="#{userr.deletee}" />
</h:panelGrid>
<p:commandButton value="Submit" update="display" oncomplete="dlg.show()" />
<p:dialog header="Selected Values" modal="true" showEffect="fade" hideEffect="fade" widgetVar="dlg">
<h:panelGrid columns="1" id="display">
<h:outputText value="Value 1: #{userr.deletee}" />
</h:panelGrid>
</p:dialog>
</p:column>
when I click on the boolean button 'Submit', the value of the userr.deletee is always false ( the default value ) and it's not updated anyway, I tried to use the Listner and actionListener in the booleanButton but it doesn't work, I also tried the postValidate event, so if someone has any idea of that, I would be graceful.
this is a part of my managed bean:
private List<UserDomain> userListe ;
public List<UserDomain> getUserListe() {
return this.userListe;
}
public void loadUserListe(){
this.userListe = new ArrayList<UserDomain>();
this.userListe.addAll(getUserServicee().getAll());
}
public void setUserListe(List<UserDomain> userListe) {
this.userListe = userListe;
}
a part of the userDomain bean:
...
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#Transient
private boolean deletee;
public boolean isDeletee() {
return deletee;
}
public void setDeletee(boolean deletee) {
this.deletee = deletee;
}
I found out a solution which seems logic to me, I added to my checkbox an ajax behavior to execute a method and an attribute which called in this method to get the whole userDomain object, so on I can get the user.deletee value, here is a part of the code I m talking about:
<h:panelGrid columns="1" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="Delete Right: " />
<p:selectBooleanCheckbox value="#{userr.deletee}">
<f:ajax execute="#this" listener="#{userMB.saveUserRights}" />
<f:attribute name="user" value="#{userr}" />
</p:selectBooleanCheckbox>
</h:panelGrid>
I hope this will be helpful for you

Resources