jsf - Loading h:selectOneMenu depending of another h:selectOneMenu value -


i have 2 h:selectonemenu (1: countries, 2: cities). need load cities 1 selected country in cities selectonemenu using ajax. when change value of countries selectonemenu cities selectonemenu takes null value 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 method find 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 getting nullpointerexception because countrybean.selectedcountry null. right way this?

one of many rules jsf starter need know:

  • don't business logic in getter method.

once try fix keeping getter methods true getter methods (i.e. not else return property;) , performing business logic in (post)constructor and/or action(listener) methods, particular problem shall disappear.

here's 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 in @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); } 

Comments

Popular posts from this blog

design - Custom Styling Qt Quick Controls -

Unable to remove the www from url on https using .htaccess -