简体   繁体   中英

<c:foreach> doesn't seem to work

I've seen all similar questions but I didn't find any working answer to my issue.

Simple jsp file:

   <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <% String[] names = {"Jhn", "Kate", "Larry"};
            pageContext.setAttribute("myNames", names);
    %>
    <html>
    <body>

    <c:forEach var="tempName" items="${myNames}">
        <p>${tempName}</p>
    </c:forEach>
    </body>
    </html>

Maven dependencies:

 <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

The output of JSP file is

${tempName}

Though, I expect

Jhn 
Kate 
Larry

I feel like I am missing something but can't find out what's wrong.

Edit: I'd like to mention that if I create a dynamic web app and do the same things(except maven dependencies, obviously) - it works perfectly but as soon as I create maven project - it stops to work

Well, that is absolutely ridiculous. It began to work and I changed nothing to make it work.

The only things I've done are:

Added to web.xml into web-app tag:

xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd"
version="2.4"

then deleted it

After I changed

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

to

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

and then back to

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

Technically, nothing changed. But somehow it began to work. I have no idea what happened.

Though, in other similar projects it still doesn't work.

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