• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to execute code for forEach loop in EL

 
aatish pandya
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
aatish pandya
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, then you have to set the list in some scope before use it inside JSTL:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
aatish pandya
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic