• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to define multiple beans

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic