• Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp:useBean and Nested Class  RSS feed

 
Dane Frazier
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hate to beat this tired horse some more, but I'm having probs with a nested class and the jsp:useBean syntax...

I have a class...


I can get everything I need for OuterFoo in my jsp:
<jsp:useBean id="myOuter" class="com.package.peril.OuterFoo" scope="page" />

but when I try to get to InnerFoo using same syntax ...
<jsp:useBean id="myInner" class="com.package.peril.OuterFoo.InnerFoo" scope="page" />
I get javax.servlet.ServletException: com.package.peril.OuterFoo.InnerFoo

however, I can use instead...
<%@ page import="com.package.peril.OuterFoo.InnerFoo" %> with problems.

Any ideas?

Thanks,
dane

[ May 27, 2004: Message edited by: Dane Frazier ]
[ May 27, 2004: Message edited by: Dane Frazier ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that when you use the useBean tag, under the covers it is using Beans.instantiate( ClassLoader cl, String classname ). The Beans.instantiate() method expects the classname to be in the form "package.Outer$Inner".

If you try to specify the class attribute as "package.Outer$Inner", it will load the bean, but it will fail on casting the bean to the appropriate class. Casting expects the full classname to be in the form "package.Outer.Inner", but it tries to use "package.Outer$Inner" that you passed in as the class attribute.

Next I tried to use the useBean tag with a class attribute specified as "package.Outer$Inner" and a type attribute specified as "package.Outer.Inner". It worked!

However, when I later tried to use the getProperty tag to retrieve a value from the bean I got a class cast exception because it was attempting to cast the bean to the specific class specified earlier after pulling it out of the page context. (It was using the form "package.Outer$Inner" rather than "package.Outer.Inner".) I was able to pull values from the bean using JSP expressions, though. ( <%= beanName.getX() %> )

All of this was done in Tomcat 4.1.29, so I'm not sure about other JSP/servlet containers, but my advice would be to not use inner classes as Java Beans in JSP. Was there any specific reason why you needed to instantiate the inner class as a JavaBean?
[ May 28, 2004: Message edited by: Nathan Pruett ]
 
Dane Frazier
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Super Information. I'm also using 4.1.2x.

Thanks.

I didn't realize what was really going on under the hood.

As to the use of the inner class - It was simply an attribute class. I needed to use it to cast my returned values out of my linked list (or map, or what ever else). I was hoping to use setProperty on the inner attributes as well for form collection / validation. However if the getProperty doesn't work, I doubt the setProperty does either. I'll test it.

Otherwise, it's cut-n-paste to a new top level class for the attributes, and import at the class level.

Thanks again.

-dane
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!