Simple JSF Application (Part III)
25 December 2007In this last part of our introductory JSF blog, we will look at bean and configuration file in our application. It is assumed that you have gone through the previous parts of “Simple JSF Application” series.
First have a look at bean, WelcomeBean.java
import javax.faces.application.Application; import javax.faces.component.html.HtmlOutputText; import javax.faces.component.html.HtmlPanelGrid; import javax.faces.context.FacesContext; import javax.faces.event.ActionEvent; import java.util.List; public class WelcomeBean { private int numControls; private HtmlPanelGrid htmlPanel; public int getNumControls() { return numControls; } public void setNumControls(int numControls) { this.numControls = numControls; } public HtmlPanelGrid getHtmlPanel() { return htmlPanel; } public void setHtmlPanel(HtmlPanelGrid htmlPanel) { this.htmlPanel = htmlPanel; } public void addControls(ActionEvent actionEvent) { Application application = FacesContext.getCurrentInstance().getApplication(); List childrenList = htmlPanel.getChildren(); childrenList.clear(); for (int count = 1; count < numControls; count++) { if(numControls%count == 0){//if count is factor of number entered? HtmlOutputText output = (HtmlOutputText)application.createComponent (HtmlOutputText.COMPONENT_TYPE); output.setValue(" " + count + " "); output.setStyle("color: blue"); childrenList.add(output); } } } }
Both of our jsp’s have used components from this bean. First property defined in this bean is numControls. In our welcome.jsp, it is used to take input and is referenced by HtmlInputText, while in factor.jsp, it is referenced by HtmlOutputText component. htmlPanel property is object of HtmlPanelGrid, which get created by
Then we have the configuration file namely faces-config.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"> <faces-config> <managed-bean> <description>My first bean for JSF</description> <managed-bean-name>welcomeBean</managed-bean-name> <managed-bean-class>WelcomeBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> <navigation-rule> <description>Navigation from the Welcome page</description> <from-view-id>/welcome.jsp</from-view-id> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>/factor.jsp</to-view-id> </navigation-case> </navigation-rule> </faces-config>
Like struts, JSF has a configuration file. This allows user to define beans, navigation rules and several other components of JSF. In our file, we have declared object welcomeBean, which is used by our jsp files. Class for this object is WelcomeBean. Navigation rule specifies the possible routes from pages. Since, we had a single case of moving from welcome.jsp to factor.jsp. So, that is given in success case.
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Simple JSF Application (Part III)