Results 1 to 4 of 4

Thread: How to extract an object from Model using a concat String

  1. #1
    Join Date
    Jan 2010
    Posts
    6

    Default 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!

  2. #2
    Join Date
    Nov 2009
    Location
    Amsterdam, The Netherlands
    Posts
    54

    Default

    How about adding a list/map of errors to the model? That would be easy to iterate in jsp.

  3. #3
    Join Date
    Jan 2010
    Posts
    6

    Default

    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...

  4. #4
    Join Date
    Nov 2009
    Location
    Amsterdam, The Netherlands
    Posts
    54

    Default

    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
  •