我一直在尝试Spring框架,在尝试在jsp文件中打印java-class属性时,我遇到了一些麻烦。在尝试将日期打印到我的页面时,我什么都没得到。我知道变量中有一个值,因为我可以在控制台中看到它,但在页面上没有任何内容。这是我的index.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,所以缺少这样的taglibrary:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
不是问题 index.jsp上的第一个c-tag有效。问题是第二个带有变量“now”的c-tag拒绝显示。
答案 0 :(得分:2)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
不包括你的jsp