This action lets you load in a JavaBean to be used in the JSP page. It locates or instantiates a Bean with a specific name and scope.
< jsp:useBean id="t1" scope="session" class="session.Time" />
this is quit similar to..
Time t1 = new Time ();
more over this you can specify a scope attribute...
Examples
< jsp:useBean id="cart" scope="session" class="session.Carts" />
< jsp:setProperty name="cart" property="*" />
< jsp:useBean id="checking" scope="session" class="bank.Checking" >
< jsp:setProperty name="checking" property="balance" value="0.0" />
< /jsp:useBean>
Description
The < jsp:useBean> tag attempts to locates a Bean, or if the Bean does not exist,
instantiates it from a class or serialized template. To locate or instantiate the Bean,
< jsp:useBean> takes the following steps, in this order:
1. Attempts to locate a Bean with the scope and name you specify.
2. Defines an object reference variable with the name you specify.
3. If it finds the Bean, stores a reference to it in the variable. Gives the Bean you
specified type.
4. If it does not find the Bean, instantiates it from the class you specify, storing a
reference to it in the new variable.
5. If it has instantiated (rather than located) the Bean, and if it has body tags
(between < jsp:useBean> and < /jsp:useBean>), executes the body tags.
Attributes and Usage
* id="beanInstanceName"
Names a variable that identifies the Bean in the scope you specify. You can use the
variable name in expressions or scriptlets in the same JSP file.
The name is case sensitive and must conform to the naming conventions of the page
scripting language (if you use the Java programming language, the conventions in the Java
Language Specification). If the Bean has already been created by another < jsp:useBean> tag,
the value of id must match the value of id used in the original < jsp:useBean> tag.
* scope="page|request|session|application"
Defines a scope in which the Bean exists and the variable named in id is available.
The default value is page.
* class="package.class"
Instantiates a Bean from a class, using the new keyword and the class constructor. The
class must not be abstract and must have a public, no-argument constructor. The package and
class name are case sensitive.
* class="package.class" type="package.class"
The value of type can be the same as class, a superclass of class, or an interface
implemented by class.
***very important
Once you have a bean, you can modify its properties via jsp:setProperty.
Recall that with beans, when you say "this bean has a property of type 'X' called foo",
you really mean "this class has a method called getFoo that returns something of type X,
and another method called setFoo that takes an X as an argument."
**Note that you can either supply an explicit value, give a param attribute to say that the
value is derived from the named request parameter, or just list the property to indicate
that the value should be derived from the request parameter with the same name as the
property.
Click here for detail document from Sun
Sunday, March 2, 2008
< jsp:useBean>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment