Values not displayed in jsp

2020-07-27 06:26发布

问题:

I am new to spring framework and using jstl. I have a problem in displaying my data on jsp file. Here is my code

Controller

@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; 
}

In my 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 both the for loops I get ${place.name}, ${place.rating}, ${place.types} and ${val} printed. However System.print.out() gives me the desired values.

What I am doing wrong?

回答1:

Ok I found the solution. maybe it help someone else in future

If you are using the old JSP 1.2 descriptor, defined by DTD ,for example 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>

The EL is disabled or ignored by default, you have to enable it manually, so that it will outputs the value store in the “msg” model.

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

If you are using the standard JSP 2.0 descriptor, defined by w3c schema ,for example 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>

The EL is enabled by default, and you should see the value stored in the “msg” model