PDA

View Full Version : Setting up the WebApplicationContext...


nw60312
Sep 10th, 2007, 11:44 AM
Hello - Little confused as to the different roles that ContextLoaderListener and the DispatcherServlet play in Web.xml. Is it generally the case that if the DispatcherServlet is defined you don't need to declare the ContextLoaderListener? Is there any reason to define both?

Thanks!

infinity2heaven
Sep 10th, 2007, 02:25 PM
1) DispatcherServlet is for configuring the main servlet for your web-application. In a Spring web-app this would be
org.springframework.web.servlet.DispatcherServlet.

2) ContextLoaderListener is a Bootstrap listener to start up Spring's root which contains the Spring's Application Contexts. It extends ServletContextListener of the Servlet API.

Without 2) you will not be able to load the application contexts (unless you hack the Dispatcher Servlet to load the same)

lumpynose
Sep 10th, 2007, 08:33 PM
If you have both, which you probably should, until you figure out what to put in the applicationContext.xml it can have just
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans />
(But DOCTYPE is so old school; try to use the nicer xml schema stuff, with the xmlns= stuff in the beans tag.)

The applicationContext.xml should have things like your database stuff, your business logic, etc. The mySpringApp-servlet.xml is where the controllers, view resolvers, etc. go.

I found the book "Expert Spring MVC and Web Flow" by Seth Ladd helpful in understanding the layers and separating the different parts.