aspose file tools*
The moose likes Struts and the fly likes How to use logic:iterate tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to use logic:iterate tag" Watch "How to use logic:iterate tag" New topic
Author

How to use logic:iterate tag

surendar prabu
Ranch Hand

Joined: Jul 24, 2006
Posts: 102
I am using logic:iterate tag to output a table from my jsp. Every time i define a bean class which has data for each column of the grid and i populate a List with that bean class object.

Is there any other way where i can use this tag with out defining a bean class? say i have a String Array where i have data for a column or a row. Just i want to know whether we can use logic:iterate tag without a separate bean class. How can i use an array object instead of a List and iterate around it?

some one pls help me regarding this.

regards,
Surendarprabu.R


SCJP 1.4
Dom Lassy
Ranch Hand

Joined: May 05, 2006
Posts: 181
Although you should use a bean, you can iterate over a string array.

In your action class:

In your jsp:

[ February 13, 2007: Message edited by: Dom Lassy ]
surendar prabu
Ranch Hand

Joined: Jul 24, 2006
Posts: 102
sorry for continous post.
I have gone through other threads related to logic iterate. so here i am posting my code to get it clear.

My Action class

public class MyAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception{
MyForm myForm = (MyForm)form;
String[] myarray = //this array is loaded by me from database result set. I dont know the size.
myForm.setMyStringArray(myarray);
}
}
My ActionForm
public class MyForm extends ActionForm
{
private String[] myStringArray;
public void setMyStringArray(String[] myStringArray)
{
this.myStringArray = myStringArray;
}
public String[] getMyStringArray()
{
return myStringArray;
}
// I am doing no other initialization for array in ActionForm as i dont know the size of it.
}

My JSP
<logic:iterate name="MyForm" id="MyArray" property="myStringArray">
<bean:write name="MyArray" property="myStringArray"/>
</logic:iterate>
When executing i am getting the exeption with message
No getter method for property myStringArray of bean MyArray
Usually to avoid this i will write a bean class and add a List to my ActionForm which holds objects of the bean class. I understand that there are some changes i have to make in my ActionForm. but i dont know what changes to make.

pls help me.

regards,
Surendarprabu
surendar prabu
Ranch Hand

Joined: Jul 24, 2006
Posts: 102
Thanks Dom. I got it working. Any way i will try to apply this over multirow table where each array reperesents a column.
surendar prabu
Ranch Hand

Joined: Jul 24, 2006
Posts: 102
I have tried what Dom has given and it works for a single collection or array which is i can say as single column of a table. but still i am not sure about how to achieve this for multiple columns of a table.

the basic structure i need to get is something like this.
<table>
<logic:itrate id="mycollection" name="myForm" property="Mylist">
<tr>
<td><bean:write name="mycollection"/>
<td><bean:write name="mycollection"/>
<td><bean:write name="mycollection"/>
</tr>
</logic:iterate>

Here in my present case i write a class which will have get and set for each column and i will load that class objects into an ArrayList and iterate around that. I dont want to write that extra class now, but still is it possible to get what is required?
I dont know how many Arraylist or Hashtable i must have in order to achieve this. help me on this regard.

regards,
Surendarprabu
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
surendar,

If you're going to be using Struts for a while, and you don't like writing JavaBeans, I suggest you just "get over it". Javabeans help you solve problems in an object-oriented way, and they're very easy to write, especially if you have an IDE that generates the getters and setters for you. Javabeans have a name for each item (age, birthday, etc) which makes your code much easier to read than col[0], col[1], etc.

Having said that, yes, there is a way to do multidimensional arrays using the logic:iterate tag.



The above code assumes colsAndRows is either a double-dimension String array (String[][]) or a Collection that contains Collections.
[ February 17, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
surendar prabu
Ranch Hand

Joined: Jul 24, 2006
Posts: 102
Thaks Merill,
I got the idea. My basic doubt is this. i have around 10 screens which have around 2 table each. If i use bean class for each table, which i am doing now, Will that be efficient? or using collections is efficient? My application will be used by around 50 users at same time and my server is tomcat 5.0. Which will give me better performance?

But i find the bean class advantageous to apply the styles in the grid as i require based on the data. If i go by collection i have to use logic:equal aginst the index of the collection or array to get it done. Or I have to maintain a single bean class, which i have to load based on the columns i have.
pls give me a suggestion.

regards,
Surendarprabu
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by surendar prabu:
Which will give me better performance?

This is the wrong question to ask when you're designing an application. Don't get me wrong: One obviously can't ignore performance considerations. However, I've seen way too many developers get sidetracked on this relatively insignificant question.

The important questions you should be asking instead are:
  • Which technique/framework/methodology will make my application easily understood and maintainable by other developers?
  • How can I make my application flexible enough to be easily modified when the inevitable change requests start coming in?
  • Which technique best follows accepted patterns and standards for software development?
  • These questions are orders of magnitude more important than "which performs best?".

    When asking the above questions, the use of JavaBeans far surpasses the use of Collections and arrays of Strings. Quite frankly, I wouldn't care if JavaBeans performed 10 times worse than collections. I'd still use them because they meet the needs of maintainability, flexibility, and adherence to standards so much better.

    Now after that diatribe, I'll finally get around to answering your question: There is virtually no difference in performance between using JavaBeans and using collections and arrays of Strings. Sure, you'll have a lot of Javabeans in your classpath, but so what? The latest JVMs can handle this quite easily.

    Some developers get the idea that if they have to write a lot of classes and create a lot of layers in their application, this is somehow going to slow down their application. Nothing could be further from the truth. JVMs are designed to be efficient at branching between code in one class to that in another. Early JVMs were slow at instantiating objects, but not any more. You still don't want to instantiate more objects than are necessary, but you don't have to worry about the cost nearly as much as in the early days.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to use logic:iterate tag