1

Below is the code I have in index.jsp using jstl 1.2.

 <%@ taglib prefix = "c" uri="http://java.sun.com/jstl/core"%>
 <% String[] setName = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
 request.setAttribute("getName", setName);
 %>
 <html>
 <body>
 <table>
 <tr><td>Print</td></tr>
 <c:forEach var="itemName" items="#{getName}" >
 <tr>
 <td>${itemName}</td>
 </tr>
 </c:forEach>
 </table>
 </body>
 </html>

The output I was expecting is as below

 Print
 Hello
 you
 are
 using
 jstl
 in
 jsp

However below is what I am getting

  Print
  #{name}

Please let me know where I am missing

Below is the only jar file I have in WEB-INF/lib folder jstl-1.2.jar

Thanks in advance

Fahim

Note: Adding Java and JSP tag as person who have knowledge of Java and JSP might be knowing JSTL too...

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • **Please note** When I run same code in JSF project, the code was working and showing me desired output. Right now I am doing the same in Ecplise under Web Dynamic Project **Am I missing any jar files??** – Fahim Parkar Jan 12 '12 at 05:50
  • Well well well, I got the problem... I should have used `<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>` instead of `<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>` – Fahim Parkar Jan 12 '12 at 09:31

6 Answers6

0

#{name} is not a valid Java variable reference - looks like you are confusing it with JQuery selector. Anyways try just using items="${name}"

dbrin
  • 15,525
  • 4
  • 56
  • 83
  • when I use items="name" I get o/p as `Print name` – Fahim Parkar Jan 12 '12 at 05:38
  • NOTE: This is simple web application project and not any JSF project. In lib I have general jar file that are needed for web application so I only added jstl1.2.jar file Let me know if I am missing any jar file here... Also When I run the same code earlier in JSF project, it was running and showing me desired output... – Fahim Parkar Jan 12 '12 at 05:42
  • Please note the QUOTES, don't forget em :) – dbrin Jan 12 '12 at 05:54
  • When I use ${name}, below is exception I am getting `org.apache.jasper.JasperException: /index.jsp(9,33) quote symbol expected org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.jav‌​a:40)` Also showing error as **Invalid attribute (items)** Below is what I have `` – Fahim Parkar Jan 12 '12 at 05:54
  • Am I missing any jar files?? Also check the note that I have added in actual post... – Fahim Parkar Jan 12 '12 at 05:55
  • can you do me a favor and rename your String array from name to names and use that for your items. alternatively get rid of the request.setAttribute line i have a feeling its getting in the way. – dbrin Jan 12 '12 at 06:01
  • Still `` saying error as `Invalid attribute(items)` and same exception Could you post the code with changes? – Fahim Parkar Jan 12 '12 at 06:08
  • is your IDE saying that its invalid attribute? If so that might be OK or a sign that you are missing a jar. Try running it. – dbrin Jan 12 '12 at 06:37
  • I run and getting same exception as above.... That means the code is incorrect @DmitryB : Actual code that I posted was running in the project which was created using JSF and this is simple Web Dynamic Project. I have feeling that I am missing some jar file or something is ODD... – Fahim Parkar Jan 12 '12 at 06:46
  • yeah i feel for you. the best thing to do is start a brand new project with just JSPs and no JSF and try out your code. The thing about JSTL and JSF tags is they get executed in different phases and can be incompatible with each other. There are plenty of articles on this topic. The best thing to do with JSF is to move on to Grails :) much easier! good luck. – dbrin Jan 12 '12 at 06:49
0

In JSTL 1.2, you don't want to use #{name} in pure JSP, that's only a JSF artifact. Instead, simply use ${name}.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
0

#{name} is should be like ${name}

oh! might be the jars related to JSTL. check thins link for those jars to include in your project

Community
  • 1
  • 1
Pokuri
  • 3,072
  • 8
  • 31
  • 55
  • When I use `${name}`, below is exception I am getting `org.apache.jasper.JasperException: /index.jsp(9,33) quote symbol expected org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)` – Fahim Parkar Jan 12 '12 at 05:44
  • Also showing error as `Invalid attribute (items)` `` – Fahim Parkar Jan 12 '12 at 05:46
  • Either use ${requestScope.name} or try pageContext.setAttribute() instead of request.setAttribute() – Pokuri Jan 12 '12 at 06:38
  • Actual code that I posted was running in the project which was created using JSF and this is simple Web Dynamic Project. I have feeling that I am missing some jar file or something is ODD... Again, what I have posted was running earlier in project that was created for JSF... – Fahim Parkar Jan 12 '12 at 06:47
  • The taglib declaration is also wrong. Use `<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>` – JB Nizet Jan 12 '12 at 07:22
0

You need to refer items using expression language like ${name}

U r using # instead of $ before name

Let me know if this resolves.

Prathap
  • 1,023
  • 7
  • 19
  • 33
0

Here,

<%@ taglib prefix = "c" uri="http://java.sun.com/jstl/core"%>

You're specifying the wrong JSTL taglib URL. This one is for JSTL 1.0. After JSTL 1.1 it requires a /jsp in the path. See also the JSTL 1.1 tag library documentation.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

As to the of the code (and to reply on all those duplicate answers complaining to use ${} instead), the #{} syntax will only work inside JSP when you're targeting a Servlet 2.5 / 2.1 compatible container with a web.xml conforming Servlet 2.5 spec. Tomcat 6.0 is an example of such a container. The #{} will indeed not work in JSP tags on older containers such as Tomcat 5.5 or older.

For clarity and to avoid confusion among starters, better use ${} all the time in JSP tags. Also better use self-documenting variable names.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% 
    String[] names = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
    request.setAttribute("names", names);
%>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>JSTL demo</title>
    </head>
    <body>
        <table>
            <tr><td>Print</td></tr>
            <c:forEach items="${names}" var="name">
                <tr><td>${name}</td></tr>
            </c:forEach>
        </table>
    </body>
</html>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks BalusC for detailed explanation... I figured out that problem yesterday and added comment for the same... Still thanks for sharing more information on this.... – Fahim Parkar Jan 13 '12 at 04:33
  • I have one question : When I was using `items=${names}`, I was getting error as **Invalid attribute (items)**. I am using Tomcat 6.0 and Servlet version in xml is 2.5. As you mentioned above, **For clarity and to avoid confusion among starters, better use ${} all the time in JSP tags. Also better use self-documenting variable names.**, this was not working for me as I was getting error as mentioned above... Please suggest me what should be used ($ or #)... Thanks in advance – Fahim Parkar Jan 13 '12 at 04:37
  • You need to quote it `items="${names}"`, exactly as the error message tried to tell you (and as shown in my answer) – BalusC Jan 13 '12 at 04:40
  • sorry for confusion.. I was using `items="${names}"`... Still I was getting error as mentioned above... – Fahim Parkar Jan 13 '12 at 04:43
0

Below is the final code I am using and it is running...

Posting so that someone can use it... Might help me tomorrow ;)

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 

 "http://www.w3.org/TR/html4/loose.dtd">

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <% String[] setName = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
 request.setAttribute("getName", setName);
 %>
 <html>
 <body>
 <table>
 <tr><td>Print</td></tr>
 <c:forEach var="itemName" items="#{getName}">
 <tr>
 <td>${itemName}</td>
 </tr>
 </c:forEach>
 </table>
 </body>
 </html>

Learning : I was using <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> instead of <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276