Forums Register Login

JavaBean "type" attribute

+Pie Number of slices to send: Send
I don't understand what the "type" attribute of a JavaBean does exactly.
Can someone give me an example of when I would use this?
Thanks,
Drew
+Pie Number of slices to send: Send
The attribute 'type' is used as java type of the variable whose name is same as the value of 'id' attribute.
Type id_val = (Type)pageContext.getAttribute(id_val,PageContext.***_SCOPE);
For example,
<jsp:useBean id="address" type="AddressBean" scope="session" />
will generate a java code as
AddressBean address = (AddressBean)(pageContext.getAttribute("address",PageContext.SESSION_SCOPE);
Here AddressBean is the base class/ interface. There can be more than one concrete subclasses such as BussinessAddressBean, HomeAddressBean which extend the base class. So in MVC (Model View Control) model, depending upon the business logic decision made, the controller servler may instantiate any one of the 2 types (BussinessAddressBean/HomeAddressBean) and make session.setAttrbute("address",sessionBean) and forward to the UI jsp.
All the Jsp cares about is the base class /(interface) type AddressBean. Since we are typecasting to the super class, the code will work well without any ClassCastException.
In future , if you add one more type of AddressBean and set in session also, the JSP will work without any problems.
Regards,
Maha Anna
+Pie Number of slices to send: Send
Thanks, that is helpful, but it's still a little confusing.
Let me ask this: What do I need to know about the 'type' attribute with respect to the SCWCD exam?
Drew
+Pie Number of slices to send: Send
FOr the SCWCD exam, we need to know that
1. 'type' attribute can be used in 3 combinations for <jsp:useBean /> tag. They are,
a. class and type
b. type
c. beanName and type
2. When used with 'class' attribute, it must be same class, a super class, or an interface implemnted by 'class'. If the particular object with 'id' name is not found in the 'scope', then a new object of 'class' is instantiated and typecasted to 'type' and stored in 'id' attribute.
3. When used alone, the bean with given id should exist in given scope. Otherwise InstantiationException will be thrown.
4. When used with beanName, a bean is instantiated from a serialized file.
Please refer to this link.
http://java.sun.com/products/jsp/tags/12/syntaxref1217.html#8865
type="package.class"
If the bean already exists in the scope, gives the bean a data type other than the class from which it was instantiated. The value of type must be a superclass of class or an interface implemented by class.
If you use type without class or beanName, no bean is instantiated. The package and class name are case sensitive.
class="package.class" type="package.class"
Instantiates a bean from the class named in class and assigns the bean the data type you specify in type. The value of type can be the same as class, a superclass of class, or an interface implemented by class.
The class you specify in class must not be abstract and must have a public, no-argument constructor. The package and class names you use with both class and type are case sensitive.

beanName="{package.class | <%= expression %>}" type="package.class"

Instantiates a bean from a class, a serialized template, or an expression that evaluates to a class or serialized template. When you use beanName, the bean is instantiated by the java.beans.Beans.instantiate method. The Beans.instantiate method checks whether the package and class you specify represents a class or a serialized template. If they represent a serialized template, Beans.instantiate reads the serialized form (which has a name like package.class.ser) using a class loader.

The value of type can be the same as beanName, a superclass of beanName, or an interface implemented by beanName. The package and class names you use with both beanName and type are case sensitive
Regards
Maha Anna
[ December 02, 2002: Message edited by: Maha Annadurai ]
You guys haven't done this much, have ya? I suggest you study this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1014 times.
Similar Threads
Standard Action tags?
target attribute of c:set
jsp:get and setProperty doubt?
Please answer me this question.
JavaBean Properties initialization
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:21:06.