Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC set up 404 problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC set up 404 problem

    Hi,

    I have a problem when using Spring MVC in my project. I have one ExampleController class which doesn't want to load into the Spring container. My test URL is: localhost:8180/springTest.do. (I've tried the @Controller annotation and the bean definition as shown below):

    web.xml:
    HTML Code:
    <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    My dispatcher-servlet.xml:

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
            
        <import resource="/application.xml"/>
    	
    	<bean name="/springTest.do" class="com.projectname.control.ExampleController"/>
    
    </beans>
    My Controller:

    Code:
    package com.projectname.control;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    /**
     * Spring MVC example controller.
     *
     */
    
    public class ExampleController  {
    	
    	@RequestMapping("/springTest")
    	public ModelAndView process() {
    		
    		return new ModelAndView("spring.jsp");
    	}
    
    	
    }
    When I start the application server (Tomcat 6) I get the following error:

    Code:
     2011-02-10 10:06:19,122 ERROR org.springframework.web.servlet.DispatcherServlet: Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/springTest.do' 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 [com.projectname.control.ExampleController]: Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems: 
    	Controller cannot be resolved to a type
    	Syntax error on tokens, delete these tokens
    	Syntax error, insert "enum Identifier" to complete EnumHeaderName
    	Syntax error, insert "EnumBody" to complete EnumDeclaration
    	Syntax error, insert "}" to complete ClassBody
    
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
    	
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    Caused by: java.lang.Error: Unresolved compilation problems: 
    	Controller cannot be resolved to a type
    If I use the @Controller annotation in the ExampleController and change the dispatcher-servlet.xml to:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
            
        <import resource="/application.xml"/>
    	
    	<context:component-scan base-package="com.projectname.control"/>
    
    </beans>
    I get:

    Code:
    2011-02-10 10:48:08,395 DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver: Looking for matching resources in directory tree [D:\source\Project\J2ee\TPW\webApplication\WEB-INF\classes\com\projectname\control]
     2011-02-10 10:48:08,395 DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver: Searching directory [D:\source\projectname\J2ee\TPW\webApplication\WEB-INF\classes\com\projectname\control] for files matching pattern [D:/source/projectname/J2ee/TPW/webApplication/WEB-INF/classes/com/projectname/control/**/*.class]
     2011-02-10 10:48:08,411 DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver: Looking for matching resources in directory tree [D:\localserver\localtpw\tpw\TPW\WEB-INF\classes\com\projectname\control]
     2011-02-10 10:48:08,411 DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver: Searching directory [D:\localserver\localtpw\tpw\TPW\WEB-INF\classes\com\projectname\control] for files matching pattern [D:/localserver/localtpw/tpw/TPW/WEB-INF/classes/com/projectname/control/**/*.class]
     2011-02-10 10:48:08,411 DEBUG org.springframework.core.io.support.PathMatchingResourcePatternResolver: Resolved location pattern [classpath*:com/projectname/control/**/*.class] to resources [file [D:\source\projectname\J2ee\TPW\webApplication\WEB-INF\classes\com\projectname\control\ExampleController.class], file [D:\localserver\localtpw\tpw\TPW\WEB-INF\classes\com\projectname\control\ExampleController.class]]
     2011-02-10 10:48:08,411 DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner: Scanning file [D:\source\projectname\J2ee\TPW\webApplication\WEB-INF\classes\com\projectname\control\ExampleController.class]
     2011-02-10 10:48:08,426 DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner: Ignored because not matching any filter: file [D:\source\projectname\J2ee\TPW\webApplication\WEB-INF\classes\com\projectname\control\ExampleController.class]
     2011-02-10 10:48:08,426 DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner: Scanning file [D:\localserver\localtpw\tpw\TPW\WEB-INF\classes\com\projectname\control\ExampleController.class]
     2011-02-10 10:48:08,442 DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner: Ignored because not matching any filter: file [D:\localserver\localtpw\tpw\TPW\WEB-INF\classes\com\projectname\control\ExampleController.class]
     2011-02-10 10:48:08,457 DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader: Loaded 5 bean definitions from location pattern [/WEB-INF/dispatcher-servlet.xml]
     2011-02-10 10:48:08,457 DEBUG org.springframework.web.context.support.XmlWebApplicationContext: Bean factory for WebApplicationContext for namespace 'dispatcher-servlet': org.springframework.beans.factory.support.DefaultListableBeanFactory@44b044b: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor]; root of factory hierarchy
    When I try the springTest.do in a browser I get:
    Code:
    17155620 2011-02-10 10:50:00,901 DEBUG org.springframework.web.servlet.DispatcherServlet: Bound request context to thread: org.apache.catalina.connector.RequestFacade@7ba27ba2
    17155620 2011-02-10 10:50:00,901 DEBUG org.springframework.web.servlet.DispatcherServlet: DispatcherServlet with name 'dispatcher' processing GET request for [/springTest.do]
    17155620 2011-02-10 10:50:00,901 DEBUG org.springframework.web.servlet.DispatcherServlet: Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@27a227a2] in DispatcherServlet with name 'dispatcher'
    17155620 2011-02-10 10:50:00,901 DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping: No handler mapping found for [/springTest.do]
    17155620 2011-02-10 10:50:00,901 DEBUG org.springframework.web.servlet.DispatcherServlet: Testing handler map [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping@7f807f8] in DispatcherServlet with name 'dispatcher'
    17155620 2011-02-10 10:50:00,901 DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping: No handler mapping found for [/springTest.do]
    17155620 2011-02-10 10:50:00,901 WARN  org.springframework.web.servlet.PageNotFound: No mapping found for HTTP request with URI [/springTest.do] in DispatcherServlet with name 'dispatcher'
    17155620 2011-02-10 10:50:00,901 DEBUG org.springframework.web.servlet.DispatcherServlet: Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@7ba27ba2
    17155620 2011-02-10 10:50:00,901 DEBUG org.springframework.web.servlet.DispatcherServlet: Successfully completed request
    17155620 2011-02-10 10:50:00,901 DEBUG org.springframework.web.context.support.XmlWebApplicationContext: Publishing event in WebApplicationContext for namespace 'dispatcher-servlet': ServletRequestHandledEvent: url=[/springTest.do]; client=[127.0.0.1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[16ms]; status=[OK]
    I'm using Spring 3 and I've put all the Spring jars into my project.

    Anybody know what's going on here? Any help would be greatly appreciated.

  • #2
    may be because you forgot the "@controller" in your controller class

    Comment


    • #3
      No, I used the @Controller annotation when I was using the <context: component-scan...

      I've found the reason now. I had experimented with different jars (versions of Spring) which were causing conflicts.

      Comment


      • #4
        No, I used the @Controller annotation when I was using the <context: component-scan...
        @Controller is not just for the component scan!!!

        Even if you define your bean in xml, you still need to mark the class with @Controller if you want Spring to recognize that class as a controller! Otherwise it's just a normal bean and marking the methods with @RequestMapping is useless, because urls will never be resolved to those methods if the class is not annotated with @Controller!!!!!

        I hope I made myself clear as that's a very important basic in Spring Annotated MVC.

        Comment


        • #5
          Ummm not really. You use the @Controller annotation with the component scan, and the bean definition without the @Controller and component scan (note the bean name). It works, try it, if you don't you'll have to come to me and I'll show you it working.


          I've got it working now it was a jar problem.

          Comment


          • #6
            Actually, you've both got it right. If you were using annotation-driven mapping, your code would fail without @Controller, even if you're not doing component scanning. However, you're NOT doing annotation-based scanning.

            When you define a bean like this:
            Code:
            <bean name="/springTest.do" class="com.projectname.control.ExampleController"/>
            that uses the BeanNameUrlHandlerMapping to do the request-to-class mapping, with @RequestMapping used to detemine the exact method.

            (Also, note that this is NOT defined out of the box when you use <mvc:annotation-driven/>. If you ever add that tag for custom conversion, bean validation, etc, your app will break until you define the BeanName mapper as well.

            That said, if you were to use a more traditional approach
            Code:
            <bean id="exampleController" class="com.projectname.control.ExampleController"/>
            you would absolutely need to use the @Controller annotation, even if you aren't using component scanning. That's because the DefaultAnnotationHandlerMapping only detects controllers annotated with @Controller.

            Also, note the latter syntax is preferred - in your example, you have to change two things (Spring config and class) when a URL changes, while the annotated approach only requires a change in the class.

            Hope this helps
            - Don

            Comment


            • #7
              Thanks Don.

              However, perhaps I didn't I make my original post clear. I tried two methods:

              1. @Controller annotation along with the <context:component-scan.../> in my <dispatcher-name>-servlet.xml

              2. No @Controller annotation with the <bean name="/spingTest.do" .../> replacing <context:component-scan.../>.

              I did this to investigate what was going on, both methods produced different output in the debug console. However, eventually I pinned it down to jar version conflicts.

              Comment

              Working...
              X