• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1756
  • Last Modified:

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







0
paygo
Asked:
paygo
  • 3
  • 2
  • 2
1 Solution
 
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
 
JeromeLiuCommented:
Oh,sorry
    items="${sessionScope.arrayOfElements}"
should be
    items="${sessionScope.theSessionArrayOfElements}"
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
koppchaCommented:
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
 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now