JSTL HOW TO read through / iterate through a multi dimensional array - session object

Hello Everyone,

I'm looking for information on how to ' read through ' or ' iterate through ' a session / request object
- that is a multi-dimentional array.

->here's the object example:

public void class  Elements
{
String sFirstElement;
Int iSecondElement;
long lThirdElement;
}

->here's the array of elements:

Elements [] arrayOfElements = new  Elements[10];
~~~~~~ 'arrayOfElements'  is  loaded correctly here

-> arrayOfElements is placed in a session variable:

session.setAttribute("theSessionArrayOfElements", arrayOfElements);

->> Here's where I get stuck (from a JSP Page)

<c:forEach ????="${sessiontScope.??????????}" var="????">
<c:out value="${?????.????}" escapeXml="false"/>
</c:forEach>

---------------Thanks for the help







paygoAsked:
Who is Participating?
 
koppchaConnect With a Mentor Commented:
1>
>B - The controller (a different class ) creates the array
>and places the filled array in request scope:

>Elements [] arrayOfElements = new  Elements[10];
>session.setAttribute("theSessionArrayOfElements", arrayOfElements);
         This is session scope.

2>Did you use <jsp:useBean id="element" class="full path of the class" scope="Session" /> in your JSP?

3>In the bean do you have getmethods that return the values?

0
 
koppchaCommented:
You have to use the corresponding bean path Just giving assuming the path to be 'com.koppcha.MyBean'

In JSP
<jsp:useBean id='MyBean' class='com.koppcha.MyBean' scope="session"/>

<c:forEach var='name' items='${sessionScope.MyBean}'>
FirstElement is: '<c:out value='${name.sFirstElement}' />
SecondElement is: <c:out value='${name.iSecondElement}' />
ThirdElement is: <c:out value='${name.lThirdElement}' />
</c:forEach>

Hope this helps .You can check this link aswell
http://www.phptr.com/articles/article.asp?p=30946&seqNum=4
0
 
JeromeLiuCommented:
You can try below in jsp:

<%@ page import="com.Elements"  %>
<%
Elements [] arrayOfElements = new  Elements[10];
session.setAttribute("theSessionArrayOfElements", arrayOfElements);
//You can set it to session in other page
%>

<c:forEach var="elemnet" items="${sessionScope.arrayOfElements}">
        <c:out value="${element.sFirstElement}" escapeXml="false"/>
</c:forEach>
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
JeromeLiuCommented:
Oh,sorry
    items="${sessionScope.arrayOfElements}"
should be
    items="${sessionScope.theSessionArrayOfElements}"
0
 
paygoAuthor Commented:
Ok - thanks.

Here's the trick.  I'm using struts

A - the elements are defined in on class

public void class  Elements
{
String sFirstElement;
Int iSecondElement;
long lThirdElement;
}

B - The controller (a different class ) creates the array
and places the filled array in request scope:

Elements [] arrayOfElements = new  Elements[10];
session.setAttribute("theSessionArrayOfElements", arrayOfElements);


C- Problem is when I use

<c:forEach var="element" items="${sessionScope.theSessionArrayOfElements}">
<c:out value="${element.sFirstElement}" escapeXml="false"/>
</c:forEach>

>>>>> The message is element 'sFirstElement' can not be found.

--thanks - I think this is pretty close toa solution


0
 
paygoAuthor Commented:
HI  koppcha

I am trying to avoid using beans, getters and setter etc. I just want to use a generic class to hold the elements.

I'm thinking this - I can pass a value into session object from any class
i.e.   session.SetAttibute("nowInSession" anyValue);

I can also retrieve that session variable from any JSP Page.
i.e. <c:out sessionScope=${"nowInSession"} />

Now with and Array - I understand you need to use the:
<c:forEach var="element" items="${requestScope.theSessionArrayOfElements}">
<c:out value="${element.sFirstElement}" escapeXml="false"/>
</c:forEach>

- But the  element 'sFirstElement' can not be found message appears.

thanks






 
0
 
paygoAuthor Commented:
Here's the answer -

public void class  Elements
{
String sFirstElement;
Int iSecondElement;
long lThirdElement;

<Add this>
public String getSFirstElement()
               {
        return sFirstElement;
      }
<end>
}

point go to koppcha for the tip off thanks - don't need to use a bean though



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.