• Post Reply Bookmark Topic Watch Topic
  • New Topic

useBean and class inheritance

 
John Holme
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to be able to assign the class attribute of a jsp:useBean tag dynamically at request-time. Depending on the servlet path, I set a different bean in the request before forwarding to my JSP. One of these beans is a super-class of the other.
I thought that assigning the value of the super-class to the "type" attribute would take care of this problem, allowing me to reference either class using the useBean id. But when I do that, I get compiler errors because the methods defined in my sub-class aren't defined in my super-class.
Alternatively, I tried to define a request-time expression (" <%= myClass %> ") as the value of the useBean "class" attribute, but request-time expressions are not legal as useBean attributes.
Finally, I tried using a scriplet which defines an if-statement to conditionally declare the useBean tag with the appropriate class attribute. However, in this case I get an error stating that duplicate useBean tags are not permitted (even though the tags are declared under mutually exclusive conditions).
Can anyone suggest a proven method for associating a useBean tag with a super-class, then invoking sub-classes of the superclass using the tag?
thanks in advance for your help!
John
 
John Holme
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the hack I'm using is to define the "get" methods used by the sub-class in the super-class as well; then jasper doesn't complain.
is there a better way?
thx.jh
 
amol re
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From my personal experience, if you are required to do some acrobatics like these, more often than not, its a hidden case to improve the design.
Still....one of the approaches could be...
Finally, I tried using a scriplet which defines an if-statement to conditionally declare the useBean tag with the appropriate class attribute.

Go one step further and instead of using the useBean tag, instantiate and appropriate class and put it in the appropriate scope.

and in your page, use

Btw, how are you making sure that at run time, get property with property name exclusive to derived will not be called if you have an instance of base?
Your problem at compile time can be solved...but you will have to take care of the run time scenarios...I would suggest relook basic desgin of the application. Maybe the use case that your are trying to realise right now wasnt considered at design time. Maybe a little tweaking somewhere inside the core will eliminate the need for you to do the acrobatics you are required to do right now. or maybe not. just wondering...
 
John Holme
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the appropriate class has been set in the expected scope.
"getProperty" requires a value for the "id" attribute which is the id value set in the useBean tag. that's where the problem is: jasper chokes if any methods of the derived class that are called from the JSP are not stubbed out in the super class. once those methods are stubbed out in the super class, setting useBean with the type of the super class works as expected when either the super class or the derived class are set in the expected scope.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!