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);
}
Related
Hi I am currently working on a java project (jsf) with primefaces and I am using enum, but I cannot access its values from the view with primefaces. I have temporarily solved the problem by creating a getter from the Bean and accessing the enum values, but it should work with allSuffix = "ALL_ENUM_VALUES" or ALL_VALUES by default, I don't know if it's a problem with primefaces, joinfaces or something I'm missing. I have looked into the official documentation and it should work... any solution?
my code is
<p:importEnum
type="com.path.enumeration.AltoMedioBajo"
var="AltoMedioBajo"
allSuffix="ALL_ENUM_VALUES" />
<p:outputLabel
for="posibilidad"
value="#{informe_msg.posibilidad}" />
<p:selectOneMenu
id="posibilidad"
style="width: 150px"
value="#{informeSeguimientoDto.specification.posibilidad}">
<f:selectItem
itemLabel=""
itemValue="" />
<f:selectItems
value="#{AltoMedioBajo.ALL_ENUM_VALUES}"
var="posibilidad"
itemValue="#{posibilidad}"
itemLabel="#{peticion_msg[posibilidad.femKey]}" />
</p:selectOneMenu> ```
Hi this question was answered here: How to use enum values in f:selectItem(s)
With joinfaces I tested this example:
User Bean
#Named
#ApplicationScoped
public class UserBean {
public Role[] getRoles() {
return Role.values();
}
}
add_user.xhtml
<p:selectOneMenu id="roleId" value="#{userBean.newUser.roleId}">
<f:selectItem itemValue="" itemLabel=""/>
<f:selectItems value="#{userBean.roles}" var="role" itemValue="#{role.getId()}" itemLabel="#{messages['role_' += role]}"/>
</p:selectOneMenu>
Role Enum with custom order, it's not mandatory you can use default .ordinal() value
public enum Role {
ASSISTANT(1),
ACCOUNTANT(4),
CASHIER(5),
ADMIN(10);
private final int id;
Role(int id) {
this.id = id;
}
public int getId() {
return id;
}
// method to get the Role using Id from the database
public static Role valueOf(int id) {
return Arrays.stream(values())
.filter(role -> role.id == id)
.findFirst()
.orElse(null);
}}
messages_es.properties as Resource Bundle
role_ASSISTANT=Asistente
role_ACCOUNTANT=Contador
role_CASHIER=Cajero
role_ADMIN=Administrador
As per documentation/demo, you should use a repeat tag.
Edited: Try this version:
<p:importEnum type="com.path.enumeration.AltoMedioBajo" var="AltoMedioBajo"
allSuffix="ALL_ENUM_VALUES" />
<p:outputLabel for="posibilidad" value="#{informe_msg.posibilidad}" />
<p:selectOneMenu id="posibilidad" style="width: 150px"
value="#{informeSeguimientoDto.specification.posibilidad}">
<f:selectItem />
<ui:repeat var="posibilidad" value="#{AltoMedioBajo.ALL_ENUM_VALUES}">
<f:selectItem itemValue="#{posibilidad}"
itemLabel="#{peticion_msg[posibilidad.femKey]}"
</ui:repeat>
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.
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}" />
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 use 3 selectOneMenu components in my project. I need to reload content of the second after change in the first. Here are some parts of the files
index.xhtml
<h:form id="form">
<p:selectOneMenu id="Rząd" value="#{birdSelectorBean.selectedState}" effect="fade"
style="width: 150px;">
<f:selectItem itemLabel="Rząd" itemValue="" />
<f:selectItems value="#{birdSelectorBean.rzad}" />
<p:ajax render="#form" listener="#{birdSelectorBean.stateChangeListener}" />
</p:selectOneMenu>
<p:selectOneMenu id="rodzina" value="#{birdSelectorBean.selectedState}" effect="fade"
style="width: 150px;">
<f:selectItem itemLabel="Rodzina" itemValue="" />
<f:selectItems value="#{birdSelectorBean.rodzina}" />
</p:selectOneMenu>
<p:selectOneMenu id="rodzaj" value="#{birdSelectorBean.selectedState}" effect="fade"
style="width: 150px;">
<f:selectItem itemLabel="Rodzaj" itemValue="" />
<f:selectItems value="#{birdSelectorBean.rodzaj}" />
</p:selectOneMenu>
</h:form>
BirdSelectionBean.java:
public class BirdSelectorBean
{
private String selectedState;
private List<SelectItem> rzad;
private List<SelectItem> rodzina;
private List<SelectItem> rodzaj;
public BirdSelectorBean()
{
rzad = new ArrayList<>();
rzad.add(new SelectItem("Rząd_X"));
rzad.add(new SelectItem("Rząd_Y"));
rzad.add(new SelectItem("Rząd_Z"));
rodzina = new ArrayList<>();
rodzaj = new ArrayList<>();
}
public void stateChangeListener(ValueChangeEvent event)
{
rodzina.clear();
rodzina.add(new SelectItem("Rodzina_A"));
rodzina.add(new SelectItem("Rodzina_B"));
rodzina.add(new SelectItem("Rodzina_C"));
}
...
getters and setters
...
}
I read many topics on that but it doesn't work for me. I tried update="rodzina" like it is in example
here
and render option like it is said in that
topic:
But it still doesn't work. Please help me :]
In the p:ajax tag change render="#form" to update="#form". Render is used by f:ajax, primefaces use another name for some reason - see here.
Looks like your stateChangeListener method nevers get called and more important, your managed bean looks like hasn't any scope (at least from your question content), remember that it must be at least #ViewScoped in order to make this work. Also, another problem in your code is that you're using the same attribute to select the data for the three <p:selectOnuMenu> (which is not a problem yet since you haven't achieved what you wanted to begin with).
To make the ajax update work, remove the parameter from your stateChangeListener. Also let's add the other two attributes for the selected items from the dropdownlists.
#ManagedBean
#ViewScoped
public class BirdSelectorBean {
private String selectedState;
private String selectedStateRodzina;
private String selectedStateRodzaj;
//other fields and methods...
public void stateChangeListener() {
rodzina.clear();
rodzina.add(new SelectItem("Rodzina_A"));
rodzina.add(new SelectItem("Rodzina_B"));
rodzina.add(new SelectItem("Rodzina_C"));
}
}
And then update your desired <p:selectOneMenu> in your <p:ajax> call (I removed the non-directly related to the problem attributes from the components like style):
<p:selectOneMenu id="Rząd" value="#{birdSelectorBean.selectedState}">
<f:selectItem itemLabel="Rząd" itemValue="" />
<f:selectItems value="#{birdSelectorBean.rzad}" />
<p:ajax update="rodzina" listener="#{birdSelectorBean.stateChangeListener}" />
</p:selectOneMenu>
<p:selectOneMenu id="rodzina" value="#{birdSelectorBean.selectedStateRodzina}">
<f:selectItem itemLabel="Rodzina" itemValue="" />
<f:selectItems value="#{birdSelectorBean.rodzina}" />
</p:selectOneMenu>