Saturday, 7 September 2013

JSF cannot populate through f:selectItems

JSF cannot populate through f:selectItems

I am trying to learn JSF 2, I have followed this tutorial which loads
f:selecItems from java class method that return ArrayList. I tried to
populate it using
f:selectItem itemValue="Row 1"
and it works. I look into the tutorial but I cannot see the difference so
I do not know what I did wrong.
model:
@ManagedBean
@SessionScoped
public class LocationsAndActivities implements Serializable
{
private final Location aLocations = new Location();
private Activies aActivities = null;
private String selectedLocation = "--- SELECT LOCATION ---";
private String selectedActivity = "--- CHOOSE ACTIVITY ---";
private boolean hasActivities = Boolean.FALSE;
public ArrayList<SelectItem> getLocations()
{
return doGetLocations();
}
private ArrayList<SelectItem> doGetLocations()
{
final ArrayList<SelectItem> modifiedLoc = aLocations.getLocations();
modifiedLoc.add(0, new SelectItem("--- SELECT LOCATION ---"));
return modifiedLoc;
}
public void setLocation(final String location)
{
doSetLocation(location);
}
private void doSetLocation(final String location)
{
this.selectedLocation = location;
aActivities = new Activies(selectedLocation);
hasActivities = aActivities.hasActivityLists();
}
public String getLocation()
{
return doGetLocation();
}
private String doGetLocation()
{
return this.selectedLocation;
}
public ArrayList<SelectItem> getActivities()
{
return doGetActivities();
}
private ArrayList<SelectItem> doGetActivities()
{
final ArrayList<SelectItem> returnValue;
if(aActivities == null)
{
hasActivities = Boolean.FALSE;
returnValue = new ArrayList<>();
}
else
{
returnValue = aActivities.getActivities();
}
returnValue.add(0,new SelectItem("--- CHOOSE ACTIVITY ---"));
return returnValue;
}
public String getActivity()
{
return doGetActivity();
}
private String doGetActivity()
{
return this.selectedActivity;
}
public void setActivity(final String activity)
{
doSetActivity(activity);
}
private void doSetActivity(final String activity)
{
this.selectedActivity = activity;
}
public boolean isValidLocation()
{
return checkLocationValidity();
}
private boolean checkLocationValidity()
{
return hasActivities;
}
public String getMessage()
{
return doGetMessage();
}
private String doGetMessage()
{
return (hasActivities
? "Please select location first"
: "You select " + selectedLocation
+" and activity of " + selectedActivity);
}
}
view:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f= "http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Title</title>
<link href="./css/styles.css"
rel="stylesheet" type="text/css"/>
</h:head>
<h:body>
<h:form>
Select Location of Adventure:
<h:selectOneMenu value="#{locationAndActivities.location}">
<f:selectItems value="#{locationAndActivities.locations()}"/>
<f:ajax render="activityId" />
</h:selectOneMenu><br />
Select Location of Activities:
<h:selectOneMenu id="activityId"
value="#{locationAndActivities.activity}"
disabled="#{locationAndActivities.validLocation}">
<f:selectItems value="#{locationAndActivities.activities()}"/>
<f:ajax render="messageId"/>
</h:selectOneMenu><br />
<h:outputText id="messageId" value="#{locationAndActivities.message}"/>
</h:form>
</h:body>
</html>
In one of the questions here about populating JSF combo box, I saw that
one answers that I have to put @PostConstruct, I have tried it but still
it does not load.
I hope someone could point what I did wrong..
Thanks

No comments:

Post a Comment