我從頭開始創建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問題中搜索我發現了像下面這樣的威脅,但還沒有解決我的錯誤。
- Cannot find the declaration of element 'beans' in internet offline mode
- Spring Security beginner's question. Build failed
因此,我推斷,可能是在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;
}
}
? – soulcheck 2012-08-08 16:15:50
當我從[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
這裏你去:http ://search.maven.org/remotecontent文件路徑=組織/ springframework的/彈簧webmvc/3.1.2.RELEASE /彈簧webmvc-3.1.2.RELEASE.jar。 – soulcheck 2012-08-08 16:21:49