File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Accessing formbean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Accessing formbean" Watch "Accessing formbean" New topic

Accessing formbean

Henrik Krievs
Ranch Hand

Joined: Jan 05, 2004
Posts: 60

In my struts application - I have a list defined in a formbean - which I show on the JSP - with regular iteration Struts tags.

Now, to be able to layout the list in two columns - I need to know the size of the list before hand.

How can I access this list?

If I somehow could access the list - like;


List list = myFormBean.getList();
int listSize = list.size();


But I get the error myFormBean is unknown. The I could use a useBean tag - but again - I don't get access to the same instance of the bean - as Struts uses.

How do I solve this problem?

Best Regards,
Zip Ped
Ranch Hand

Joined: Jul 26, 2005
Posts: 336
Use a property - say noOfRecords, in your form bean which stores the size of the list. Use a logic tag(logic:greaterThan, logic:lessThan) in your JSP to check the value of the noOfRecords and display your list accordingly.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you want to access a bean in a scripting variable, you must get it from the scope in which it is stored. So, if myFormBean is in request scope, you could access it like this:


List list = ((com.mycompany.MyFormBean)request.getAttribute("myFormBean")).getList();
int listSize = list.size();


Another way to do it would be to use the <bean:size> tag like this:

<bean:size id="size" name="myFormBean" property="list" />

This provides you with a page scoped variable named "size" which contains the size of the list in myFormBean.

Consultant, Sima Solutions
I agree. Here's the link:
subject: Accessing formbean
It's not a secret anymore!