• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use Bean query

 
yeshwanth reddy
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Can u tell me why beanName is used in a useBean. Is anything mentioned about it hfsj?


<jsp:useBean id="softDrink" beanName="Drink.ser" type="com.baboon.interface.Drink" />

Regards,
Yashwanth
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HFSJ didnt say anything about beanName ...... i think it is equivalent to 'class'.
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

No, beanName is not equivalent to class. beanName specify the name of a bean (a REAL javaBean) as you would use in a instantiate() method of Java Beans. This attribute MUST go along with the type attribute, defining the object type. So, the only way to use beanName is like this :

<jsp:useBean id="foo" beanName="name" type="org.phoenix.bar" />

If you see beanName without type, this IS an error.

See specs for further explanations.
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between these two syntax?

<jsp:useBean id="foo" class="org.phoenix.bar" />

<jsp:useBean id="foo" beanName="name" type="org.phoenix.bar" />

can u plz point out a situation where we can use beanName?

Thanks...
 
yeshwanth reddy
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

"instantiate() method of Java Beans"- what does this mean?

Can u please be more clear on what beanName is used for?Does it specify the name of the java bean (class name?) and the type here is used to specify what type the java bean is?


Regards,
Yashwanth
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BeanName specify the name of a bean (a REAL javaBean).It means that beanName should follow java naming rule.
Look at this question,

Which of the following are correct JSP useBean declarations? [Check all correct answers]

1. <jsp:useBean id="softDrink" beanName="Drink.ser" type="com.baboon.interface.Drink" />
2. <jsp:useBean id="softDrink" beanName="Drink.ser" class="com.baboon.interface.Drink" />
3. <jsp:useBean id="card" class="com.baboon.cardgame.Card" />
4. <jsp:useBean beanName="instructor" class="com.baboon.users.Instructor" />
5. <jsp:useBean id="iDevice" type="com.baboon.types.Device" />

Answers given 1,3,5

In answer 1,beanName did'nt follow java naming rule.So how come it is correct?

my concusion is,

The beanName attribute can refer to a class name or the name of a serialized file containing the bean.We can either use beanName or class , it means the same but in addition beanName can also refer to a serialized bean file name.The beanName and class attributes cannot be used together.

Request anyone to clarify...


Thanks.
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i forgot about the serialized file. Yes it works with a serialized bean.
And beanName may ONLY be used WITH the type attribute.

From the JSP spec (JSP 5) :
The attribute beanName specifies the name of a Bean, as specified in the
JavaBeans specification. It is used as an argument to the instantiate method in the java.beans.Beans class. It must be of the form a.b.c, which may be either a class,or the name of a resource of the form a/b/c.ser that will be resolved in the current ClassLoader. If this is not true, a request-time exception, as indicated in the semantics of the instantiate method will be raised. The value of this attribute can be a request-time attribute expression.


From Beans API, the instantiate method description :
instantiate

public static Object instantiate(ClassLoader cls,
String beanName)
throws IOException, ClassNotFoundException

Instantiate a JavaBean.

Parameters:
cls - the class-loader from which we should create the bean. If this is null, then the system class-loader is used.
beanName - the name of the bean within the class-loader. For example "sun.beanbox.foobah"

Throws:
ClassNotFoundException - if the class of a serialized object could not be found.
IOException - if an I/O error occurs.
[ March 14, 2006: Message edited by: Frederic Esnault ]
 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think one more important difference is beanName can accept the jsp expression also,whereas class cannot.
Ref: Hanumant Deshmukh.
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes...the beanName attribute can accept a request-time attribute expression
as a value.

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic