值未显示在jsp中 [英] Values not displayed in jsp
问题描述
我是Spring框架和jstl的新手.我在jsp文件上显示数据时遇到问题.这是我的代码
I am new to spring framework and using jstl. I have a problem in displaying my data on jsp file. Here is my code
控制器
@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
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循环,我都会打印出${place.name}
,${place.rating}
,${place.types}
和${val}
.但是System.print.out()
给了我想要的值.
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.
我做错了什么?
推荐答案
好,我找到了解决方案.也许将来对别人有帮助
Ok I found the solution. maybe it help someone else in future
例如,如果您使用的是DTD定义的旧的JSP 1.2描述符,例如 web.xml
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>
默认情况下,禁用或忽略EL,您必须手动启用它,以便它以"msg"模型输出值存储.
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>
例如,如果您正在使用由w3c模式定义的标准JSP 2.0描述符, web.xml
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>
默认情况下,EL处于启用状态,您应该看到存储在"msg"模型中的值
The EL is enabled by default, and you should see the value stored in the "msg" model
这篇关于值未显示在jsp中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!