Announcement Announcement Module
Collapse
No announcement yet.
DispatcherServlet and ContextLoaderListener Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • DispatcherServlet and ContextLoaderListener

    In a web app, the web.xml has the following:

    <servlet>
    <servlet-name>@WEBAPP@</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/test-servlet.xml
    /WEB-INF/spring/test-web-layer.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
    <servlet-name>@WEBAPP@</servlet-name>
    <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    --My understanding:

    When submit a test.htm?cmd=testFunc from the javascript, per the web.xml, it will find the servlet @WEBAPP@, then the DispatcherServlet. From there, it will locate the controller class and inject in the method such as testFunc.

    --Is this correct understanding?



    The web.xml has another section:

    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>


    --What is this ContextLoaderListener used for and how does it work?




    Thanks


    Scott

  • #2
    Hi scott,

    If you are new to Spring I would recommend the book "Java Development with the Spring Framework" (Wrox press), its a oldie but a goodie.

    That said, here is a sample web.xml I have used so the basic parts of it can be explained :
    Code:
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    	<display-name>New Spring MVC</display-name>
    
     	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:nz/co/curlybrackets/tutorial/newspring/applicationContext-services.xml
    		</param-value>
    	</context-param>
    	
    	<listener>
    		<listener-class>
    			org.springframework.web.context.ContextLoaderListener
    		</listener-class>
    	</listener>
    	
    	<context-param>
    		<param-name>defaultHtmlEscape</param-name>
    		<param-value>true</param-value>
    	</context-param>
    
    	<servlet>
    		<servlet-name>newspring-sampling</servlet-name>
    		<servlet-class>
    			org.springframework.web.servlet.DispatcherServlet
    		</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/applicationContext-mvcConfig.xml,
    					 /WEB-INF/applicationContext-controllers.xml,
                                             classpath:nz/co/curlybrackets/tutorial/newspring/applicationContext-web.xml</param-value>
    		</init-param>
    	</servlet>
    	
    	<servlet-mapping>
    		<servlet-name>newspring-sampling</servlet-name>
    		<url-pattern>/nwm/*</url-pattern>
    	</servlet-mapping>
    
    	<!-- Session timeout period in minutes -->
    
    	<session-config>
    		<session-timeout>30</session-timeout>
    	</session-config>
    
    	<welcome-file-list>
    		<welcome-file>index.jsp</welcome-file>
    	</welcome-file-list>
    
    </web-app>
    In the web.xml I have two areas which I include Spring application context files, the ContextLoaderListener and within the servlet definition. The difference between the two what servlet has access to what beans defined. In the case of the servlet definition, only that servlet can access and use the beans defined there. The ContextLoaderListener on the other hand has a corresponding
    Code:
    <context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:nz/co/curlybrackets/tutorial/newspring/applicationContext-services.xml
    		</param-value>
    	</context-param>
    which defines what application contexts are to be loaded for the entire web apps servlets to use and access. It is recommended that you put you repositories, services, and shared beans within this area. Use the servlet definition for controllers and beans specific to that servlet.

    Regarding your web app name, try and use something meaningful, and maybe without special characters like '@' as it is ugly (although thats more of a personal opinion). Its hard to know where test.htm is going to lead without seeing your mapping config within your Spring application context.

    Have a good read of the reference docs and download the sample applications as they contain a wealth of information on how to set up a web app.

    Hope this helps

    Josh

    Comment

    Working...
    X