aspose file tools*
The moose likes JSP and the fly likes Not able to execute code for forEach loop in EL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Not able to execute code for forEach loop in EL" Watch "Not able to execute code for forEach loop in EL" New topic
Author

Not able to execute code for forEach loop in EL

aatish pandya
Greenhorn

Joined: Apr 20, 2011
Posts: 21
I am trying to execute code for forEach Loop in EL, but I am not getting any output.

My index.jsp is as below -

<body>
<%
LinkedList errorMsgs = new LinkedList();
errorMsgs.add("Error 1");
errorMsgs.add("Error 2");
%>
<c:if test="${not empty errorMsgs}">


Please correct the following errors:


    <c:forEach var="message" items="${errorMsgs}">
  • ${message}

  • </c:forEach>



</c:if>
</body>

but, output is -

Please correct the following errors:



That is output is not displayed.

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

If you are using JSTL/EL etc... then why Java code inside the JSP? They should belong in your model layer. Can you tell us what configuration you have setup here (application server, JSTL version)? And perhaps the complete Error message?
Please read TellTheDetails.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
aatish pandya
Greenhorn

Joined: Apr 20, 2011
Posts: 21
Vijitha,

I am just trying to see whether message in the LinkedList is getting displayed or not, hence java code, I will later put it in bean file. Server is GlassFish 3.1 and using Netbeans 7.0.1 and the output is the same which I have already mentioned. Again repeating -

Please correct the following errors:







Please note the following code is working -

<jsp:useBean class="pack1.NewClass" id="v1" scope="request"></jsp:useBean>
${v1.msg}

Output-
Hello
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Ok, then you have to set the list in some scope before use it inside JSTL:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

Exactly. The EL and JSTL do not "see" scripting variables, only scoped variables. Even for testing, mixing scriptlets with JSTL/EL can be a problem -- they're like oil and water.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
aatish pandya
Greenhorn

Joined: Apr 20, 2011
Posts: 21
if we are setting using pageContext.setAttribute then where to get it! Please explain the exact usage in the code

[code]
<body>
<%
LinkedList errorMsgs = new LinkedList();
errorMsgs.add("Error 1");
errorMsgs.add("Error 2");
%>
<c:if test="${not empty errorMsgs}">


Please correct the following errors:


    <c:forEach var="message" items="${errorMsgs}">
  • ${message}

  • </c:forEach>



</c:if>
[/code]

Output -

Please correct the following errors:

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

if we are setting using pageContext.setAttribute then where to get it!

To get the value you already have the code. But its about setting it. Just add the line I posted after you set the values of the list inside the scriptlet.

And please use code tags when posting code in the forums as already pointed out.
aatish pandya
Greenhorn

Joined: Apr 20, 2011
Posts: 21
Dear Vijitha,

Look at the modified code as per your suggestion -

[code=java]
<%
LinkedList errorMsgs = new LinkedList();
errorMsgs.add("Error 1");
errorMsgs.add("Error 2");
pageContext.setAttribute("list", errorMsgs);
%>
<c:if test="${not empty errorMsgs}">


Please correct the following errors:


    <c:forEach var="message" items="${errorMsgs}">
  • ${message}

  • </c:forEach>



</c:if>
[/code]

Result - No Effect - same Blank output. I would suggest, you please run the and test code on your system, this will help you know the real problem!
I am using NetBeans 7.0.1 to run the code!
aatish pandya
Greenhorn

Joined: Apr 20, 2011
Posts: 21
Dear Vijitha,

Look at the modified code as per your suggestion -



Result - No Effect - same Blank output. I would suggest, you please run the and test code on your system, this will help you know the real problem!
I am using NetBeans 7.0.1 to run the code!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

You are creating a scope variable named list and then never referencing it.

Once again, as I already said much earlier in this topic, the EL cannot see scripting variables. Period.
aatish pandya
Greenhorn

Joined: Apr 20, 2011
Posts: 21
Dear Bear Bibeault and Vijitha Kumar or Guest,

Actually, I want to test a forEach loop for EL, can you give an example or make corrections in the given example to explain it!

Sample code -



The Data can be in a Bean File in the form of an Array/List/LinkedList or Object as may be the case!

Eg I. <Bean_File_Array.java>


Eg.II <Bean_File_Linked_List.java>


Eg.III <Bean_File_Object.java>


I have explained you all the 3 types - (Array,LinkedList, Object and LinkedList for names of type String and phone of type int).

How will you retrieve these from EL using forEach Loop for each of the 3 cases?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

There are different ways you can access depending on the various data structures being used.
You already have done this for the List data structure. And the *square bracket style* ([]) can be used with Map, bean, List, Array types.

Here the "x" can be the index (for array types) or a key (for Map types) etc... Note that this object should be placed in one of EL/JSTL accessible scopes as mentioned already.

I suggest you read the EL, JSTL in the J2EE tutorial here for more examples.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Bear Bibeault wrote:The EL and JSTL do not "see" scripting variables, only scoped variables. Even for testing, mixing scriptlets with JSTL/EL can be a problem -- they're like oil and water.

Quoted for a wisdom!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to execute code for forEach loop in EL