0

I have the following Code to get an object I stored in an ArrayList.

  <% MyClass myObject =(MyClass)session.getAttribute("session_my_object"); %>
  <%= myObject.getName() %>

I can successfully retrieve the name as shown in the secondline. However, when I attempt to access an arraylist named course_list, which happens to be a static member of myClass I am not getting any values from my jstl foreach iteration(shown in the code below).

 <table>

    <thead>
      <tr>
         <th>Course Code</th>
         <th>Course Description</th>
         <th>Course Type</th>
         <th>Select Course</th>
      </tr>
    </thead>

    <tbody>
     <c:forEach  var="course" items= "${myObject.course_list}" >
      <tr>
         <td><c:out value="${course.code}"></c:out></td>
         <td><c:out value="${course.name}"></c:out></td>
         <td><c:out value="${course.courseType}"></c:out></td>
     </tr>
  </c:forEach>

</tbody>
</table>

Kindly help me find a solution to the problem.

  • Why are you using `${myObject}` instead of `${session_my_object}`? Or, why did you use the attribute name `session_my_object` instead of the intented `myObject`? – BalusC Aug 28 '15 at 14:13
  • I figured I had to "pull out" the object from the session object and then use it. Does the above code not point myObject and session_my_object not point to the same object ? – Khayelihle Tshuma Aug 28 '15 at 14:16
  • The problem is probably that you are trying to access **static** member. See http://stackoverflow.com/questions/3732608/how-to-reference-constants-in-el for details. – Jozef Chocholacek Aug 28 '15 at 14:17
  • ^ I'd say that's a valid assumption. You're not exposing the variable you create in the scriptlet into a scope accessible by JSTL. You'd need to add it into the pageContext object in your scriptlet with the name "myObject", via `pageContext.setProperty("myObject", myObject);`. **EDIT** Actually, you should pull out the static variable that you want, and stuff that into the page context instead, a la `pageContext.setProperty("courseList", MyClass.course_list)` or something. – Shotgun Ninja Aug 28 '15 at 14:20
  • Scriptlets are not "static". They just don't share the same variable scope. See also abovelinked dupe and http://stackoverflow.com/questions/14007689/use-el-xy-directly-in-scriptlet-xy/ for an explanation how EL works under the covers. – BalusC Aug 28 '15 at 14:23
  • @BalusC I think he meant that the `course_list` variable is a static member of `MyClass`, not a static member of `myObject`. If that's the case, then accessing it through the object instance in JSTL won't work, even if the instance is made accessible to JSTL, right? – Shotgun Ninja Aug 28 '15 at 14:31
  • If that's not the case, then I retract my statement. – Shotgun Ninja Aug 28 '15 at 14:32
  • 1
    @ShotGun Ninja Thanks. That is right. Have edited accordingly. – Khayelihle Tshuma Aug 28 '15 at 14:53
  • 1
    Thank you for the help , links and all. The variable was static and I had forgotten to add the import tag in my jsp. – Khayelihle Tshuma Aug 28 '15 at 17:36

0 Answers0