LatinCanuck
LatinCanuck

Reputation: 463

Displaying java list content using JSTL per table row

I'm having trouble with displaying data in an html table using java jstl. I'm hoping someone has done this before and can offer guidance or an example.

This is how the table(rows, data) is currently displaying :

Apple, Orange, Mange
100, 101, 102
200, 201, 202
300, 301, 302

But this is how I actually need it to display

Apple, 100, 200, 300
Orange, 101, 201, 301
Mango, 102, 202, 302   

This is my current JSTL display code

<c:forEach items="${dataSetList}" var="currentDataSetObj">
    <tr>
     <c:forEach items="${currentDataSetObj.myList}" var="currentValueOfDataElement" varStatus="loopCount" >
          <td>  <c:out value="${currentValueOfDataElement}" />  </td>
     </c:forEach>
     </tr>
</c:forEach>

And this is the server code

public class DataSet {
    List<String> myList = new ArrayList<String>();
    // more stuff not relevant to question here
}

List<DataSet> dataSetList = new ArrayList<DataSet>();
DataSet dataSetOne   = new DataSet();
DataSet dataSetTwo   = new DataSet();
DataSet dataSetThree = new DataSet();
DataSet dataSetFour  = new DataSet();

List<String> fruitA = new ArrayList<String>();
List<String> priceA = new ArrayList<String>();
List<String> priceB = new ArrayList<String>();
List<String> priceC = new ArrayList<String>();

fruitA.add("Apple");
fruitA.add("Orange");
fruitA.add("Mango");
priceA.add("100");
priceA.add("101");
priceA.add("102");
priceB.add("200");
priceB.add("201");
priceB.add("202");
priceC.add("300");
priceC.add("301");
priceC.add("302");

dataSetOne.setMyList(fruitA);
dataSetTwo.setMyList(priceA);
dataSetThree.setMyList(priceB);
dataSetFour.setMyList(priceC);

dataSetList.add(dataSetOne);
dataSetList.add(dataSetTwo);
dataSetList.add(dataSetThree);
dataSetList.add(dataSetFour);

request.setAttribute("dataSetList", dataSetList);

Upvotes: 0

Views: 1189

Answers (2)

Abhishek Nayak
Abhishek Nayak

Reputation: 3748

@ZaoTaoBao has answered your question,


Apart from your problem:

make your server code simple follow OO design in Java.

instead of creating many List's , create a class to hold fruit information like:

public class Fruit {
    private String name;
    private double[] prices;        
        //getters, setters & constructor 
}

then create one List object to hold Fruit's like:

List<Fruit> fruitList = new ArrayList<Fruit>();

Fruit appleFruit = new Fruit("Apple", new double[]{100.0, 200.0, 300.0});
Fruit orangeFruit = new Fruit("Orange",new double[]{101.0, 201.0, 301.0});
Fruit mangoFruit = new Fruit("Mango",new double[]{102.0, 202.0, 302.0});

fruitList.add(appleFruit);
fruitList.add(orangeFruit);
fruitList.add(mangoFruit);

request.setAttribute("fruitList", fruitList);

and render Fruits in jsp using single <c:forEach..

<c:forEach items="${fruitList}" var="currentFruit">
    <tr>      
       <td> 
        <c:out value="${currentFruit.name}" />        
       </td>       
        <c:forEach items="${currentFruit.prices}" var="price">
            <td>
                ${price}
            </td>
        </c:forEach>       
    </tr>
</c:forEach>
</table>

See Also: Object Oriented Design

Upvotes: 2

ZaoTaoBao
ZaoTaoBao

Reputation: 2615

the easiest way to do it is changing the value's order inside dataSet.

for example:

fruitA.add("Apple");
fruitA.add("100");
fruitA.add("200");
fruitA.add("300");

Upvotes: 2

Related Questions