Link to home
Start Free TrialLog in
Avatar of paygo
paygo

asked on

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







Avatar of koppcha
koppcha
Flag of United States of America image

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
Avatar of JeromeLiu
JeromeLiu

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>
Oh,sorry
    items="${sessionScope.arrayOfElements}"
should be
    items="${sessionScope.theSessionArrayOfElements}"
Avatar of paygo

ASKER

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


ASKER CERTIFIED SOLUTION
Avatar of koppcha
koppcha
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paygo

ASKER

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






 
Avatar of paygo

ASKER

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