1
我一直在試用Spring框架,並嘗試在jsp文件中打印java類屬性時,我遇到了一些麻煩。在嘗試將日期打印到我的頁面時,我什麼都沒有收到。我知道變量中有一個值,因爲我可以在控制檯中看到它,但在頁面上沒有任何值。這裏是我的index.jsp其中值應顯示:Spring:在jsp頁面中打印變量
<!-- contains taglibraries -->
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello :: Spring Application</title>
</head>
<body>
<h1>Hello - Spring Application</h1>
<h2>Testing tags</h2>
<p> <c:out value="If you see this then C-tag works."/> </p>
<h2>Testing values brought from controller. One should see a date after "now".</h2>
<p>Greetings, it is now <c:out value="${now}"/></p>
</body>
</html>
這裏是我的indexController.java:
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class IndexController implements Controller{
protected Log logger = LogFactory.getLog(getClass());
@Override
public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
String now = (new Date()).toString();
logger.info("Returning hello view and " + now);
return new ModelAndView("WEB-INF/jsp/index.jsp" , "now" , now);
}
}
有人能指出它是什麼,我做錯了什麼?哦,我只想澄清了:「include.jsp」包含了我所有taglibraries所以缺少標記庫是這樣的:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
是不是一個問題,因爲 的index.jsp的作品第一個C-標籤。問題是第二個c標籤的變量「now」拒絕顯示。