2017-10-10 103 views
1

我試圖使用Spring MVC製作一個簡單的JAVA Web應用程序。我試圖將CSS和JS文件添加到我的資源文件夾中,分別位於/Webcontents/resources/ and webcontents/JS。因此,我需要在我的servlet.xm l中進行一些配置,並在該文件中包含spring-webmvc。我servlet.xml如下所示:MVC 4與mvc:資源不工作

<?xml version="1.0" encoding="UTF-8"?> 
 
<beans xmlns="http://www.springframework.org/schema/beans" 
 
\t xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 
 
\t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
\t 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"> 
 
    
 
\t <context:component-scan base-package="com.webrtc.controller" /> 
 

 
\t <bean id="viewResolver" 
 
\t \t class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
 
\t \t <property name="viewClass" 
 
\t \t \t value="org.springframework.web.servlet.view.JstlView" /> 
 
\t \t <property name="prefix" value="/WEB-INF/jsp/" /> 
 
\t \t <property name="suffix" value=".jsp" /> 
 
\t \t <mvc:resources mapping="/resources/**" location="/resources/" /> 
 
\t \t <mvc:resources mapping="/js/**" location="/js/" /> 
 
     <mvc:annotation-driven /> 
 
\t </bean> 
 
    
 
</beans>

我的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構建項目,並嘗試使用Tomcat服務器運行這是在Eclipse IDE上添加的,我收到以下錯誤:

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

配置問題:無法找到BeanDefinitionDecorator爲 元素[資源] 犯規資源:在org.springframework.beans.factory.parsing.FailFastProblemReporter.fatal ServletContext的資源[/WEB-INF/webrtc-servlet.xml] ( FailFastProblemReporter.java:60) at org.springframework.beans.factory.parsing.ReaderContext.fatal(ReaderContext.java:68) at 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.j AVA: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) 在org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.processBeanDefinition(DefaultBeanDefinitionDocumentReader.java:301) 在org.springframework.beans。 factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:190) at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBea nDefinitions(DefaultBeanDefinitionDocumentReader.java:169) 在org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142) 在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) 在有機springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefini行動(XmlBeanDefinitionReader.java:304) 在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) 在org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java: 217) at 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) 在org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129) 在org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613) 在org.springframework.context.support.AbstractApplicationContext。刷新(AbstractApplicationContext.java:514) at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668) at org .springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682) at 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) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java :1099) at org.apa che.catalina.core.StandardWrapper.load(StandardWrapper.java:989) at 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) at 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)

我的其他confiurations情況如下:

的Windows 10

的Eclipse IDE

春4.3.7

JAVA SDK 1.8

Tomcat的8.5

我嘗試了不同的解決方案來解決版本問題,更改servlet.xml文件等問題,但無法解決問題。

任何人都知道我該如何解決這個問題?

任何幫助或建議將不勝感激!如果您需要更多關於項目的細節,我將能夠爲您提供更多細節。

回答

1

我的servlet。xml如下所示

配置/WEB-INF/webrtc-servlet.xml有錯誤。刪除 此文件或修復錯誤。

這些元素應該是bean定義

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

嗨之外!你的意思是我發佈的文件中有一些語法錯誤嗎?或者一些配置錯誤? – ikramulhaq

+0

這是資源元素的配置錯誤。 –

+0

只需將的位置置於定義之外即可解決問題!非常感謝你 – ikramulhaq