This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to define multiple beans

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I have two beans defined on my page and I want want of them to be used in a bean:define. I would think you could do something like this:

But that doesn't work because you can't define the same bean more than once. Is there any pattern for using either bean1 and bean2 as realBean?

[disabled smilies -JM]
[ June 07, 2004: Message edited by: Jason Menard ]
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should work as long as the conditionals are exclusive of each other. The following code worked for me and gave me the expected output:



The output was "value3". The conditionals ensure that I am only actually defining the bean once.

HTH
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,
I tried that and got an uninitialzed error at JSP compilation time. I guess it depends on the JVM. I'm using 1.3. What did you try it in?

As a workaround, I'm using Java code for the if/else and then using the bean:define on that value. It's a bit messy, but then again this whole thing is a workaround until I can get the logic out of the JSP.

(Sorry about the smilies. I didn't proofread my post.)
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How odd. I tested using Servlet 2.3, JSP 1.2, JDK 1.4.2, and Struts 1.1 I believe. I don't remember the exact version of Tomcat I was using. Just out of curiousity, did you run the exact code I posted above? Not that I'm doubting you, I just find it interesting.

I guess the other thing you might be able to do is determine in your Action which bean to put in scope, that way you always have the right bean available for your JSP without having to do the conditionals there.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,
I tried the exact code at work today. Same problem.

I wound up using my hack for now. In the future, I can refactor as you suggested. Thanks!
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this on the Struts site. This may explain the problem. Or not. :roll:

USAGE NOTE - There is a restriction in the JSP 1.1 Specification that disallows using the same value for an id attribute more than once in a single JSP page. Therefore, you will not be able to use <bean efine> for the same bean name more than once in a single page.
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was aware of that restriction but I was thinking maybe the conditional would prevent the error. Obviously that's not always the case, although as I said, it did work for me using JSP 1.2.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic