1

I am creating a HelloWorld web/spring application from scratch. I have followed this tutorial in order to learn how to use the mvc pattern. So, after finished all steps and starts to run the application I received this error in console:

Grave: Context initialization failed org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 100; cvc-complex-type.2.4.c

Searching around SO question I found some threat like below but have not resolved my error.

So, I deduced that could be the dispatcher-servlet.xml at WEB-INF folder. This xml looks like:

<?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>

Finally these are my included jars:

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

Thanks in advance

EDIT 1:

After make the changes of @Biju Kunjummen, looks like now the problem is that there is a comflic in bean declaration:

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

I have checked the controller and looks fine into 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;
              }

}
Community
  • 1
  • 1
manix
  • 14,537
  • 11
  • 70
  • 107
  • why aren't you using the same version of spring-webmvc as the rest of spring jars? – soulcheck Aug 08 '12 at 16:15
  • When I download the Spring Framework from [Spring community](http://www.springsource.org/download/community), it does not come inside, so I download it with another link: [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 Aug 08 '12 at 16:19
  • 1
    here you go: http://search.maven.org/remotecontent?filepath=org/springframework/spring-webmvc/3.1.2.RELEASE/spring-webmvc-3.1.2.RELEASE.jar. – soulcheck Aug 08 '12 at 16:21
  • thank you, this fix some error at line 3 – manix Aug 08 '12 at 16:32

1 Answers1

2

You have the http://www.springframework.org/schema/mvc as the default, but your bean definitions belong to the http://www.springframework.org/schema/beans namespace, that should be the issue.

<?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>

.....

Or make beans namespace the default this way:

<?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>
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • Now the error looks different. I have updated the post to detail the result, but I agree with you, the error is in `dispatcher-servlet.xml` file – manix Aug 08 '12 at 16:55
  • Ohh, I found a link that could be useful: http://forum.springsource.org/showthread.php?69823-Can-t-load-a-bean-from-applicationContext. So now the error is placed at my controller. I think that is time to accept the answer. Thank you so much and the rest of people too. – manix Aug 08 '12 at 17:05