无法使用 spring 迭代 jstl 中的字符串列表

Unable to iterate of a list of strings in jstl with spring

我正在努力学习 Spring/mvc 框架。我创建了一个基本示例,我在其中将一个简单的字符串打印到屏幕上,这很好,我现在想遍历字符串列表,但什么也没有出现。似乎我的 foreach 正在查看整个集合而不是其中的元素。我在循环中打印了一个额外的字符,但只显示了一个字符,但有 4 个项目。

不使用 foreach 直接在屏幕上打印 ti 以 [a,b,c,d] 的方式显示内容。

我不确定我做错了什么,感谢您的帮助。

控制器:

    @Controller
public class HelloController {

    //the request mapping simply says, what url am i tied to?
    @RequestMapping(value = "/greeting") //defines the url and the method 
it is tied to
    public String sayHello(Model model){ //model is a key-value pair.

        List<String> stringList = new ArrayList<String>();
        stringList.add("A");
        stringList.add("B");
        stringList.add("C");
        stringList.add("D");

        model.addAttribute("greeting", "Hello, World"); //greeting is key, value hello world
        //the jsp page will reference back to 'greeting' as above.
        model.addAttribute("stringList", stringList);
        model.addAttribute("stringlist2", "stringlist2");

        return "hello";//this ties us to the jsp pages
    }
}

JSP:

<h1>
    ${greeting}
    ${stringList}
    ${stringlist2}
    <%--this maps to the model attribute greeting in the controller.--%>
</h1>

<c:forEach items="${stringList}" var="elt">
    <div>:<c:out value="${elt}"/></div>
</c:forEach>

页面上的结果:

Hello, World [A, B, C, D] stringlist2
: //this is the extra char I'm printing in the foreach.

view source shows: <c:foreach items="[A, B, C, D]" var="elt"> <div>:<c:out value=""></c:out></div> </c:foreach>

您的服务器无法查找、解析和执行 JSTL 标记。

如果您忘记在 JSP 顶部声明 c 名称空间前缀,就会发生这种情况:

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

如果还是不行,或者你得到一个异常提示“", then that can happen if you you're using the jurassic JSTL 1.0 (which is really unexpected these days), or that you forgot to install JSTL too. For installation instructions and other troubleshooting hints, head over to our JSTL wiki page.