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.
Definition of the bean:
package bean; |
Using the bean in the JSP
<%-- We introduce the bean to the JSP with the id "myBean". --%> |
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" |
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.