I'm a beginner in primefaces framework, I want that my commandButton verify if the selected item is "All" to call a specific method allBooks() , and if another item is choosen: to call another method : loadBook()
<p:selectOneMenu value="#{bookBean.selectedBook.id}">
<f:selectItem itemLabel="Select a book :" itemValue="" />
<f:selectItem itemLabel="All" />
<f:selectItems value="#{bookBean.selectedBooksItems}" />
<p:ajax execute="bookSelect" event="change" listener="#{bookBean.loadBook}" />
</p:selectOneMenu>
<p:commandButton id="validate" action="#{bookBean.requestBook}" value="Validate"/>
Do it in your actionListener method
<p:selectOneMenu value="#{bookBean.selection}">
<f:selectItem itemLabel="Select a book :" itemValue="#{null}" />
<f:selectItem itemLabel="All" itemValue="#{'ALL'}" />
<f:selectItems value="#{bookBean.options}" />
<p:ajax/>
</p:selectOneMenu>
<p:commandButton actionListener="#{bookBean.loadButtonActionListener}" value="Load"/>
public void loadButtonActionListener(ActionEvent event){
if(this.selection.equals("ALL")) {
this.allBooks();
} else {
this.loadBook(this.selection);
}
}
commandButton ajaxified by default so this snippet work as you expect:
<h:form>
<p:selectOneMenu value="#{myBean.selected}">
<f:selectItem itemLabel="ALL" itemValue="ALL" />
<f:selectItem itemLabel="NONE" itemValue="NONE" />
</p:selectOneMenu>
<p:commandButton value="Validate" actionListener="#{myBean.doAction}" />
</h:form>
here is declared bean:
#Named
#RequestScoped
public class MyBean {
private String selected;
public MyBean() {
}
public String getSelected() {
return selected;
}
public void setSelected(String selected) {
this.selected = selected;
}
public void doAction() {
if (selected.equals("ALL")) {
System.out.println("ALL Called!");
} else if (selected.equals("NONE")) {
System.out.println("NONE Called");
}
}
}
UPDATE:
if you want to add ajax change event to selectOneMenu just nest this line in selectOneMenu element
<p:ajax listener="#{myBean.doAction}" />
Related
h:selectOneMenu is not calling the listener method in the backing bean.
<h:selectOneMenu class="form-control" id="catSoc1"
value="#{product.itemIdSelected}"
valueChangeListener="#{product.itemChangeListener1}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{product.items}" var="catprod"
itemLabel="#{catprod.name}" itemValue="#{catprod.id}" />
<p:ajax event="change" update="opl11 sco12 imgId pageImgId sco13 opl2" />
backing bean method is:
public void itemChangeListener1(ValueChangeEvent event) {
selectedItem = (String) event.getNewValue();
if (selectedItem != null) {
item = productUploadService.getItemOnId(selectedItem);
if (item != null) {
setTotalPages(item.getTotalpages());
}
}
}
This question already has an answer here:
Ajax update/render does not work on a component which has rendered attribute
(1 answer)
Closed 6 years ago.
I'm new to JSF and Primefaces and wanted to know what the best solution is to render a component according to radio selection (hiding and showing again)? I posted my ManagedBean and my JSF-Page-Excerpt. Thanks in advance.
My JSF-Page:
<p:dialog widgetVar="komponentErstellenDialogWV" modal="true"
id="komponentErstellenDialog" header="Komponente erstellen">
<h:form>
<p:wizard flowListener="#{userWizard.onFlowProcess}">
<p:tab id="produktAuswahlTab" title="Produkt auswählen">
<p:panel>
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputText value="Produkt:" />
<p:selectOneRadio id="produktAERadio"
value="#{komponenteErstellenWizardController.produktAuswahl}">
<f:selectItem itemValue="1" itemLabel="Neu erstellen" />
<f:selectItem itemValue="2" itemLabel="Aus Liste auswählen" />
<p:ajax event="click" update="produktSelect" />
</p:selectOneRadio>
<p:selectOneMenu id="produktSelect"
rendered="#{komponenteErstellenWizardController.shouldRenderProduktSelect()}"
value="#{komponenteErstellenWizardController.komponente.produkt}">
<f:selectItems
value="#{komponenteErstellenWizardController.findAllProdukt()}"
var="currentProdukt"
itemLabel="#{currentProdukt.hersteller.concat(' ').concat(currentProdukt.name)}"
itemValue="#{currentProdukt.id}" />
</p:selectOneMenu>
</h:panelGrid>
</p:panel>
</p:tab>
</p:wizard>
</h:form>
</p:dialog>
My ManagedBean:
#ManagedBean
#SessionScoped
public class KomponenteErstellenWizardController implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
// Properties
private Komponente komponente = new Komponente();
private String produktAuswahl;
#PostConstruct
public void init() {
produktAuswahl = "1";
}
public String getProduktAuswahl() {
System.out.println("GetProduktAuswahl invoked with returning value: " + produktAuswahl);
return produktAuswahl;
}
public void setProduktAuswahl(String produktAuswahl) {
System.out.println("SetProduktAuswahl invoked with Value: " + produktAuswahl);
this.produktAuswahl = produktAuswahl;
}
public Komponente getKomponente() {
return komponente;
}
public void setKomponente(Komponente komponente) {
this.komponente = komponente;
}
// EJBs
#EJB
KomponenteFacade komponenteFacade;
#EJB
ProduktFacade produktFacade;
#EJB
ProduktArtFacade produktArtFacade;
public List<Produkt> findAllProdukt() {
return produktFacade.findAll();
}
public Boolean shouldRenderProduktSelect() {
System.out.println("Wizard Produktauswahl: " + produktAuswahl);
return "2".equals(produktAuswahl);
}
}
I updated my code according to suggestions:
Replaced faces components by primefaces components
Added a new action method "shouldRenderProduktSelect"
Still not working...
Few things..
1) Add this method to your controller:
public boolean shouldRenderSelect(){
return "2".equals(produktAuswahl);
}
2) Change the rendered attribute:
<h:selectOneMenu id="produktSelect"
rendered="#{komponenteErstellenWizardController.shouldRenderSelect()}"
3) update your html:
<p:selectOneRadio id="produktAERadio"
value="#{komponenteErstellenWizardController.produktAuswahl}">
<f:selectItem itemValue="1" itemLabel="Neu erstellen" />
<f:selectItem itemValue="2" itemLabel="Aus Liste auswählen" />
<p:ajax event="click" update="panelSelect" />
</p:selectOneRadio>
<h:panelGroup id="panelSelect">
<p:selectOneMenu id="produktSelect"
rendered="#{komponenteErstellenWizardController.shouldRenderProduktSelect()}"
...
</p:selectOneMenu>
</h:panelGroup>
The key is to wrap the selectOneMenu within and from ajax update the panelGroup, not the menu.
This question already has answers here:
Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
(5 answers)
Closed 6 years ago.
I have an selectOneMenu and when Its value at 0, I want to "Add" button does not show up. I have rendered for this situation. Here is code ;
<p:panelGrid id="pnlRegister" columns="6" style="width: 95%" >
<p:inputText id="name" class=" width100" value="#{register.name}" />
<p:selectOneMenu id="numberOfChild" value="#{register.numberOfChild}" >
<p:ajax listener="#{register.listenNumberOfChild()}" update="pnlRegister" />
<f:selectItem itemValue="0" itemLabel="0" />
<f:selectItem itemValue="1" itemLabel="1" />
<f:selectItem itemValue="2" itemLabel="2" />
<f:selectItem itemValue="3" itemLabel="3" />
<f:selectItem itemValue="4" itemLabel="4" />
<f:selectItem itemValue="5" itemLabel="5" />
</p:selectOneMenu>
<p:commandButton id="addPerson" value="Add" actionListener="#{register.openPersonDialog(1)}" rendered="#{register.visibleAddPersonButton}" >
<p:ajax event="dialogReturn" update="pnlPerson" />
</p:commandButton>
And Bean code;
public void listenNumberOfChild() {
if (numberOfChild == 0) {
visibleAddPersonButton = false;
} else {
visibleAddPersonButton = true;
}
System.out.println("ListenerNumberOfChild " + numberOfChild);
}
</panelGrid>
Now problem is, when I change numberOfChild, listener works uptades pnlRegister as expected. However server does not keep inputText values. For example before listener works, I write register.name as "John". After listener works, it does not keep it and it gets as null. I hope I could tell what I'm trying and what is problem. So do you have any suggestion ? Also I tried RequestContext.getCurrentInstance().update but same problem occured.
This is how I made it work:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<f:view>
<h:head>
<title>pfaces</title>
</h:head>
<h:form>
<p:panelGrid id="pnlRegister" columns="3" style="width: 95%">
<p:inputText id="name" value="#{register.name}"/>
<p:selectOneMenu id="numberOfChild" value="#{register.numberOfChild}">
<p:ajax process="name numberOfChild"
listener="#{register.listenNumberOfChild()}" update="pnlRegister"/>
<f:selectItem itemValue="0" itemLabel="0"/>
<f:selectItem itemValue="1" itemLabel="1"/>
<f:selectItem itemValue="2" itemLabel="2"/>
</p:selectOneMenu>
<p:commandButton id="addPerson" value="Add"
rendered="#{register.visibleAddPersonButton}"/>
</p:panelGrid>
</h:form>
<h:body>
</h:body>
</f:view>
</html>
and the backing bean:
package biz.tugay.jsfexampla;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class Register {
private boolean visibleAddPersonButton = false;
private int numberOfChild = 0;
private String name;
public boolean isVisibleAddPersonButton() {
return visibleAddPersonButton;
}
public void setVisibleAddPersonButton(boolean visibleAddPersonButton) {
this.visibleAddPersonButton = visibleAddPersonButton;
}
public int getNumberOfChild() {
return numberOfChild;
}
public void setNumberOfChild(int numberOfChild) {
this.numberOfChild = numberOfChild;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void listenNumberOfChild() {
visibleAddPersonButton = numberOfChild != 0;
System.out.println("ListenerNumberOfChild " + numberOfChild);
}
}
Here, you can see it in action: https://youtu.be/IQN3HAejhcM
In p:selectOneMenu loginbranchName value is not set in bean.In selectItems having list of branchName.I'm using listener to select value from list but value not set in getLoginBranchId method....
<h:outputLabel value="Branch Name:*" style="text-align: left;display: block;" rendered="#{loginBean.userLoggedIn}"/>
<p:selectOneMenu value="#{loginBean.loginbranchName}"
rendered="#{loginBean.userLoggedIn}"
style="width:175px;">
<f:selectItem itemLabel="Select" itemValue="0" />
<f:selectItems value="#{loginBean.branchName}" />
<p:ajax event="change" listener="#{loginBean.getLoginBranchId}"/>
</p:selectOneMenu>
login.java
public String getLoginbranchName() {
return loginbranchName;
}
public void setLoginbranchName(String loginbranchName) {
System.out.println("loginbranchName"+loginbranchName);
this.loginbranchName = loginbranchName;
}
public void getLoginBranchId()
{
System.out.println("enter into getloginbranchid");
System.out.println("loginbranchName"+loginbranchName);
int unitId=loginDAO.getLoginBranchId(loginbranchName);
System.out.println("unitId"+unitId);
}
#BalusC was correct regarding the above discussion where, JSF2 can work with List and SelectItem https://stackoverflow.com/tags/selectonemenu/info I think you might be missing
<h:form>
</h:form>
tag.
I have two h:selectOneMenu (1: Countries, 2: Cities). I need to load all cities from one selected country in the cities selectOneMenu using ajax. When I change the value of the countries selectOneMenu my cities selectOneMenu takes a null value from countryBean.selectedCountry.
<h:panelGrid columns="2">
<h:outputLabel for="countries" value="Countries: " />
<h:selectOneMenu converter="omnifaces.SelectItemsConverter"
id="countries" required="true" value="#{countryBean.selectedCountry}">
<f:selectItem itemLabel="Choose country" />
<f:selectItems value="#{countriesBB.findAllCountries()}"
var="country" itemLabel="#{country.name}" />
<f:ajax event="change" render="cities" />
</h:selectOneMenu>
<h:outputLabel for="cities"
value="Cities: " />
<h:selectOneMenu converter="omnifaces.SelectItemsConverter"
id="cities" required="true"
value="#{cityBean.selectedCity}">
<f:selectItem itemLabel="Choose city" />
<f:selectItems value="#{cityBean.findAllCitiesByCountry(countryBean.selectedCountry)}"
var="city" itemLabel="#{city.name}" />
</h:selectOneMenu>
</h:panelGrid>
This is the method that find the cities:
public List<city> findAllCitiesByCountry(Country country) {
List<City> cities = null;
try {
cities = citiesService.findAllCitiesByCountry(country);
} catch (Exception exception) {
logger.debug("Error finding cities.", exception);
}
return cities;
}
I am getting a NullPointerException because countryBean.selectedCountry is always null. What is the right way to do this?
One of the many rules which a JSF starter need to know:
Don't do business logic in a getter method.
Once you try to fix that by keeping your getter methods true getter methods (i.e. do not do anything else than just return property;) and performing the business logic in (post)constructor and/or action(listener) methods, then this particular problem shall disappear.
Here's a kickoff example:
<h:selectOneMenu value="#{bean.country}">
<f:selectItems value="#{bean.countries}" ... />
<f:ajax listener="#{bean.changeCountry}" render="cities" />
</h:selectOneMenu>
<h:selectOneMenu id="cities" value="#{bean.city}">
<f:selectItems value="#{bean.cities}" ... />
</h:selectOneMenu>
With something like this in a #ViewScoped bean:
private Country country; // +getter+setter
private City city; // +getter+setter
private List<Countries> countries; // +getter
private List<Cities> cities; // +getter
#EJB
private SomeService service;
#PostConstruct
public void init() {
countries = service.getCountries();
}
public void changeCountry() {
cities = service.getCities(country);
}