2012-08-08 109 views
1

我從頭開始創建HelloWorld web/spring應用程序。我跟着this tutorial爲了學習如何使用mvc模式。因此,後完成了所有步驟,並開始運行我的控制檯收到此錯誤的應用程序:嚴重:上下文初始化失敗

墓:上下文初始化失敗 org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: 行11 XML文檔中的ServletContext資源 [/WEB-INF/dispatcher-servlet.xml]無效;嵌套的異常是 org.xml.sax.SAXParseException; lineNumber:11; columnNumber:100; cvc-complex-type.2.4.c

在SO問題中搜索我發現了像下面這樣的威脅,但還沒有解決我的錯誤。

因此,我推斷,可能是在WEB-INF文件夾中的調度員servlet.xml中。這個XML的樣子:

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


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

     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property name="interceptors"> 
      <list> 
       <ref local="localeChangeInterceptor" /> 
      </list> 
     </property> 

     <property name="urlMap"> 
      <map> 
       <entry key="/hello.html"> 
        <ref bean="helloController" /> 
       </entry> 
      </map> 
     </property> 

    </bean> 

    <bean id="helloController" class="controllers.HelloController"> 
    </bean> 

    <bean id="localeChangeInterceptor" 
     class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 

     <property name="paramName" value="hl" /> 

    </bean> 

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> 
    </bean> 

</beans> 

最後,這些是我的罐子包括:

commons-logging-1.1.1.jar 

org.springframework.aop-3.1.2.RELEASE.jar 
org.springframework.asm-3.1.2.RELEASE.jar 
org.springframework.aspects-3.1.2.RELEASE.jar 
org.springframework.beans-3.1.2.RELEASE.jar 
org.springframework.context-3.1.2.RELEASE.jar 
org.springframework.context.support-3.1.2.RELEASE.jar 
org.springframework.core-3.1.2.RELEASE.jar 
org.springframework.expression-3.1.2.RELEASE.jar 
org.springframework.instrument-3.1.2.RELEASE.jar 
org.springframework.instrument.tomcat-3.1.2.RELEASE.jar 
org.springframework.jdbc-3.1.2.RELEASE.jar 
org.springframework.jms-3.1.2.RELEASE.jar 
org.springframework.orm-3.1.2.RELEASE.jar 
org.springframework.oxm-3.1.2.RELEASE.jar 
org.springframework.spring-library-3.1.2.RELEASE.libd 
org.springframework.test-3.1.2.RELEASE.jar 
org.springframework.transaction-3.1.2.RELEASE.jar 
org.springframework.web-3.1.2.RELEASE.jar 
org.springframework.web.portlet-3.1.2.RELEASE.jar 
org.springframework.web.servlet-3.1.2.RELEASE.jar 
org.springframework.web.struts-3.1.2.RELEASE.jar 

spring-webmvc-3.0.5.RELEASE.jar 

在此先感謝

編輯1:

使@Biju Kunjummen的更改後,看起來像現在的問題是,在bean聲明中有一個comflic:

Grave: Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMapping' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Cannot resolve reference to bean 'helloController' while setting bean property 'urlMap' with key [TypedStringValue: value [/hello.html], target type [null]]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [controllers.HelloController]: Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems: 
    The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files 
    Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor 
    String cannot be resolved to a type 

我檢查了控制器和看起來很好爲src/controllers DIR:

package controllers; 

import java.io.IOException; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.Controller; 

public class HelloController implements Controller { 

    public ModelAndView handleRequest(HttpServletRequest request, 
       HttpServletResponse response) throws ServletException, IOException { 

       String Mess = "Hello World!"; 

       ModelAndView modelAndView = new ModelAndView("hello"); 
       modelAndView.addObject("message", Mess); 

       return modelAndView; 
       } 

} 
+0

? – soulcheck 2012-08-08 16:15:50

+0

當我從[Spring community](http://www.springsource.org/download/community)下載Spring框架時,它並沒有進入,所以我用另一個鏈接下載它:[spring-webmvc-3.0.5 .RELEASE](http://search.maven.org/remotecontent?filepath=org/springframework/spring-webmvc/3.0.5.RELEASE/spring-webmvc-3.0.5.RELEASE.jar) – manix 2012-08-08 16:19:38

+1

這裏你去:http ://search.maven.org/remotecontent文件路徑=組織/ springframework的/彈簧webmvc/3.1.2.RELEASE /彈簧webmvc-3.1.2.RELEASE.jar。 – soulcheck 2012-08-08 16:21:49

回答

2

你有http://www.springframework.org/schema/mvc爲默認,但您的bean定義屬於http://www.springframework.org/schema/beans命名空間,這應該是這個問題。

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


<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix"> 
     <beans:value>/WEB-INF/jsp/</beans:value> 
    </beans:property> 

.....

或者使豆命名空間中的默認這種方式:你爲什麼不使用彈簧webmvc相同版本的彈簧罐的其餘

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


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

     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 
+0

現在錯誤看起來不同了。我已更新帖子以詳細說明結果,但我同意你的看法,錯誤在'dispatcher-servlet.xml'文件 – manix 2012-08-08 16:55:12

+0

哦,我找到了一個可能有用的鏈接:http://forum.springsource。組織/ showthread.php?69823燦叔負荷-A-豆從的ApplicationContext。所以現在錯誤被放置在我的控制器上。我認爲現在是接受答案的時候了。非常感謝你和其他人。 – manix 2012-08-08 17:05:15

相關問題