Results 1 to 4 of 4

Thread: Spring MVC + DispatcherServlet and ContextLoaderListener

  1. #1
    Join Date
    Jan 2011
    Posts
    4

    Post Spring MVC + DispatcherServlet and ContextLoaderListener

    Hi Experts,

    I am trying to come up with a simple spring application - with Spring MVC as the web layer.

    Following best practices, I have broken down my app definitions into layers and so have different XML files for web, app and resources bean definitons.

    However, after setting the "contextConfigLocation" parameter the URLs dont come up. They give the error - "The requested resource () is not available."
    with the following lines in the log -


    Code:
    Jan 30 15:50:09 DEBUG .web.servlet.DispatcherServlet getLastModified           DispatcherServlet with name 'myTestapp' determining Last-Modified value for [/myTestspringapp/login.rm]
    Jan 30 15:50:09 DEBUG .web.servlet.DispatcherServlet getLastModified           No handler found in getLastModified
    Jan 30 15:50:09 DEBUG .web.servlet.DispatcherServlet doService                 DispatcherServlet with name 'myTestapp' processing request for [/myTestspringapp/login.rm]
    Jan 30 15:50:09 WARN  ework.web.servlet.PageNotFound noHandlerFound            No mapping found for HTTP request with URI [/myTestspringapp/login.rm] in DispatcherServlet with name 'myTestapp'
    Jan 30 15:50:09 DEBUG .web.servlet.DispatcherServlet processRequest            Successfully completed request
    My web.xml looks like -


    Code:
    	 
    
    <context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    			WEB-INF/springs/applicationContext.xml		
    	    </param-value>
    	</context-param>
    	
    	<listener>
    		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    	</listener>
    
        <listener>
            <listener-class>
                org.springframework.web.context.ContextLoaderListener
            </listener-class>
        </listener>
    
        <servlet>
    		<servlet-name>myTestapp</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value></param-value>
    		</init-param> 
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>myTestapp</servlet-name>
    		<url-pattern>*.rm</url-pattern>
    	</servlet-mapping>

    I have confirmed that with the above config on, there is no - "myTestapp-servlet.xml" anywhere. Also that the individual XMLs imported under the applicationContext.xml are all loaded - confirmed from the logs. However, the page doesnt come up.

    If I disable the "ContextLoaderListener and the config file config" + disable the "init param under dispatcher servlet" - and allow springs to load from a single myTestapp-servlet.xml, the pages come up fine - confirming that my views.xml and other URL configs are all configured ok.

    Any clues what could be wrong.

    Any help would be greatly appreciated ....

    Cheers,
    Dk
    Last edited by DKuruppath; Jan 30th, 2011 at 10:28 AM.

  2. #2
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    287

    Default

    This should work,

    Code:
    	<servlet>
    		<servlet-name>myTestapp</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>WEB-INF/springs/applicationContext.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    Amila Domingo

  3. #3
    Join Date
    Jan 2011
    Posts
    4

    Cool What happens to CLL ?

    Thank you amiladomingo !

    It did work. In fact, it just delegated the *-servlet.xml to my applicationContext path hard coded in the dispatcher servlet. Good idea !



    However, it did it at the cost of ContextLoaderListener, which I had to disable for this to work.

    Can I have the ContextLoaderListener active as well, so that in my applicationContext.xml, I could add component-scans ?

    Rgds
    DK

  4. #4
    Join Date
    Nov 2005
    Posts
    113

    Default

    Okay, a couple points:

    • Component scanning will work whether you're using the Dispatcher Servlet OR the ContextLoaderListener (or both)
    • It's typical to see both used in the same application, but you should assign the individual XML files to one or the other (i.e. don't use that single master XML file)


    For example:

    Code:
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/app-config.xml
            /WEB-INF/spring/resource-config.xml
        </param-value>
    </context-param>
    	
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    
    <servlet>
        <servlet-name>myTestapp</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/web-config.xml</param-value>
        </init-param> 
        <load-on-startup>1</load-on-startup>
    </servlet>
    (If you had included your applicationContext.xml in both places, you'd actually get TWO copies of all your beans created, which is probably not what you intended)

    Hope this helps
    - Don

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •