值未显示在jsp中


问题内容

我是Spring框架和jstl的新手。我在jsp文件上显示数据时遇到问题。这是我的代码

控制者

@RequestMapping(method = RequestMethod.GET, value = "/test")
public ModelAndView getTest(HttpServletRequest request) {
    List<Place> places = PlacesService.search(types, 48.137048, 11.57538599, 10000);
    for(Place place:places){

        System.out.println("Name: " + place.getName());
        System.out.println("Rating: " + place.getRating());
        System.out.println("Categories: " + place.getTypes());
        counter++;
    }
    ModelAndView model = new ModelAndView("test");
    model.addObject("places", places);
    return model; 
}

在我的test.jsp中

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
 Hello World!
 <c:forEach items="${places}" var="place">
   <c:out value="${place.name}"/>   
   <c:out value="${place.rating}"/>
   <c:out value="${place.types}"/>
</c:forEach>
<c:forEach begin="6" end="15" var="val">
    <c:out value="${val}"/>
</c:forEach>

</body>
</html>

对于这两个for循环中,我得到
${place.name}${place.rating}${place.types}${val}打印。但是System.print.out()给了我想要的值。

我做错了什么?


问题答案:

好的,我找到了解决方案。也许将来会帮助别人

如果使用的是DTD定义的旧的JSP 1.2描述符,例如web.xml

  <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 //...
</web-app>

EL默认情况下处于禁用或忽略状态,您必须手动启用它,以便它将输出以“ msg”模型存储的值。

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <html>
 <head>
    <%@ page isELIgnored="false" %>
 </head>
 <body>
       ${msg}
 </body>
 </html>

如果使用的是由w3c模式定义的标准JSP 2.0描述符,例如web.xml

<web-app id="WebApp_ID" version="2.4" 
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 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
    //...
</web-app>

EL默认情况下处于启用状态,您应该看到存储在“ msg”模型中的值