UI Component Tag - Part 2
Creating a Menu Backing Bean
Now we will create a backing bean
which holds the list of MenuItems. Below shown is the MenuBackingBean.java
class
package
com.jsflessons.facelets.example;
import java.util.ArrayList;
import java.util.Collection;
public class MenuBackingBean {
private Collection<MenuItem> menus;
public Collection<MenuItem> getMenus() {
return menus;
}
public void setMenus(Collection<MenuItem> menus) {
this.menus = menus;
}
public MenuBackingBean() {
super();
menus = new ArrayList<MenuItem>();
menus.add(new MenuItem("home.xhtml", "Home"));
menus.add(new MenuItem("news.xhtml", "News"));
menus.add(new MenuItem("articles.xhtml", "Articles"));
menus.add(new MenuItem("about.xhtml", "About Us"));
}
}
import java.util.ArrayList;
import java.util.Collection;
public class MenuBackingBean {
private Collection<MenuItem> menus;
public Collection<MenuItem> getMenus() {
return menus;
}
public void setMenus(Collection<MenuItem> menus) {
this.menus = menus;
}
public MenuBackingBean() {
super();
menus = new ArrayList<MenuItem>();
menus.add(new MenuItem("home.xhtml", "Home"));
menus.add(new MenuItem("news.xhtml", "News"));
menus.add(new MenuItem("articles.xhtml", "Articles"));
menus.add(new MenuItem("about.xhtml", "About Us"));
}
}
Configure the managed bean in faces-config.xml.
In the Project Explorer, expand the
node, JSFFaceletsExample->WebContent. Double-click on faces-config.xml.
This will launch the Faces Configuration editor. Select the ManagedBean
tab. Click on the Add button. This will launch the New
Managed Bean wizard. Select the option, "Using an existing Java
class". In the next wizard panel, search for the class we created, MenuBackingBean.java.
Click Ok button. Select the scope as request and click Finish.
Now we have registered MenuBackingBean.java
class as a managed bean. Save the Faces Configuration editor. Select
source tab. Your faces-config.xml file should have the following
code.
<managed-bean>
<managed-bean-name>menuBackingBean</managed-bean-name>
<managed-bean-class>com.jsflessons.facelets.example.MenuBackingBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean-name>menuBackingBean</managed-bean-name>
<managed-bean-class>com.jsflessons.facelets.example.MenuBackingBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
Now we need to create facelets page for the 'side menu'. (Click Next to Continue..)