1

I am trying to display string array in JSP page.

I have got a test String array in my controller set this to my registration model

String[] test={"ab","cb","sc","ad"};
registration.setTestArray(test);

Now I am trying to display it in jsp Its working fine if I do like this

<tr>
        <c:forEach var="arr" items="${registration.testArray}">
            <td>${arr} </td>
        </c:forEach>
    </tr>

But my problem is I want to display only some of the values from this array like 2nd and 4th index of this array.

I tried like

<tr>
        <c:forEach var="arr" items="${registration.testArray}">
            <td>${arr[2]} </td>
        </c:forEach>
    </tr>

but its throwing an error. This is just a test in my actual project I have long array of array from which I have to display some selected values.

I am thinking of doing this by first process my required values in controller and then display it in jsp. But I am not sure is this the best method. It would be great help if someone suggest me the better way.

1
  • You have a logical condition to display the values? If you want to hard code the position in the array there's no need to loop. Commented Sep 26, 2011 at 22:11

1 Answer 1

1

It depends on how you get these "selected values". You can:

  • ${registration.testArray[2]}
  • you can loop using a specific step of the c:forEach tag
  • you can loop everything and check <c:if test="${selectedValues.contains(arrItem)}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Sergio Michels and Thanks Bozho. It really helped me

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.