简体   繁体   中英

JSTL - Output not coming as expected

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...

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

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

#{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

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

U r using # instead of $ before name

Let me know if this resolves.

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:

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" %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM