This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

useBean standard action

 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I far as I understand standard action were introduced to avoid scripting elements.

UseBean action declares and initializes scripting variable but what for? There is no way to access the variable but using scriptlets or scripting expressions.

Another doubt. I know there are also getProperty and setProperty actions that operate on the declared bean but on the low level I do not clearly understand how they work.
jsp:getProperty for example uses findAttribute so
it actually operates on an attribute, not on a scripting variable so for example something like this should work in my opinion but it doesn't, Why?:

<%
Person p = new Person(); p.setName("jasiu");
pageContext.setAttribute("ppp", p, PageContext.SESSION_SCOPE);
%>
<jsp:getProperty name="ppp" property="name" />
[ May 12, 2007: Message edited by: Jasiek Motyka ]
 
Hendy Setyo Mulyo
Ranch Hand
Posts: 219
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jasiek,

When you use <jsp:getProperty name=.... /> the name attribute specifies the name of the bean instance as declared by a previous <jsp:useBean> action. So maybe you can try to insert <jsp:useBean id="ppp" /> as below:

<%
Person p = new Person(); p.setName("jasiu");
pageContext.setAttribute("ppp", p, PageContext.SESSION_SCOPE);
%>
<jsp:useBean id="ppp" />
<jsp:getProperty name="ppp" property="name" />
 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response. But I still have doubts.
I know how to use bean standard actions.

But I don't know what is the reason for the fact that useBean action declares bean object (as scripting variable)
There is no way to access that variable but using scripting variables.
Look for the example:



name property comes from com.foo.bar.beans.Person
id property comes from com.foo.bar.beans.Employee

Despite I declared the type for Person the above code still works! So what is the reason for the type attribute? It only affects the type of the scripting variable.
But using scripting variables by scripting elements is bad and was to be eleminated by introducing standard actions.
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to use EL expressions to access to your bean (Eg. ${myBean.myProperty}).

The type attribute is necessary if you have, for example, two jsp: the first creates Employees, and the second one creates Directors.
Without the type attribute, you would get a 'ClassCastException' if the two pages are called into the same scope of your bean.
 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the servlet I create and set as a request attribute a new Person object.

In fact in that instance something like below would not work:

<jsp:useBean id="per" class="com.foo.bar.Employee" scope="request" />

So the type attribute to the rescue:

<jsp:useBean id="per" type="com.foo.bar.Person" class="com.foo.bar.Employee" scope="request" />

But still when I want to get property I get ClassCastException

<jsp:getProperty name="per" property="name" />

So what is the advantage of the type attribute. The object is implicitly cast to Employee

Could you give me a better example?
[ May 15, 2007: Message edited by: Jasiek Motyka ]
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I hope Person is a superclass for Employee...

Consider that 'type' is used when you have to reference a bean, while 'class' or 'beanName' is used to instantiate it.

In my previous example, 'type' may be a Person, while the two jsp would set 'class' as 'Employee' and 'Director', respectively. Obviously, you should use only the bean properties provided by 'type'.

Can you attach all your example files in the next message?
 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Thanks for your response...



Below jsp/TestStandardActions.jsp




When I call jsp:getProperty under the hood there is a cast to com.foo.bar.Employee causing ClassCastException even though I declared the type to com.foo.bar.Person. So every use of jsp:getProperty inserts a cast to a class declared in the class attribute.

Getting back to your example with two jsp pages.

Suppose the first page did not found a bean in a session scope and created new one an Employeee instance. Next there is request to the second page. The bean was found and was assigned to the reference of type Person. Next jsp:getProperty was invoked according to what I written above ClassCastException would occur (due to cast an Employee instance to Director class). So I still don't see the point of type attribute.

Well... I assumed Director extends directly from Person, and Employee extends from Person too. Maybe wrong. But If Director extended from Employee there would be no need for type attribute neither.

Could you show me your example because I must misunderstand it.
[ May 16, 2007: Message edited by: Jasiek Motyka ]
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've reproduced your problem. Watching the java class created by the application seems the problem is when you use <jsp:getProperty> for a bean with 'type' and 'class' attributes: in this case, <jsp:getProperty> is translated with ((<Class> pageContext.getAttribute(<id>, <scope> ).get<Property>, instead of using type 'type' attribute.
Using scripts or EL the problem does not happen, because the <useBean> behaviour is as described in the previous topics.
Moreover, if you replace 'class' with 'beanName' the problem does not occur, because in this case <jsp:getProperty> uses Beans facilities, instead of direct casting.

Saying the truth, I've never seen such a strange behaviour. I thought to post it as a new message, maybe I'm forgetting something. However, reading the specs, it is not clear if <jsp:getProperty> must use 'class' or 'type' to identify the object type.

P.S. you are using a 2.3 servlet specs for your web.xml. Is it a desired configuration? If so, remember to set the IsELIgnored as false for your jsp, if you want to try to use EL.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic