2014-01-27 78 views
1

當使用Spring MVC,addMinutes.jsp & hello.jsp嘗試訪問我的頁面時,出現以下錯誤。我只能訪問localhost無,但如果我瀏覽到一個網頁,我得到這個錯誤:Spring MVC Servlet錯誤

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from ServletContext resource [/WEB-INF/config/servlet-config.xml] is invalid; 
nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'. 

我在網上搜索,我想我xml=""環節都可能是錯的,但我從得到這個直pluralsight.com教程。我是JAVA MVC的新手,所以這對我來說是陌生的。任何幫助,將不勝感激。

這裏是我的servlet-config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> 

     <mvc:annotation-driven/> 
     <context:component-scan base-package="com.pluralsight.controller"></context:component-scan> 

     <!-- 
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> 
      <property name="prefix" value="/WEB-INF/jsp/"/> 
      <property name="suffix" value=".jsp"/>--> 

      <bean class="org.springframework.web.servlet.view.InteralResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/> 
    </beans> 

這裏是我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <servlet> 
     <servlet-name>fitTrackerServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/config/servlet-config.xml</param-value> 
     </init-param> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>fitTrackerServlet</servlet-name> 
     <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 


    <display-name>Archetype Created Web Application</display-name> 
</web-app> 
+1

瞭解XML以及它是什麼意思以及聲明一個名稱空間需要什麼。 –

回答

1

添加mvccontext模式位置到beans節中的應用程序上下文文件

<?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc"   
     xmlns:p="http://www.springframework.org/schema/p" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-2.0.xsd      http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop 
          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"> 
+0

有了這些更改,我得到了三個錯誤:沒有爲上下文找到聲明:組件掃描,p:前綴未綁定到bean,以及在此行找到多個註釋: \t - schema_reference.4:無法讀取模式文檔'http ://www.springframework.org/schema/ \t mvc/spring-mvc-2.0.xsd',因爲1)找不到文檔; 2)文件無法閱讀; 3) \t文檔的根元素不是。 \t - cvc-complex-type.2.4.c:匹配通配符是嚴格的,但是對於元素 \t'mvc:annotation-driven'沒有發現聲明。 –

+0

也缺少上下文名稱空間模式位置 - 請參閱更新 – Reimeus

+0

@ C.Coggins您應該嘗試瞭解手頭的問題,而不是訴諸某些複製/粘貼狂熱。對於每個名稱空間聲明,您需要指定一個位置。你需要看看是否一切都結束。 – Bart

0

這個怎麼樣,基本上我TS缺少一些命名空間的xmlns:P = 「http://www.springframework.org/schema/p」

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <mvc:annotation-driven /> 
    <context:component-scan base-package="com.pluralsight.controller"></context:component-scan> 


    <bean 
     class="org.springframework.web.servlet.view.InteralResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 
</beans> 
+0

謝謝,但我仍然遇到同樣的錯誤。 –

0

下面是Spring配置工作頭模板:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 
xmlns:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
... 
</beans> 

還要確保您正在運行最近版本的Spring> = 3(否則mvcp-名稱空間將不起作用),並且您的類路徑中包含所有必需的Spring JAR。

如果您需要經常編輯Spring配置文件,安裝Spring Tool Suite(STS)會很有幫助,因爲它具有模板以及用於在Spring配置文件中配置必需的XML名稱空間的非常有用的「命名空間」選項卡。

同樣花幾個小時學習XML也不會傷害。