Show TOC

Using a Bean with the TaglibLocate this document in the navigation structure

Use

The bean has to be declared at the beginning of the JSP.

<jsp:useBean id="myBean" scope="application" class="bean.myBean" />

See Beans: Usage & Scopes for details about the attributes.

To specify the model for the control with the taglib without a scriptlet, a property section in the bean must be defined. In addition the bean needs set and get methods to have access to the bean. The get and set method for the bean has to follow the naming convention:

get+MethodName

The MethodName must start with a capital (upper case) letter.

The following example shows the definition of the bean and than the specification in the JSP. The get and set methods in the bean are called setModel and getModel . The JSP accesses the bean with the bean id and the method model.

Example

Definition of the bean:

                 package bean;

  import com.sap.htmlb.BreadCrumb;
  import com.sap.htmlb.IListModel;
  import com.sap.htmlb.DefaultListModel;

  public class BreadCrumbBean {

/* Property declaration */
     private IListModel model;

/* Constructor */
  public BreadCrumbBean() {
     model = new DefaultListModel();
     model.addItem("home","Home");
  }

/* Defining methods */
  public IListModel getMyDefaultListModel() {
     return this.model;
     }
  public void setMyDefaultListModel(IListModel bc) {
     model = bc;
     }
  }

            

Using the bean in the JSP

               <%-- We introduce the bean to the JSP with the id "myBean".   --%>
<%-- With the id we have access to the bean in the JSP.       --%>
<%-- As class name we have to specify the package name        --%>
<%-- plus the class name of the bean -                        --%>
<%-- that is "bean.BreadCrumbBean"                            --%>
 <jsp:useBean id="myBean" scope="application" class="bean.BreadCrumbBean"
 />
 <hbj:breadCrumb id="breadCrumb"
        tooltip="Click to move back to ..."
        onClick="goToPage"
        size="SMALL"
        behavior="DEFAULT"
<%-- model is specified with the id used in useBean (myBean)  --%>
<%-- and the property model                                   --%>
        model="myBean.myDefaultListModel"
 />

            

Special Naming Convention - model

The method name model invokes a special process. If you define getModel and setModel methods in your bean you can refer in the JSP to your bean like that:

               <jsp:useBean id="myBean" scope="application" class="bean.BreadCrumbBean"
/>
      <hbj:breadCrumb id="breadCrumb"
           tooltip="Click to move back to ..."
           onClick="goToPage"
           size="SMALL"
           behavior="DEFAULT"
<%-- model is specified with the id used in useBean (myBean)    --%>
<%-- and the property model                                     --%>
           model="myBean.model"
      />

            

Because of an internal convention that becomes effective when using the name model, the scope of the useBean statement is omitted and the model will be looked up in every scope in the following order:

page

request

session

application

In case you want to use different models that you transfer in different contexts (for example, session and application) you have to use another name for the get and set methods.

This special case however is only in effect when you use the model tag as shown in the example above ( model="myBean.model" ). If you refer to your model in a scriptlet, the model is taken from the scope declared in the useBean statement only.