Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:useBean

 
Sartoris Snopes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sartoris Snopes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic