带MV的Spring MVC 4:资源无效

时间:2017-10-10 00:13:29

标签: java spring-mvc servlets pom.xml tomcat8

我试图使用Spring MVC创建一个简单的JAVA Web应用程序。我试图在我的资源文件夹中添加CSS和JS文件,分别位于/Webcontents/resources/ and webcontents/JS。因此,我需要在servlet.xm l中进行一些配置,并在此文件中包含spring-webmvc。我的servlet.xml如下所示:



<?xml version="1.0" encoding="UTF-8"?>
<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-4.3.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
 
	<context:component-scan base-package="com.webrtc.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" />
		<mvc:resources mapping="/resources/**" location="/resources/" />
		<mvc:resources mapping="/js/**" location="/js/" />
        <mvc:annotation-driven />
	</bean>
 
</beans>
&#13;
&#13;
&#13;

我的POM.xml如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>WebRTCProject</groupId>
  <artifactId>WebRTCProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <properties>
        <jdk.version>1.8</jdk.version>
        <spring.version>4.3.7.RELEASE</spring.version>
  </properties>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencyManagement>
  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>
  </dependencyManagement>
</project>

一旦我使用Maven构建项目并尝试使用在Eclipse IDE上添加的Tomcat在服务器上运行它,我就会遇到以下错误:

    SEVERE: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
     

配置问题:找不到BeanDefinitionDecorator   元素[资源]       违规资源:ServletContext资源[/WEB-INF/webrtc-servlet.xml]         在org.springframework.beans.factory.parsing.FailFastProblemReporter.fatal(FailFastProblemReporter.java:60)         在org.springframework.beans.factory.parsing.ReaderContext.fatal(ReaderContext.java:68)         在org.springframework.beans.factory.parsing.ReaderContext.fatal(ReaderContext.java:55)         在org.springframework.beans.factory.xml.NamespaceHandlerSupport.findDecoratorForNode(NamespaceHandlerSupport.java:121)         在org.springframework.beans.factory.xml.NamespaceHandlerSupport.decorate(NamespaceHandlerSupport.java:99)         在org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateIfRequired(BeanDefinitionParserDelegate.java:1448)         在org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateBeanDefinitionIfRequired(BeanDefinitionParserDelegate.java:1435)         在org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateBeanDefinitionIfRequired(BeanDefinitionParserDelegate.java:1415)         at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.processBeanDefinition(DefaultBeanDefinitionDocumentReader.java:301)         在org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:190)         在org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:169)         在org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142)         at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94)         在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)         在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)         在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)         在org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)         在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)         在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)         在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)         在org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)         在org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)         at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)         在org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613)         在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)         在org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)         在org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)         在org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)         在org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)         在org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)         在org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)         在javax.servlet.GenericServlet.init(GenericServlet.java:158)         在org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)         在org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)         在org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:989)         在org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4931)         at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5241)         在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)         在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1419)         在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1409)         at java.util.concurrent.FutureTask.run(FutureTask.java:266)         在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)         at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)         在java.lang.Thread.run(Thread.java:748)

我的其他配置如下:

Windows 10

Eclipse IDE

Spring 4.3.7

JAVA SDK 1.8

Tomcat 8.5

我尝试了不同的解决方案来解决版本问题,更改servlet.xml文件等但无法解决问题的问题。

任何人都知道如何解决这个问题?

任何帮助或建议都将受到高度赞赏!如果您需要有关项目的更多详细信息,我将能够为您提供更多详细信息。

1 个答案:

答案 0 :(得分:2)

  

我的servlet.xml如下所示

配置/WEB-INF/webrtc-servlet.xml有错误。要么删除 这个文件或修复那里的错误。

这些元素应该在bean定义之外

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/js/**" location="/js/" />