• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to use logic:iterate tag

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
surendar prabu
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
     
    What a show! What atmosphere! What fun! What a tiny ad!
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic