my dog learned polymorphism*
The moose likes JSP and the fly likes jsp:useBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp:useBean" Watch "jsp:useBean" New topic
Author

jsp:useBean

Sartoris Snopes
Greenhorn

Joined: Jan 12, 2004
Posts: 4
say I have a couple of beans
class BeanA {
private BeanB = new BeanB();
public get/setBeanB(...);
}
class BeanB {
private String s;
public get/setS(...)
}
And I have a JSP:
<jsp:useBean id="beanA" class="BeanA" ... />
..
How do I get/setS from BeanB?
In other words, how do I dig into a bean and get inner beans?
Thanks in advance.
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
In order to access class B, you can do any of the following:
1.) instantiate class B as a Bean (i.e. jsp:useBean)
2.) instantiate an instance of class B in your jsp page
3.) use composition (i.e. class A maintains an instance member of type class B)
If you use scheme 1, you can access class B by using getAttribute and setAttribute (i.e. B.getAttribute("color")) or by calling B's gets and sets directly (i.e. B.getColor())
If you use scheme 2, you can use class B's gets and sets directly (i.e. B.getColor()).
And of course, if you use scheme 3, you'll have to go through class A first, in order to retrieve instance members of class B.
Good Luck,
WS


for (int i = today; i < endOfTime; i++) { code(); }
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Well, you could use a scriptlet:

or you could add extra top-level methods to BeanA

or you could use the new "EL" syntax (if you are using JSP 2)

Have any of those options helped?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Sartoris Snopes
Greenhorn

Joined: Jan 12, 2004
Posts: 4
Unfortunately, JSP 2 is not available to me now.
This
"<jsp:useBean id="beanA" class="BeanA" scope="session" />the value of s is ${beanA.beanB.s}"
is close to what I want.
Smoething like this
"<jsp:useBean id="beanA" class="BeanA" scope="session" />the value of s is <jsp:getProperty name="beanA" property="beanB.s" />
I saw something like this in BeanUtils (I think it was called getNestedProperty()). But, alas, I have not found how to do this using <jsp:...>
Sartoris Snopes
Greenhorn

Joined: Jan 12, 2004
Posts: 4
I found a solution, although it requires struts.
I can use the <bean efine /> to create/copy/get a bean which is contained in another bean. From there I can use <jsp:getProperty /> to get the inner attribute of beanB.
I still would like to know if there is a JSTL only solution.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
The first solution I gave does not need JSP 2. You would use it like:


Do you have an aversion to using a scriptlet? If so, the usual answer is to use similar code but put it into a custom tag.
Sartoris Snopes
Greenhorn

Joined: Jan 12, 2004
Posts: 4
Originally posted by Frank Carver:
Do you have an aversion to using a scriptlet? If so, the usual answer is to use similar code but put it into a custom tag.

Scriptlets are ok. I'm really just kicking the tires of JSTL, etc right now. But a goal is to move all my jsps to be valid xml (or as close as possible).
Thanks.
 
Consider Paul's rocket mass heater.
 
subject: jsp:useBean
 
Similar Threads
Need to use a bean inside static block of another bean
Response mismatch in cluster environment
BMT not propogated to invoked beans
Precision concerning Stateful and Singleton beans
struts iterate tag