1

Hi there I am using Spring MVC and I am trying to display some data from multiple Lists in a jsp page. I've searched and found some similar topics but I wasn't able to achieve what I am trying to do plus got more confused. My model class contains something like this:

private String BlaBla1;

private String BlaBla2;

private List<String> Alpha;

private List<String> Beta; 
.....
//getters setters

What I want is to display a table in a jsp with the values from these two Lists (Alpha and Beta ..) one value at a column.They both have the same number of values. For Example

<tr><td>Alpha.value1</td><td>Beta.value1</td></tr>
<tr><td>Alpha.value2</td><td>Beta.value2</td></tr>
......................................
<tr><td>Alpha.valueN</td><td>Beta.valueN</td></tr>

As I've seen here rendering data in jsp using spring controllers and different classes

and some other examples, they create something like that: List<MyObjects> objects but MyObjects model always has private String ... and NOT any List<String>..

I tried to construct something like that

Map<String,List<String>> test = new HashMap<String,List<String>>();

then

test.put("alfa", Alpha);
test.put("beta", Beta);

but all I got was just display them in 2 rows and a single column using

<c:forEach var="testValue" items="${test}">
    <tr><td>${testValue.value}</td></tr>
</c:forEach>

Please don't aske me to change my Model class, it is 'impossible'. I've seen somewhere saying to use Collection but I'm not sure how to do that.

Any suggestion would be useful, happy coding!

2
  • "but all I got was just display them in N rows and a single column using." Does this mean you are now getting all the correct list data displayed but it's just all displayed incorrectly i.e in a single column? Is that what's wrong? Commented Apr 25, 2015 at 20:56
  • Well to be honest not exactly..you are right!All I get is all the values from each List in a row..so all values from Alpha List in row 1 and all values from Beta List in row 2..So no, they are not displayed correctly!I just can't use ${testValue.Alpha}</td><td>${testValue.Beta}</td></tr> as in other examples as above with the List<MyObjects> I am editing my original post (Sorry) Commented Apr 25, 2015 at 21:06

2 Answers 2

2
model.addObject("alphaList", alpha);
model.addObject("betaList", beta);

In the jsp :

<c:forEach var="listItem" items="${alphaList}" varStatus="theCount" >
    <tr><td>${listItem}</td><td>${betaList[theCount.index]}</td></tr>
</c:forEach>

Note : ${theCount.index} starts with 0 ${theCount.count} starts with 1.

So basically you can use the count to iterate over the second list.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Nikhil and credits to Serge Ballesta for his answer too!
1

You just have to iterate the Alpha list normally ... but with use of the varStatus attribute of <c:foreach > tag. This varStatus has (among others) an attribute index which is the index of the iteration and all what you need to display corresponding element from Beta list. Code example :

<c:forEach var="alphaVal" items="${Alpha}" varStatus="status">
    <tr><td>${alphaVal}</td><td>${Beta[status.index]}</td></tr>
</c:forEach>

1 Comment

Oh hi thanks for posting. I'll try that also but I have one small question..where do I put Beta in the controller? I mean should I use model.addAttribute("..", Alpha); and model.addAttribute("..", Beta); or Should I put them inside another List or Map-HashMap? I've tried many things and I am lost..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.