为什么Spring只在添加tld后才会抱怨WebApplicationContext

时间:2011-05-23 22:47:00

标签: apache spring tags

当我将这个tld和自定义标记添加到我的jsp时Spring会在启动时抛出错误。正在加载的页面“index.jsp”被配置为应用程序的默认第一页。请解释为什么会这样?在将标记添加到页面之前,所有内容都按预期工作。

head.tag

<%@ attribute name="title" required="true" description="Page's title" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="../mytags.tld" prefix="pbl" %>
<head>
<title>
<spring:message code="${title}" text="${title}"></spring:message>
</title>
<script type="text/javascript" src="resources/scripts/global.js"></script>
<script type="text/javascript" src="resources/scripts/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="resources/scripts/jquery-ui-1.8.13.custom.min.js"></script>
<link rel="stylesheet" type="text/css" href="resources/styles/global.css" media="screen" />
<link rel="stylesheet" type="text/css" href="resources/styles/dark-hive/jquery-ui-1.8.13.custom.css" media="screen" />
<jsp:doBody />
</head>

的index.jsp

<%@ taglib uri="/WEB-INF/mytags.tld" prefix="pbl" %>
<html>
    <pbl:head title="hello">
    <link rel="stylesheet" type="text/css" href="resources/styles/global.css" media="screen" />
    </pbl:head>
<body>
    <a href="home">Say Hello</a>
</body>
</html>

错误

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 3

1: <%@ taglib uri="/WEB-INF/mytags.tld" prefix="pbl" %>
2: <html>
3: <pbl:head title="hello">
4:  <link rel="stylesheet" type="text/css" href="resources/styles/global.css" media="screen" />
5: </pbl:head>
6: <body>

root cause 

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:84)
org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestContextUtils.java:81)
org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:219)
org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:74)
org.springframework.web.servlet.support.JspAwareRequestContext.<init>(JspAwareRequestContext.java:48)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:76)
org.apache.jsp.tag.web.head_tag._jspx_meth_spring_005fmessage_005f0(head_tag.java:132)
org.apache.jsp.tag.web.head_tag.doTag(head_tag.java:85)
org.apache.jsp.index_jsp._jspx_meth_pbl_005fhead_005f0(index_jsp.java:93)
org.apache.jsp.index_jsp._jspService(index_jsp.java:63)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

1 个答案:

答案 0 :(得分:2)

因为弹簧标签需要弹簧应用。要有这样的,你需要在web.xml中定义ContextLoaderListener<listener>..</listener>(我怀疑映射DispatcherServlet也应该没问题,尽管消息没有说明)