首先,我想提前道歉,如果这个问题已经在某个地方得到解答,但我找不到合理的解决方案,我也会说这主要是因为我对Java Spring Framework很新。我自己设法部署了一个简单的应用程序,并添加了一些css / js文件。但是,特别是在index.jsp文件中,我总是收到:
将这些行添加到模板时“ERROR org.springframework.web.servlet.tags.UrlTag - 否 发现WebApplicationContext:不在DispatcherServlet请求中而不是 ContextLoaderListener注册了吗? java.lang.IllegalStateException:没有 发现WebApplicationContext:不在DispatcherServlet请求中而不是 ContextLoaderListener注册?“
:
<spring:url value="/resources/example1/css/main.css" var="mainCss" />
<link href="${mainCss}" rel="stylesheet" />
这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>CrunchifySpringMVCTutorial</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>example1</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example1</servlet-name>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
这是我的example1-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.example1.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/example1"
cache-period="31556926"/>
<mvc:annotation-driven />
</beans>
这是我的index.jsp
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html>
<head>
<spring:url value="/resources/example1/css/main.css" var="mainCss" />
<link href="${mainCss}" rel="stylesheet" />
<%-- <spring:url value="/resources/example1/css/main.css" var="mainCss" /> --%>
<%-- <spring:url value="/resources/example1/js/main.js" var="mainJs" /> --%>
<%-- <spring:url value="/resources/example1/js/jquery-2.2.3.min.js" var="jqueryJs" /> --%>
<%-- <link href="${mainCss}" rel="stylesheet" /> --%>
<%-- <script src="${jqueryJs}"></script> --%>
<%-- <script src="${mainJs}"></script> --%>
</head>
<body>
</body>
</html>
另外,这是我的文件结构:
/WebContent
/META-INF
/resources
/example1
/css
-main.css
/js
-main.js
-jquery-2.2.3.min.js
/WEB-INF
/jsp
-welcome.jsp
-example1-servlet.xml
-web.xml
-index.jsp
使用其他模板(welcome.jsp),添加相同的资源文件时一切运行正常。任何想法?
感谢。
答案 0 :(得分:0)
您需要在 web.xml 中添加侦听器 org.springframework.web.context.ContextLoaderListener :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>CrunchifySpringMVCTutorial</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>example1</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example1</servlet-name>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>