• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using bean:define with collection

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all...
i want to use <bean efine > to define an ArrayList Collection.. tha have the same result of this:
<%
ArrayList list=new ArrayList();
..
..
..
list.add("add");
%>

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

************************************************************************
another question :
<%
int x=3;
int y=5;
if(x>y)
//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....

thnx
[ July 13, 2006: Message edited by: Ala'a Hendi ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic