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

using bean:define with collection

Alan Hermin
Ranch Hand

Joined: Feb 16, 2006
Posts: 290
hi all...
i want to use <bean efine > to define an ArrayList Collection.. tha have the same result of this:
ArrayList list=new ArrayList();

any one can help me how to write <bean : define........

another question :
int x=3;
int y=5;
//do some thing.....;
And I want to use it in <logic:greaterThan> , how i can do this...bcz this logic tag support objects not premitives....

[ July 13, 2006: Message edited by: Ala'a Hendi ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm going to avoid your specific question for a moment to look at the big picture. Struts is a Model/View/Controller (MVC) framework and one of it's purposes is to make it easy to divide up your logic between view and model components. When you design a Struts application, you need to bear this in mind.

The reason I mention this is that a JSP is a view component and populating an ArrayList is generally not something you would want to do in a view component. You should do this in either one of your model classes, or in your Action class. Once the action class has created it and put it in some scope, the JSP can use it.

Now, back to your original questions. You can instantiate a new ArrayList with a <jsp:useBean> tag, but not with a <bean:define> tag. The thing you need to realize, though, is that the <bean:xxx> tags were meant to work only with JavaBeans and ArrayList is not a JavaBean. Even if you have an instance of it, you can't change it or add to it with <bean:xxx> tags or any other struts tags because it does not follow the JavaBean conventions for naming methods (they don't start with "get" or "set").

I'd suggest instantiating and populating the ArrayList in the action class that forwards to this JSP, and then putting it in request scope.

Regarding your second question, my advice is to not use <logic:xxx> tags at all, but to use JSTL tags instead. JSTL is much more powerful and more a standard part of Java EE.

JSTL does automatic type conversions, similar to JavaScript, and that would come in handy in your example. Here's how you'd do it in JSTL:

<c:set var="x" value="3" />
<s:set var="y" value="5" />
<c:if test="${x > y}" >do something</c:if>

Note that without JSTL's automatic type conversion, it would do a String comparison rather than a numeric comparison. In a string comparison, if x is "10" and y is "5", "x > y" would evaluate to false.
[ July 13, 2006: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
I agree. Here's the link:
subject: using bean:define with collection
It's not a secret anymore!