• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

useBean standard action.

 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the useBean standard action we can use class and type attributes in combination.

Is there is any specific case where you should use both in one useBean standrad action.

Thanks
 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must use the class & type attribute together if the bean tht u r referring to is not available in the said scope already. It will throw an IllegalStateException.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jayashree,

I think this can be done without the type attribute. Only class attribute is sufficient.

Thanks
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the useBean standard action we can use class and type attributes in combination.


Yes u can use both class & type.


Is there is any specific case where you should use both in one useBean standrad action.


This is clearly explained in SCWCD exam study kit.If u dont have one here is a simple example.

interface animal{}

class dog implements animal{}

class cat implements animal{}

In java we get used to use the stmt something like this :

animal a = new dog();

What is it implying?.Parent reference is pointing to child object.In the same way we can use it for usebean declaration in JSP also.

<jsp:usebean id="pet" class="dog" type="animal" scope="session"/>

The above declaration is equivalent for the following stmts

animal pet = (animal)session.getAttribute("pet");

if(animal==null){

pet = new dog();

}

hth..

Regards,
Priya.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priya



<jsp:usebean id="pet" class="dog" type="animal" scope="session"/>


I want to say, here I know I am using the the dog class. and it is subclass of animal. So, is there is any need to specify type="animal". If I pass other subclass of the animal (cat). This is error due to the class="dog".
so there is no need to specify the type="animal" attribute. I think it is reductant. I want to know, there is any particular case, where it is necessary to specify both please tell me, because I personaly think there is no need to specify both in the useBean.

Thanks
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok u r asking for when/why it is used rather than how it used..am i right?.

Considering the same example u can create dog instance in 2 ways..

dog d = new dog();

or

animal a = new dog();

These are the 2 possible ways to create a dog instance..nothing can state u should use the one over the other..coz that is upto the designer who design the java class / JSP.How u r goin to make use of that bean object in ur JSP is left to you.

Coming to ur doubt in case u dont specify type attribute for the usebean declaration instantiation will be the first scenario..else if u specify type attribute as "animal" it'll be of the second one.Both are legal!!

Regards,
Priya.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Priya, I got the clue.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic