tags:

views:

36

answers:

3

I have a servlet ( that I cannot change ) to gather information to be displayed in on a web page. I use a bean in a JSP page to loop through the information.

I tried to simplify, but my information is stored tables basically like this alt text

So for example, the bean stores information in nested arrays like this:

---2009
------TOYOTA
---------BLUE 10
---------RED 20
------CHEVY
---------BLUE 30
---------RED 10
---2010
------TOYOTA
---------BLUE 30
---------RED 12
------CHEVY
---------BLUE 12
---------RED 20

This is great for when I display the report per year, per category, per color. I can easily loop through the years, loop through the categories, then loop through the colors. This is the report I have ( simplified ) alt text

Now I need to turn that information sideways. I need to create a report to display the count per color, per year. This is the report I need:

alt text

How can I rearrange the information within the JSP? Should I use arrays to gather the info, or is there an equivalent to a hash that I can use?

Thanks

+2  A: 

I don't think you should be doing such things in a JSP. Have a servlet handle the request, put the data in the correct form, and have the JSP simply display it. Sounds to me like you're asking the JSP to do something it shouldn't.

If those are objects, you should be able to iterate over a different key (color in this case) and redisplay without too much trouble. Have the servlet send down a second map in the right form.

Are you using JSTL? If you're not, you should be.

duffymo
They are objects, but they are nested - so the year object contains a list of category objects, each of which contains a list of color objects. Unfortunately I can't change the servlet, I'm stuck trying to unwind and rewind in JSP.
jeph perro
+1  A: 

To be consistant with your original approach, you could rearrange the nested arrays to look like the following:

---BLUE
------2009
---------TOYOTA 10
---------CHEVY 30
------2010
---------TOYOTA 30
---------CHEVY 12
etc

Your JSP code must look very bad having to iterate through all the arrays. My suggestion would be the following: Create a JavaBean class which represents a row in the table and pass a List of instances of this JavaBean class to the JSP for display:

JavaBean

public class ColorRow{
  private String color;
  private int year;
  //...
  //getters and setters
}

Servlet/Controller

List<ColorRow> colorTable = new ArrayList<ColorRow>();
//populate the list
request.setAttribute("colorTable", colorTable);

JSP

<c:forEach items="${colorTable}" var="row">
  <tr><td>${row.color}</td><td>${row.year}</td><!-- ... --></tr>
</c:forEach>
Michael Angstadt
A: 

I solved the problem by looping though the lists in the bean until I got to the color list, then started looping through again and again until I found all the records for each color.

<c:forEach var="year1" items="${Info.list}"  varStatus="yearCounter1" >
    <c:if test="${yearCounter1.count == 1}">
        <c:forEach var="clist1" items="${year1.list}" varStatus="catCounter1">   
            <c:if test="${catCounter1.count == 1}">
                 <c:forEach var="colorlist1" items="${colorlist1.list}" varStatus="colorCounter1">
                                 <!-- this gets me to all possible colors -->
                                 <c:forEach var="year2" items="${Info.list}"  varStatus="yearCounter2" >
                    <c:forEach var="clist2" items="${tlist2.list}" varStatus="catCounter2">
                                             ....
                                                          <c:if test="${colorCounter1.description==colorCounter3.description&& year2.description==year3.description  && clist2.description==clist3.description}" >

Is it efficient? No.

Is it a good use of JSTL? No.

But I am stuck. I cannot change anything in the bean, I had to use JSP to do the work.

jeph perro
Create another bean which wraps the original bean and use it instead.
BalusC