Managing Bean Example

This section describes you the use of the managed bean in an JSF application. In an JSF application, backing beans (managed beans) are used that can be associated with UI components. Backing Beans are nothing but JavaBeans which defines properties and me

Ads

Tutorials   
JSF Tutorials: Easy steps to learn JSF Java Server Faces (JSF) Tutorials What is JSF Mojarra? JSF Life Cycle JSF Examples JSF Tutorial For Beginners Roseindia JSF Tutorial JSF core Tag Reference JSF internationalization example Tomahawk selectOneCountry tag RCFaces About Hibernate, Spring and JSF Integration Tutorial Creating Login Page In JSF using NetBeans JSF Hello World Declaring the Bean class in the faces-config.xml file Creating a Web Application with the JSF Framework Binding the Form Elements With the Bean Properties Adding Navigation in JSF Application Binding Component Instance to a Bean Property Tomahawk validateEqual tag Tomahawk validateEmail tag Tomahawk validateCreditCard tag Tomahawk selectOneListbox tag Tomahawk selectManyListbox tag Tomahawk selectManyCheckbox tag Tomahawk selectBooleanCheckbox tag Tomahawk saveState tag Tomahawk popup tag Tomahawk inputCalendar tag Tomahawk htmlTag tag Tomahawk graphicImage tag Tomahawk documentHead tag Tomahawk documentBody tag Tomahawk document tag Tomahawk column tag DataBase Connectivity with MySql in Visual Web JSF Application Using Net Beans IDE Retrieve Image in JSF From MySQL Table Using NetBeans IDE JSF Training Java Sever Faces 1.2 - JSF 1.2 Display Data from Database in JSF Application Business Objects of Business Logic tier Setup MySQL Database Working with Database through JDBC Working with JavaScript & CSS Referencing a Localized Message Referencing Resource Bundle from a Page Binding Component Value to an Implicit Object Phase Listener Value Change Event Listener Action Event Listener
Ads

Managing Bean Example

        

This section describes you the use of the managed bean in an JSF application. In an JSF application, backing beans (managed beans) are used that can be  associated with UI components. Backing Beans are nothing but JavaBeans which defines properties and methods. These properties are bound to component values or component instances. setX() and getX() methods are defined and implemented for all properties. We can also define methods that can be used for different purpose like event handling, navigation, validation etc. that are associated with component. To bind value of the component to the property of the bean or to refer method of the bean from component tag, Expression Language (EL) syntax is used. For example, #{BeanName.propertyName}can be used to bind the value of the component to the property "propertyName" of the bean "BeanName". #{BeanName.methodName} can be used to refer method "methodName()" of the bean "BeanName".

This tutorial explains how backing bean can be used in our application "managedbean". In this application "Bean.java" is backing bean which has been used further in view pages. We have saved this JavaBean file in WEB-INF\classes\roseindia directory of this application.

Step1 : Create a JavaBean file "Bean.java".

package roseindia;

public class Bean{
  private String thought_of_the_day;
  private String userName;
  private String phoneNo;
  private String company;
  
  public void setThought_of_the_day(String thought_of_the_day){
  this.thought_of_the_day=thought_of_the_day;
  }
  public void setUserName(String userName){
  this.userName=userName;
  }

  public void setPhoneNo(String phoneNo){
  this.phoneNo=phoneNo;
  }

  public void setCompany(String company){
  this.company=company;
  }

  public String getThought_of_the_day(){
  return thought_of_the_day;
  }  
  public String getUserName(){
  return userName;
  }

  public String getPhoneNo(){
  return phoneNo;
  }

  public String getCompany(){
  return company;
  }

  public String selectPage(){
  if(company.equals("RoseIndia")){
  return "rose";
  }
  else{
  return "other";
  }
  }
}

 In the above file, there are four properties "thought_of_the_day", "useName", "phoneNo", "company" and setter and getter methods for each property to get and set values for the components respectively . There is one more method selectPage() that returns "rose" and "other" strings according to the condition.

Step2 : Configure the configuration file "faces-config.xml". Just add lines given below in this file. To configure the bean in configuration file <managed-bean> element is used. Its child element <managed-bean-name> is used to give the name of the bean to be used in the application to refer the bean class specified in another child element <managed-bean-class>. We can also specify the property value in the configuration file. For this <managed-property> child element is used where its child element <property-name> specifies the name of the property and  <value> specifies its value. 

<managed-bean>
   <managed-bean-name>Bean</managed-bean-name>
   <managed-bean-class>roseindia.Bean</managed-bean-class>
   <managed-bean-scope>session</managed-bean-scope>
   <managed-property>
   <property-name>thought_of_the_day</property-name>
   <value>It's good to be important but it's more important 
   to be good.
  
</value>
   </managed-property>
</managed-bean>

 For navigation purpose add following lines to the configuration file.

<navigation-rule>
  <from-view-id>/pages/user.jsp</from-view-id>
  <navigation-case>
  <from-outcome>rose</from-outcome>
  <to-view-id>/pages/welcome.jsp</to-view-id>
  </navigation-case>
  <navigation-case>
  <from-outcome>other</from-outcome>
  <to-view-id>/pages/otherpage.jsp</to-view-id>
  </navigation-case>
</navigation-rule>
<navigation-rule>
  <from-view-id>/pages/welcome.jsp</from-view-id>
  <navigation-case>
  <from-outcome>Edit</from-outcome>
  <to-view-id>/pages/user.jsp</to-view-id>
  </navigation-case>
</navigation-rule>
<navigation-rule>
  <from-view-id>/pages/otherpage.jsp</from-view-id>
  <navigation-case>
  <from-outcome>back</from-outcome>
  <to-view-id>/pages/user.jsp</to-view-id>
  </navigation-case>
</navigation-rule>

Step3 : Now we are ready to create view pages and use bean properties and methods in the application . In this example, we have three JSP pages "user.jsp", "welcome.jsp", "otherpage.jsp" in the "pages" directory within the application home directory.

user.jsp :

<[email protected] uri="http://java.sun.com/jsf/html" prefix="h" %>
<[email protected] uri="http://java.sun.com/jsf/core" prefix="f" %>

<html>
   <head><title>Managed Bean Example(User Page)</title></head>
   <body>
   <f:view>
  <h:form>
  &nbsp;&nbsp;<h:outputText value="User Name." /><br>
  &nbsp;&nbsp;<h:inputText value="#{Bean.userName}"/><br><br>
  &nbsp;&nbsp;<h:outputText value="Phone No." /><br>
  &nbsp;&nbsp;<h:inputText value="#{Bean.phoneNo}" /><br><br>
  &nbsp;&nbsp;<h:outputText value="Company Name" /><br> 
  &nbsp;&nbsp;<h:inputText value="#{Bean.company}" /><br><br>
  &nbsp;&nbsp;<h:outputText value="Thought of the day" /><br>
  &nbsp;&nbsp;<h:inputText value="#{Bean.thought_of_the_day}" />
   <h:commandButton value="Submit" action="#{Bean.selectPage}" />
  </h:form>
  </f:view>
 </body>
</html>

In this page we have associated values of the UIcomponents to the  backing bean's properties. This binding has been shown by bold letters. userName, phoneNo, company, thought_of_the_day properties are bound to the input components. When the user first calls this page, the last input component will be shown with the value specified in the "faces-config.xml"  file because we have already specified this value in the configuration file. When the user fills values in these components these values are stored in the properties of the bean. So when they are again bound with the component, stored values are used. For example, in "welcome.jsp" page we have again used these properties values so it considers the values filled in previous page. Command button component's action is associated with selectPage() method of the Bean class. This method  returns a String value either "rose" or "other". This value is set to the action attribute of the commandButton tag. This button works according to the returned value and the navigation rules specified in the "faces-config.xml" file.

welcome.jsp :

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<html>
  <head><title>Managed Bean Example (Welcome page)</title></head>
  <body>
   <f:view>
  <h:form>
   <h:outputText value="You have entered :" /><hr>User Name :
   <h:outputText value="#{Bean.userName}" /><br> Password :
   <h:outputText value="#{Bean.phoneNo}" /><br> Phone No :
   <h:outputText value="#{Bean.company}" /><br>
   <h:commandButton value="Edit Details" action="Edit"/><hr>
   <h:outputText value="#{Bean.thought_of_the_day}" /></b>
  </h:form>
   </f:view>
  </body>
</html>

otherpage.jsp :

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<html>
  <head><title>Managed Bean Example (otherpage page)</title></head>
  <body>
   <f:view>
   <h:form>
 <h:outputText value="Sorry........This is for RoseIndia 
   employees only. " /><br> 
   <h:commandButton value="Go Back" action="back"/>
   </h:form>
  </f:view>
 </body>
</html>

 Output : This is the first page which has four input boxes and one submit button. Last input box already contains some text which comes from the value specified in the configuration file.

Now suppose user fills values as shown below :

The page below comes if the user fills Company Name as "RoseIndia".

The page below comes if user doesn't fill Company Name as "RoseIndia".

Advertisements

Ads
Share on Google+Share on Google+

Managing Bean Example

Posted on: February 26, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Managing Bean Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
skabdus
October 17, 2011
jsf

it's very good demonstration...... thank you
med amine
April 22, 2013
thanks

thanks for ur tuto i have 3 days looking a solutions for my probleme thanjssss u helped me

Ads

 

Ads