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

发布于 2024-11-09 11:07:30 字数 3247 浏览 0 评论 0原文

当我将此 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)

When I add this tld and custom tag to my jsp Spring throws an error on startup. The page 'index.jsp' that is loading is configured as the default first page for the application. Please explain why this is happening? Everything works as expected before the tag is added to the page.

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>

error

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2024-11-16 11:07:30

因为 spring 标签需要 spring 应用程序。为此,您需要在 web.xml 中将 ContextLoaderListener 定义为 .. (我怀疑它映射了 DispatcherServlet 也应该没问题,尽管消息中没有指出)

Because spring tags expect a spring application. To have such, you need to define a ContextLoaderListener as <listener>..</listener> in web.xml (I suspect thta mapping a DispatcherServlet should also be fine, although not indicated by the message)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文