my dog learned polymorphism*
The moose likes Struts and the fly likes how to define multiple beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to define multiple beans" Watch "how to define multiple beans" New topic
Author

how to define multiple beans

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

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 ]

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

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

Joined: Nov 09, 2000
Posts: 6450
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

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

Joined: Feb 05, 2003
Posts: 4727

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.


A good workman is known by his tools.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to define multiple beans
 
Similar Threads
Struts 1 : How to get last element using Struts logic iterate.
logic:equal not working. Why ?
BeanUtils and null-properties.
mapping bean properties question
get list value using index in logic : iterate