Make multiple dependent / cascading selection components in JSF - jsf

I am trying to make 4 dependent / cascading selection components. In this question, the selection component happens to be a <h:selectOneMenu>, but this is of course applicable on any other kind of selection component extending from UISelectOne/UISelectMany superclass, such as <h:selectManyCheckbox> or PrimeFaces <p:selectCheckboxMenu>, <p:selectManyMenu>, etc.
When the user chooses an item from the first menu, the second menu will show dependent data and when the user chooses item from the second one , the third one will show dependent data and so on.
The user will see items on the first menu only and the other ones will be blank. If he chooses an item on the first menu the second one will show data but the third and the fourth will remain blank, and so on. The user must eventually choose entries from all the 4 menus.
<h:selectOneMenu id="first" value="#{nodes.selectState}">
<f:selectItems value="#{nodes.stateList}"/>
<f:ajax render="second">
</h:selectOneMenu>
<h:selectOneMenu id="second" value="#{nodes.selectCity}">
<f:selectItems value="#{nodes.cityList}"/>
<f:ajax render="third">
</h:selectOneMenu>
<h:selectOneMenu id="third" value="#{nodes.selectRegion}">
<f:selectItems value="#{nodes.regionList}"/>
<f:ajax render="fourth">
</h:selectOneMenu>
<h:selectOneMenu id="fourth" value="#{nodes.selectStation}">
<f:selectItems value="#{nodes.stationList}"/>
</h:selectOneMenu>
Nodes bean
private String selectState; //+setters, getters
private String selectCity; //+setters, getters
private String selectRegion; //+setters, getters
private String selectStation; //+setters, getters
private List<SelectItem> stateList; //+setters, getters
private List<SelectItem> cityList; //+setters, getters
private List<SelectItem> regionList; //+setters, getters
private List<SelectItem> stationList; //+setters, getters
public getStateList(){
stateList= new ArrayList<SelectItem>();
stateList.add(new SelectItem("A"));
}
public getCityList(){
CityList= new ArrayList<SelectItem>();
if(selectState.equals("A")){
CityList.add(new SelectItem("B"));
}
}
public getRegionList(){
RegionList= new ArrayList<SelectItem>();
if(selectCity.equals("B")){
RegionList.add(new SelectItem("C"));
}
}
public getStationList(){
StationList= new ArrayList<SelectItem>();
if(selectRegion.equals("C")){
StationList.add(new SelectItem("D"));
}
}
It's only working for the first 2 menus. The other 2 menus get null values.

Put the bean in the view scope and get rid of any business logic in getter methods.
The bean must be placed in the view scope so that all previous selections and new available items are remembered for subsequent postbacks, otherwise things will fail when JSF needs to validate the selected item against the list of available items which was prepopulated in a previous selection, or when e.g. rendered attribute depends on a condition which was only set in a previous request.
The getter methods may not contain any business logic as they will also be invoked during among others the validations phase. You should use <f:ajax listener>/<p:ajax listener> to perform business logic based on a change. You should in the listener method also explicitly clear out selected values of child selection components. You can use <f:ajax render>/<p:ajax update> to update the contents of child selection components.
Thus, so:
<h:selectOneMenu id="state" value="#{nodes.selectedState}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableStates}" />
<f:ajax listener="#{nodes.changeState}" render="city region station" />
</h:selectOneMenu>
<h:selectOneMenu id="city" value="#{nodes.selectedCity}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableCities}" />
<f:ajax listener="#{nodes.changeCity}" render="region station" />
</h:selectOneMenu>
<h:selectOneMenu id="region" value="#{nodes.selectedRegion}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableRegions}" />
<f:ajax listener="#{nodes.changeRegion}" render="station" />
</h:selectOneMenu>
<h:selectOneMenu id="station" value="#{nodes.selectedStation}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableStations}" />
</h:selectOneMenu>
with
#Named
#ViewScoped
public class Nodes {
private String selectedState; // getter+setter
private String selectedCity; // getter+setter
private String selectedRegion; // getter+setter
private String selectedStation; // getter+setter
private List<SelectItem> availableStates; // getter (no setter necessary!)
private List<SelectItem> availableCities; // getter (no setter necessary!)
private List<SelectItem> availableRegions; // getter (no setter necessary!)
private List<SelectItem> availableStations; // getter (no setter necessary!)
#EJB
private SomeService someService;
#PostConstruct
public void init() {
availableStates = someService.listStates();
}
public void changeState(AjaxBehaviorEvent event) {
availableCities = someService.listCities(selectedState);
selectedCity = selectedRegion = selectedStation = null;
availableRegions = availableStations = null;
}
public void changeCity(AjaxBehaviorEvent event) {
availableRegions = someService.listRegions(selectedCity);
selectedRegion = selectedStation = null;
availableStations = null;
}
public void changeRegion(AjaxBehaviorEvent event) {
availableStations = someService.listStations(selectedRegion);
selectedStation = null;
}
// Generate necessary getters+setters here. You should not change them.
}
See also:
How to choose the right bean scope?
Best way to add a "nothing selected" option to a selectOneMenu in JSF
How to populate options of h:selectOneMenu from database?
When to use valueChangeListener or f:ajax listener?
Why JSF calls getters multiple times

Try this, it may help you
By using the --Select City-- , --Select Region--, --Select Station-- to avoid the null pointer Exception.
public getStateList(){
stateList= new ArrayList<SelectItem>();
stateList.add(new SelectItem("A"));
}
public getCityList(){
CityList= new ArrayList<SelectItem>();
if(selectState.equals("A"))
{
CityList.add(new SelectItem("B"));
}
else
{
CityList.add(new SelectItem("--Select City--"));
selectCity = "--Select City--";
}
public getRegionList(){
RegionList= new ArrayList<SelectItem>();
if(selectCity.equals("B"))
{
RegionList.add(new SelectItem("C"));
}
else
{
RegionList.add(new SelectItem("--Select Region--"));
selectRegion = "--Select Region--";
}
}
public getStationList(){
StationList= new ArrayList<SelectItem>();
if(selectRegion.equals("C"))
{
StationList.add(new SelectItem("D"));
}
else
{
StationList.add(new SelectItem("Select Station"));
selectStation = "--Select Station--";
}
}

There is a typo error in your code. For third menu you have given id name as "first" instead of "third". May be its because of that problem.

You are facing this issue because you have twice id="first".
Fix this and it should work.

Related

selectCheckboxMenu always fails validation (empty) [duplicate]

I am trying to make 4 dependent / cascading selection components. In this question, the selection component happens to be a <h:selectOneMenu>, but this is of course applicable on any other kind of selection component extending from UISelectOne/UISelectMany superclass, such as <h:selectManyCheckbox> or PrimeFaces <p:selectCheckboxMenu>, <p:selectManyMenu>, etc.
When the user chooses an item from the first menu, the second menu will show dependent data and when the user chooses item from the second one , the third one will show dependent data and so on.
The user will see items on the first menu only and the other ones will be blank. If he chooses an item on the first menu the second one will show data but the third and the fourth will remain blank, and so on. The user must eventually choose entries from all the 4 menus.
<h:selectOneMenu id="first" value="#{nodes.selectState}">
<f:selectItems value="#{nodes.stateList}"/>
<f:ajax render="second">
</h:selectOneMenu>
<h:selectOneMenu id="second" value="#{nodes.selectCity}">
<f:selectItems value="#{nodes.cityList}"/>
<f:ajax render="third">
</h:selectOneMenu>
<h:selectOneMenu id="third" value="#{nodes.selectRegion}">
<f:selectItems value="#{nodes.regionList}"/>
<f:ajax render="fourth">
</h:selectOneMenu>
<h:selectOneMenu id="fourth" value="#{nodes.selectStation}">
<f:selectItems value="#{nodes.stationList}"/>
</h:selectOneMenu>
Nodes bean
private String selectState; //+setters, getters
private String selectCity; //+setters, getters
private String selectRegion; //+setters, getters
private String selectStation; //+setters, getters
private List<SelectItem> stateList; //+setters, getters
private List<SelectItem> cityList; //+setters, getters
private List<SelectItem> regionList; //+setters, getters
private List<SelectItem> stationList; //+setters, getters
public getStateList(){
stateList= new ArrayList<SelectItem>();
stateList.add(new SelectItem("A"));
}
public getCityList(){
CityList= new ArrayList<SelectItem>();
if(selectState.equals("A")){
CityList.add(new SelectItem("B"));
}
}
public getRegionList(){
RegionList= new ArrayList<SelectItem>();
if(selectCity.equals("B")){
RegionList.add(new SelectItem("C"));
}
}
public getStationList(){
StationList= new ArrayList<SelectItem>();
if(selectRegion.equals("C")){
StationList.add(new SelectItem("D"));
}
}
It's only working for the first 2 menus. The other 2 menus get null values.
Put the bean in the view scope and get rid of any business logic in getter methods.
The bean must be placed in the view scope so that all previous selections and new available items are remembered for subsequent postbacks, otherwise things will fail when JSF needs to validate the selected item against the list of available items which was prepopulated in a previous selection, or when e.g. rendered attribute depends on a condition which was only set in a previous request.
The getter methods may not contain any business logic as they will also be invoked during among others the validations phase. You should use <f:ajax listener>/<p:ajax listener> to perform business logic based on a change. You should in the listener method also explicitly clear out selected values of child selection components. You can use <f:ajax render>/<p:ajax update> to update the contents of child selection components.
Thus, so:
<h:selectOneMenu id="state" value="#{nodes.selectedState}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableStates}" />
<f:ajax listener="#{nodes.changeState}" render="city region station" />
</h:selectOneMenu>
<h:selectOneMenu id="city" value="#{nodes.selectedCity}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableCities}" />
<f:ajax listener="#{nodes.changeCity}" render="region station" />
</h:selectOneMenu>
<h:selectOneMenu id="region" value="#{nodes.selectedRegion}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableRegions}" />
<f:ajax listener="#{nodes.changeRegion}" render="station" />
</h:selectOneMenu>
<h:selectOneMenu id="station" value="#{nodes.selectedStation}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableStations}" />
</h:selectOneMenu>
with
#Named
#ViewScoped
public class Nodes {
private String selectedState; // getter+setter
private String selectedCity; // getter+setter
private String selectedRegion; // getter+setter
private String selectedStation; // getter+setter
private List<SelectItem> availableStates; // getter (no setter necessary!)
private List<SelectItem> availableCities; // getter (no setter necessary!)
private List<SelectItem> availableRegions; // getter (no setter necessary!)
private List<SelectItem> availableStations; // getter (no setter necessary!)
#EJB
private SomeService someService;
#PostConstruct
public void init() {
availableStates = someService.listStates();
}
public void changeState(AjaxBehaviorEvent event) {
availableCities = someService.listCities(selectedState);
selectedCity = selectedRegion = selectedStation = null;
availableRegions = availableStations = null;
}
public void changeCity(AjaxBehaviorEvent event) {
availableRegions = someService.listRegions(selectedCity);
selectedRegion = selectedStation = null;
availableStations = null;
}
public void changeRegion(AjaxBehaviorEvent event) {
availableStations = someService.listStations(selectedRegion);
selectedStation = null;
}
// Generate necessary getters+setters here. You should not change them.
}
See also:
How to choose the right bean scope?
Best way to add a "nothing selected" option to a selectOneMenu in JSF
How to populate options of h:selectOneMenu from database?
When to use valueChangeListener or f:ajax listener?
Why JSF calls getters multiple times
Try this, it may help you
By using the --Select City-- , --Select Region--, --Select Station-- to avoid the null pointer Exception.
public getStateList(){
stateList= new ArrayList<SelectItem>();
stateList.add(new SelectItem("A"));
}
public getCityList(){
CityList= new ArrayList<SelectItem>();
if(selectState.equals("A"))
{
CityList.add(new SelectItem("B"));
}
else
{
CityList.add(new SelectItem("--Select City--"));
selectCity = "--Select City--";
}
public getRegionList(){
RegionList= new ArrayList<SelectItem>();
if(selectCity.equals("B"))
{
RegionList.add(new SelectItem("C"));
}
else
{
RegionList.add(new SelectItem("--Select Region--"));
selectRegion = "--Select Region--";
}
}
public getStationList(){
StationList= new ArrayList<SelectItem>();
if(selectRegion.equals("C"))
{
StationList.add(new SelectItem("D"));
}
else
{
StationList.add(new SelectItem("Select Station"));
selectStation = "--Select Station--";
}
}
There is a typo error in your code. For third menu you have given id name as "first" instead of "third". May be its because of that problem.
You are facing this issue because you have twice id="first".
Fix this and it should work.

SelectManyCheckbox value not updating on xhtml(frontend) though updates in bean [duplicate]

I am trying to make 4 dependent / cascading selection components. In this question, the selection component happens to be a <h:selectOneMenu>, but this is of course applicable on any other kind of selection component extending from UISelectOne/UISelectMany superclass, such as <h:selectManyCheckbox> or PrimeFaces <p:selectCheckboxMenu>, <p:selectManyMenu>, etc.
When the user chooses an item from the first menu, the second menu will show dependent data and when the user chooses item from the second one , the third one will show dependent data and so on.
The user will see items on the first menu only and the other ones will be blank. If he chooses an item on the first menu the second one will show data but the third and the fourth will remain blank, and so on. The user must eventually choose entries from all the 4 menus.
<h:selectOneMenu id="first" value="#{nodes.selectState}">
<f:selectItems value="#{nodes.stateList}"/>
<f:ajax render="second">
</h:selectOneMenu>
<h:selectOneMenu id="second" value="#{nodes.selectCity}">
<f:selectItems value="#{nodes.cityList}"/>
<f:ajax render="third">
</h:selectOneMenu>
<h:selectOneMenu id="third" value="#{nodes.selectRegion}">
<f:selectItems value="#{nodes.regionList}"/>
<f:ajax render="fourth">
</h:selectOneMenu>
<h:selectOneMenu id="fourth" value="#{nodes.selectStation}">
<f:selectItems value="#{nodes.stationList}"/>
</h:selectOneMenu>
Nodes bean
private String selectState; //+setters, getters
private String selectCity; //+setters, getters
private String selectRegion; //+setters, getters
private String selectStation; //+setters, getters
private List<SelectItem> stateList; //+setters, getters
private List<SelectItem> cityList; //+setters, getters
private List<SelectItem> regionList; //+setters, getters
private List<SelectItem> stationList; //+setters, getters
public getStateList(){
stateList= new ArrayList<SelectItem>();
stateList.add(new SelectItem("A"));
}
public getCityList(){
CityList= new ArrayList<SelectItem>();
if(selectState.equals("A")){
CityList.add(new SelectItem("B"));
}
}
public getRegionList(){
RegionList= new ArrayList<SelectItem>();
if(selectCity.equals("B")){
RegionList.add(new SelectItem("C"));
}
}
public getStationList(){
StationList= new ArrayList<SelectItem>();
if(selectRegion.equals("C")){
StationList.add(new SelectItem("D"));
}
}
It's only working for the first 2 menus. The other 2 menus get null values.
Put the bean in the view scope and get rid of any business logic in getter methods.
The bean must be placed in the view scope so that all previous selections and new available items are remembered for subsequent postbacks, otherwise things will fail when JSF needs to validate the selected item against the list of available items which was prepopulated in a previous selection, or when e.g. rendered attribute depends on a condition which was only set in a previous request.
The getter methods may not contain any business logic as they will also be invoked during among others the validations phase. You should use <f:ajax listener>/<p:ajax listener> to perform business logic based on a change. You should in the listener method also explicitly clear out selected values of child selection components. You can use <f:ajax render>/<p:ajax update> to update the contents of child selection components.
Thus, so:
<h:selectOneMenu id="state" value="#{nodes.selectedState}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableStates}" />
<f:ajax listener="#{nodes.changeState}" render="city region station" />
</h:selectOneMenu>
<h:selectOneMenu id="city" value="#{nodes.selectedCity}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableCities}" />
<f:ajax listener="#{nodes.changeCity}" render="region station" />
</h:selectOneMenu>
<h:selectOneMenu id="region" value="#{nodes.selectedRegion}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableRegions}" />
<f:ajax listener="#{nodes.changeRegion}" render="station" />
</h:selectOneMenu>
<h:selectOneMenu id="station" value="#{nodes.selectedStation}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableStations}" />
</h:selectOneMenu>
with
#Named
#ViewScoped
public class Nodes {
private String selectedState; // getter+setter
private String selectedCity; // getter+setter
private String selectedRegion; // getter+setter
private String selectedStation; // getter+setter
private List<SelectItem> availableStates; // getter (no setter necessary!)
private List<SelectItem> availableCities; // getter (no setter necessary!)
private List<SelectItem> availableRegions; // getter (no setter necessary!)
private List<SelectItem> availableStations; // getter (no setter necessary!)
#EJB
private SomeService someService;
#PostConstruct
public void init() {
availableStates = someService.listStates();
}
public void changeState(AjaxBehaviorEvent event) {
availableCities = someService.listCities(selectedState);
selectedCity = selectedRegion = selectedStation = null;
availableRegions = availableStations = null;
}
public void changeCity(AjaxBehaviorEvent event) {
availableRegions = someService.listRegions(selectedCity);
selectedRegion = selectedStation = null;
availableStations = null;
}
public void changeRegion(AjaxBehaviorEvent event) {
availableStations = someService.listStations(selectedRegion);
selectedStation = null;
}
// Generate necessary getters+setters here. You should not change them.
}
See also:
How to choose the right bean scope?
Best way to add a "nothing selected" option to a selectOneMenu in JSF
How to populate options of h:selectOneMenu from database?
When to use valueChangeListener or f:ajax listener?
Why JSF calls getters multiple times
Try this, it may help you
By using the --Select City-- , --Select Region--, --Select Station-- to avoid the null pointer Exception.
public getStateList(){
stateList= new ArrayList<SelectItem>();
stateList.add(new SelectItem("A"));
}
public getCityList(){
CityList= new ArrayList<SelectItem>();
if(selectState.equals("A"))
{
CityList.add(new SelectItem("B"));
}
else
{
CityList.add(new SelectItem("--Select City--"));
selectCity = "--Select City--";
}
public getRegionList(){
RegionList= new ArrayList<SelectItem>();
if(selectCity.equals("B"))
{
RegionList.add(new SelectItem("C"));
}
else
{
RegionList.add(new SelectItem("--Select Region--"));
selectRegion = "--Select Region--";
}
}
public getStationList(){
StationList= new ArrayList<SelectItem>();
if(selectRegion.equals("C"))
{
StationList.add(new SelectItem("D"));
}
else
{
StationList.add(new SelectItem("Select Station"));
selectStation = "--Select Station--";
}
}
There is a typo error in your code. For third menu you have given id name as "first" instead of "third". May be its because of that problem.
You are facing this issue because you have twice id="first".
Fix this and it should work.

Dependent dropdown box in jsf primefaces is not setting(changing) to default value select-one when selecting All option [duplicate]

I am trying to make 4 dependent / cascading selection components. In this question, the selection component happens to be a <h:selectOneMenu>, but this is of course applicable on any other kind of selection component extending from UISelectOne/UISelectMany superclass, such as <h:selectManyCheckbox> or PrimeFaces <p:selectCheckboxMenu>, <p:selectManyMenu>, etc.
When the user chooses an item from the first menu, the second menu will show dependent data and when the user chooses item from the second one , the third one will show dependent data and so on.
The user will see items on the first menu only and the other ones will be blank. If he chooses an item on the first menu the second one will show data but the third and the fourth will remain blank, and so on. The user must eventually choose entries from all the 4 menus.
<h:selectOneMenu id="first" value="#{nodes.selectState}">
<f:selectItems value="#{nodes.stateList}"/>
<f:ajax render="second">
</h:selectOneMenu>
<h:selectOneMenu id="second" value="#{nodes.selectCity}">
<f:selectItems value="#{nodes.cityList}"/>
<f:ajax render="third">
</h:selectOneMenu>
<h:selectOneMenu id="third" value="#{nodes.selectRegion}">
<f:selectItems value="#{nodes.regionList}"/>
<f:ajax render="fourth">
</h:selectOneMenu>
<h:selectOneMenu id="fourth" value="#{nodes.selectStation}">
<f:selectItems value="#{nodes.stationList}"/>
</h:selectOneMenu>
Nodes bean
private String selectState; //+setters, getters
private String selectCity; //+setters, getters
private String selectRegion; //+setters, getters
private String selectStation; //+setters, getters
private List<SelectItem> stateList; //+setters, getters
private List<SelectItem> cityList; //+setters, getters
private List<SelectItem> regionList; //+setters, getters
private List<SelectItem> stationList; //+setters, getters
public getStateList(){
stateList= new ArrayList<SelectItem>();
stateList.add(new SelectItem("A"));
}
public getCityList(){
CityList= new ArrayList<SelectItem>();
if(selectState.equals("A")){
CityList.add(new SelectItem("B"));
}
}
public getRegionList(){
RegionList= new ArrayList<SelectItem>();
if(selectCity.equals("B")){
RegionList.add(new SelectItem("C"));
}
}
public getStationList(){
StationList= new ArrayList<SelectItem>();
if(selectRegion.equals("C")){
StationList.add(new SelectItem("D"));
}
}
It's only working for the first 2 menus. The other 2 menus get null values.
Put the bean in the view scope and get rid of any business logic in getter methods.
The bean must be placed in the view scope so that all previous selections and new available items are remembered for subsequent postbacks, otherwise things will fail when JSF needs to validate the selected item against the list of available items which was prepopulated in a previous selection, or when e.g. rendered attribute depends on a condition which was only set in a previous request.
The getter methods may not contain any business logic as they will also be invoked during among others the validations phase. You should use <f:ajax listener>/<p:ajax listener> to perform business logic based on a change. You should in the listener method also explicitly clear out selected values of child selection components. You can use <f:ajax render>/<p:ajax update> to update the contents of child selection components.
Thus, so:
<h:selectOneMenu id="state" value="#{nodes.selectedState}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableStates}" />
<f:ajax listener="#{nodes.changeState}" render="city region station" />
</h:selectOneMenu>
<h:selectOneMenu id="city" value="#{nodes.selectedCity}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableCities}" />
<f:ajax listener="#{nodes.changeCity}" render="region station" />
</h:selectOneMenu>
<h:selectOneMenu id="region" value="#{nodes.selectedRegion}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableRegions}" />
<f:ajax listener="#{nodes.changeRegion}" render="station" />
</h:selectOneMenu>
<h:selectOneMenu id="station" value="#{nodes.selectedStation}">
<f:selectItem itemValue="#{null}" itemLabel="-- select --" />
<f:selectItems value="#{nodes.availableStations}" />
</h:selectOneMenu>
with
#Named
#ViewScoped
public class Nodes {
private String selectedState; // getter+setter
private String selectedCity; // getter+setter
private String selectedRegion; // getter+setter
private String selectedStation; // getter+setter
private List<SelectItem> availableStates; // getter (no setter necessary!)
private List<SelectItem> availableCities; // getter (no setter necessary!)
private List<SelectItem> availableRegions; // getter (no setter necessary!)
private List<SelectItem> availableStations; // getter (no setter necessary!)
#EJB
private SomeService someService;
#PostConstruct
public void init() {
availableStates = someService.listStates();
}
public void changeState(AjaxBehaviorEvent event) {
availableCities = someService.listCities(selectedState);
selectedCity = selectedRegion = selectedStation = null;
availableRegions = availableStations = null;
}
public void changeCity(AjaxBehaviorEvent event) {
availableRegions = someService.listRegions(selectedCity);
selectedRegion = selectedStation = null;
availableStations = null;
}
public void changeRegion(AjaxBehaviorEvent event) {
availableStations = someService.listStations(selectedRegion);
selectedStation = null;
}
// Generate necessary getters+setters here. You should not change them.
}
See also:
How to choose the right bean scope?
Best way to add a "nothing selected" option to a selectOneMenu in JSF
How to populate options of h:selectOneMenu from database?
When to use valueChangeListener or f:ajax listener?
Why JSF calls getters multiple times
Try this, it may help you
By using the --Select City-- , --Select Region--, --Select Station-- to avoid the null pointer Exception.
public getStateList(){
stateList= new ArrayList<SelectItem>();
stateList.add(new SelectItem("A"));
}
public getCityList(){
CityList= new ArrayList<SelectItem>();
if(selectState.equals("A"))
{
CityList.add(new SelectItem("B"));
}
else
{
CityList.add(new SelectItem("--Select City--"));
selectCity = "--Select City--";
}
public getRegionList(){
RegionList= new ArrayList<SelectItem>();
if(selectCity.equals("B"))
{
RegionList.add(new SelectItem("C"));
}
else
{
RegionList.add(new SelectItem("--Select Region--"));
selectRegion = "--Select Region--";
}
}
public getStationList(){
StationList= new ArrayList<SelectItem>();
if(selectRegion.equals("C"))
{
StationList.add(new SelectItem("D"));
}
else
{
StationList.add(new SelectItem("Select Station"));
selectStation = "--Select Station--";
}
}
There is a typo error in your code. For third menu you have given id name as "first" instead of "third". May be its because of that problem.
You are facing this issue because you have twice id="first".
Fix this and it should work.

how to merge a shop entity to a client entity (many to one) in jsf

I'm trying to merge an existing shop to a new client in jsf but it isn't successful. The program consists basically off a backing bean controller,an ejb and the jsf page (register) I have been able to populate the shoplist in a comboBox UI. here is the code.
register.xhtml:
clientcontroller.client.fname is a SFSB.
Property already persisted but trying to be merged.
A shop from a list (shopcontroller.shopList)
<h:form>
<h:panelGrid columns="3" >
<h:outputText value="Select From.
Available Shops :" />
<h:selectOneMenu value="#.
{shopController.shop}" >
<f:selectItems var="s" value="#.
{shopController.shopList}" />
</h:selectOneMenu>
<h:commandButton value="register".
action="#{clientcontroller.Register(s)}" />
</h:panelGrid>
</h:form>
backing bean class:
ManagedBean(name="clientcontroller")
#RequestScoped
public class clientController {
#EJB ClientEJB clientEJB;
private Client clt = new Client();
private Shop shp = new Shop();
private String clientfname;
//getters and setters
public String Register(Shop shp){
this.shp = shp;
clientEJB.register(clt, shp);
return "";
}
EJB class:
#Stateful
#LocalBean
public class ClientEJB {
#PersistenceContext
EntityManager em;
public void addClient(Client clt){
em.persist(clt);
}
public void register(Client c ,Shop s){
c.getShopList().add(s);
s.setAvailability("false");
s.setClientid(c);
em.merge(s);
em.merge(c);
}
}
Adjust your code as follows:
XHTML:
<h:commandButton value="register" action="#{clientcontroller.Register}" />
ManagedBean
public String Register(){
clientEJB.register(clt, shp);
return "";
}
See Also:
JSF 2 dropdown box example

How to implement "Select all" on selectManyCheckbox

I want to set checkboxes of a selectManyCheckbox to checked if a condition is true "#{myBean.isCondition}":
MyBean.java
public class MyBean{
private boolean isCondition;
...
public boolean isCondition{
return isCondition;
}
...
}
my_page.jspx :
<h:selectManyCheckbox
layout="pageDirection"
label="#{messages['numsTelephone.label']}"
value="#{listSelected}">
<s:selectItems var="clientTelephone"
value="#{list}"
label="#{clientTelephone}" />
</h:selectManyCheckbox>
I need to know if it exist something like
checked = "#{myBean.isCondition}"
that I can add in my selectManyCheckbox bloc ? Or should I use a javascript ?
Any other solution is welcome too.
My other question is how to set myBean.isCondition to true if a checkbox is selected ?
Thanks,
Use <f:ajax> in the "Select all" checkbox and do the selection job in the listener method and then re-render the checkboxes.
Kickoff example:
<h:form>
<h:selectBooleanCheckbox value="#{bean.selectAll}">
<f:ajax listener="#{bean.onSelectAll}" render="items" />
</h:selectBooleanCheckbox>
<h:selectManyCheckbox id="items" value="#{bean.selectedItems}">
<f:selectItems value="#{bean.availableItems}" />
</h:selectManyCheckbox>
</h:form>
#Named
#ViewScoped
public class Bean implements Serializable {
private boolean selectAll; // +getter +setter
private List<String> selectedItems; // +getter +setter
private Map<String, String> availableItems; // +getter (no setter necessary)
#PostConstruct
public void init() {
availableItems = new LinkedHashMap<String, String>();
availableItems.put("Foo label", "foo");
availableItems.put("Bar label", "bar");
availableItems.put("Baz label", "baz");
}
public void onSelectAll() {
selectedItems = selectAll ? new ArrayList<>(availableItems.values()) : null;
}
// ...
}

Resources