-
May 31st, 2010, 11:51 AM
#1
How to extract an object from Model using a concat String
Hello everybody...
I'm dealing with the following problem...
I have a controller that puts some values in the model based on an iteration index i (for example)
for(int i=0; i<aNumber; i++){
myModel.put(error+i, "error");
}
In my JSP page I have this "aNumber" value and I would like to do the following "for" iteration using the varStatus (in this example the size of the collection "questions" is equals to "aNumber"):
<c:forEach items="${model.questions}" var="qst" varStatus="loop">
<c:if test=${!empty model.XXX}>
Then print something!
</c:if>
</c:forEach>
I need to replace XXX to something that in Java would be:
error+loop.index
In other words I have to concatenate "error" with the index of the for iterator, and get
model.error1 in the first iteration
model.error2 in the second
etc etc
Do you have any idea if this is possible???
I have tried combinations like:
<c:if test=${!empty model.error+loop.index}>...
<c:if test=${!empty model.error+'loop.index'}>...
with no success...
I omit the others because are too nusty to be published in this forum 
Any help would be appreciated... Thanxs!
-
Jun 1st, 2010, 05:15 AM
#2
How about adding a list/map of errors to the model? That would be easy to iterate in jsp.
-
Jun 1st, 2010, 07:11 AM
#3
Hi weissed,
Thank you for your reply. I've been following your scheme in other views. My problem is that I have this particular view in which I need to iterate on a collection and every item in that collection can have an associated error.
<c:forEach items="${model.questions}" var="qst" varStatus="loop">
..
I can have an error related to every particular "question" item.
My strategy is the following:
I have a separated collection in which I put the errors associated to that question.
But now the problems is: how do I extract the errors from that particular question inside the <c:forEach > tag?
Maybe I'm just using the wrong strategy...
-
Jun 1st, 2010, 07:53 AM
#4
How about a view bean QuestionAndErrorsViewBean that contains the question and the collection of errors for that question?
You could build these view beans in the controller and have the view just render data without connecting the questions to the errors.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules