3

I have a JSP that imports an interface. The interface has a String[] QUESTION_ARRAY.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="com.mypackage.message.Questions"%>

<table>
    <c:forEach var="question" items="<%=Questions.QUESTION_ARRAY%>" varStatus="ctr">
        <tr>
            <td><%=Questions.QUESTION_ARRAY[ctr.index]%></td>
        </tr>
    </c:forEach>
</table>

In [ctr.index], it is saying ctr is unresolved. How do I access it inside the expression?

k_rollo
  • 5,304
  • 16
  • 63
  • 95

2 Answers2

4

The variable ctr created in the page scope. To access page scope variable in JSP expression you can use pageContext implicit object.

<table>
  <% pageContext.setAttribute("questions", Questions.QUESTION_ARRAY); %>
  <c:forEach var="question" items="${questions}" varStatus="ctr">
    <tr>
      <td>
        <%=Questions.QUESTION_ARRAY[((LoopTagStatus)pageContext.getAttribute("ctr")).getIndex()]%>
      </td>
    </tr>
  </c:forEach>
</table>

But it seems ugly if you are using it with JSTL forEach tag. Better to construct the JSP EL expression.

<table>
  <% pageContext.setAttribute("questions", new Questions(){}.QUESTION_ARRAY); %>
  <c:forEach var="question" items="${questions}" varStatus="ctr">
    <tr>
      <td>
        ${questions[ctr.index]} 
      </td>
    </tr>
  </c:forEach>
</table>

Even this expression not necessary if you are using var attribute of the forEach tag which defines the variable referencing the element of the array, also in the page scope. You can access it like

<table>
  <% pageContext.setAttribute("questions", Questions.QUESTION_ARRAY); %>
  <c:forEach var="question" items="${questions}" >
    <tr>
      <td>
        ${question}
      </td>
    </tr>
  </c:forEach>
</table>

Also see this question for other alternatives:
How to reference constants in EL?

Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Despite our previous misunderstanding, I'm upvoting and accepting this answer, because it worked. Particularly, a combination of the 2nd and 3rd code snippets. Thanks for showing how to access the `ctr`, I needed that for something I wasn't able to mention in the original question. – k_rollo Apr 05 '15 at 14:35
  • 1
    I appreciate things that you understand the style of communication on SO, I'm happy that it was helpful to you and may be others. – Roman C Apr 05 '15 at 14:45
  • do you think setting attributes in the JSP itself violates MVC2? – k_rollo Apr 08 '15 at 16:15
  • 1
    not really from the view layer you can communicate to the model. – Roman C Apr 08 '15 at 16:26
2

Why do you need the index if you are already iterating the questions? Why do you use JSTL for the loop and scriptlets for the output?

If I understand your scenario correctly, this should look something like the below:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<bean:define id="questions" type="ph.edu.iacademy.message.Questions" />

<table>
    <c:forEach var="question" items="questions.QUESTION_ARRAY" >
        <tr>
            <td>${question.text}</td>
        </tr>
    </c:forEach>
</table>

If you really do want access to the status then you can do it as:

${ctr.index}
Alan Hay
  • 22,665
  • 4
  • 56
  • 110