aspose file tools*
The moose likes Spring and the fly likes DispatcherServlet vs ContextLoaderListener? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "DispatcherServlet vs ContextLoaderListener?" Watch "DispatcherServlet vs ContextLoaderListener?" New topic
Author

DispatcherServlet vs ContextLoaderListener?

selva raja
Ranch Hand

Joined: Nov 24, 2009
Posts: 69
Dear All,

In spring mvc,i have defined in web.xml DispatcherServlet and ContextLoaderListener.


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/xxxxxxx-servlet.xml
</param-value>
</context-param>


----------------------------------

<servlet>
<servlet-name>xxxxxxx</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

---------------------------------------
How many Instances are created during the start up in container (service,dao,handler)?

Thanks,
SR
Lalit Bhatt
Ranch Hand

Joined: Dec 27, 2007
Posts: 69

There is one instance of dispatcher servlet which than traps all the request.

Again there is no reason to have more than one Context loader listener.


Java-JavaEE Hibernate Spring Software Development Business Process Analysis
selva raja
Ranch Hand

Joined: Nov 24, 2009
Posts: 69
Thanks..Lalit Bhatt..

I have very basic question. I understood in third party serlvets(DWR,STRUST...ETC) are defining in ContextLoaderListener only. But in spring MVC how to define in web.xml? Is it necessary to use both configurations? If i am doing both how can I avoid 2 instance creation in spring mvc?

Thanks,
selva
selva raja
Ranch Hand

Joined: Nov 24, 2009
Posts: 69
Thanks....

I have very basic question. I understood in third party serlvets(DWR,STRUTS..etc) are defining in ContextLoaderListener only. But in spring MVC how to define in web.xml? Is it necessary to use both configurations? If i am doing both how can I avoid 2 instance creation in spring mvc?

Thanks,
selva
Lalit Bhatt
Ranch Hand

Joined: Dec 27, 2007
Posts: 69

In Spring MVC, there are two context. One is the application context or global context which is booted up by ContextLoaderListener. It takes all the configuration file mentioned in contextConfigLocation parameter.

Now if you are are using Spring MVC also than Dispatcher servlet is required, which boots up another container which is also known as web application container. This container takes the global container as a parent.

If you are using Spring in the backend only, then the global context is good enough. However if you are using Spring MVC also for front end than you need Dispatcher Servlet also.
selva raja
Ranch Hand

Joined: Nov 24, 2009
Posts: 69
Is it possible to configure third part framework (I.example : DWR,STRUTS,JSF…etcc)in dispatcher servlet file? i mean without ContextLoaderListener.

Thanks,
SR
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

selva raja wrote:Is it possible to configure third part framework (I.example : DWR,STRUTS,JSF…etcc)in dispatcher servlet file? i mean without ContextLoaderListener.

Thanks,
SR


If you don't have the ContextLoaderListener then you won't have your Parent Application Context with your services, repository/dao middle tier classes.

DispatcherServlet is only for Spring MVC classes in a child application context. So DispatcherServlet loads classes for the web layer. If you use Struts, then you don't need the DispatcherServlet, but will need the ContextLoaderListener if you want your Struts action classes to have access to you business logic and lower layers. If you are using Struts, the integration is to have your Action classes extend ActionSupport.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Muddassir Ahmad
Greenhorn

Joined: Nov 10, 2010
Posts: 10
hey all.

Thing is that i made my sample project (java web project) and added Spring capabilities and hibernate capabilities. In my web.xml i defined ContextLoaderListener and gave ContextConfigLocation
************************************************** *********
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListe ner
</listener-class>

</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/applicationContext-hibernate.xml


</param-value>

</context-param>

************************************************** *******
applicationContext.xml is containing beans (conrollers, managers and DAO declarations)
applicationContext-hibernate.xml is containing dataSource, sessionFactory and transactionManager information.


after that i decided to add MVC module of spring and add following into web.xml

************************************************** **
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

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

************************************************** *****
I have added action-servlet.xml in WEB-INF folder. and provided "viewResolver" and "urlMapping". BUt when i try to run the project following error occur

**************************************************
javax.servlet.ServletException: No adapter for handler [controller.CityController@17ebdf8]: Does your handler implement a supported interface like Controller?


************************************************** *
Although I have provided CityController in action-servlet.xml

************************************************** *
This is being a headache for me since one day. Is this not the way to use Spring + Spring MVC + Hibernate

**********************************
I m using forums for the very first time. Sorry if i forget any discipline.


hey i will be working hard to find out people's problems
selva raja
Ranch Hand

Joined: Nov 24, 2009
Posts: 69
please look into below spring forums get it resolved,

http://forum.springsource.org/showthread.php?t=52432

Thanks,
SR
Muddassir Ahmad
Greenhorn

Joined: Nov 10, 2010
Posts: 10
Hey thanks for your quick reply SR...
But on the link you provided its using annotation base classes. M using just simple controller. I m send you its code...

Now is it possible to use applicationContext.xml , applicationContext-hibernate.xml and action-servlet.xml all together.......
and using DispatcherServlet and ContextLoaderListener both in xml...?
selva raja
Ranch Hand

Joined: Nov 24, 2009
Posts: 69
Hi Muddassir,

please use this subclass your controller (CityController ) from MultiActionController.

Then in your bean configuration,

<bean id="cityController MultiController" class="com.xxxxx.xxxxx.CityController ">
<property name="methodNameResolver" ref="paramResolver"/>
... other property settings...
</bean>

you need to set other configurations...etc..

Thanks,
SR
Muddassir Ahmad
Greenhorn

Joined: Nov 10, 2010
Posts: 10
Thanks Raja... your tip is quite help full.
There is a question that I got three xml files.

1. what I define as ContextConfigLocation (two in number)

a: applicationContext.xml (In this I define beans about Controller, Manager (Business Logic) and DAO)
b: applicationContext-hibernate.xml (In this I define dataSource, sessionactory, TransactionManager)
2. What I define as DispatcherServlet
<servletName>-servlet.xml (In this I give controllers and their URLMappings.)

My question is this can i skip defining controllers in DispatcherServlet's xml as it can also pick them from applicationContext.xml. BUt when i delete controllers from <servletName>-servlet.xml file , it start generating exceptions that controller not find and all that.

What to do to make these three xmls work together?
selva raja
Ranch Hand

Joined: Nov 24, 2009
Posts: 69
HI,

DispatcherServlet will always load its own configuration file using <servlet_name>-servlet.xml. This file will contain web components such as Controllers, ViewResolvers and LocaleResolvers.( <servletName>-servlet.xml)

The ContextLoaderListener is then used to load the files containing your middle tier and data tier components. (applicationContext.xml, applicationContext-hibernate.xml).

No need to define two xml files in ContextConfigLocation. You can merge these files in single.

Thanks,
SR
Muddassir Ahmad
Greenhorn

Joined: Nov 10, 2010
Posts: 10
Thanks Raja. I am just deleting controller from applicationContext.xml, and defining them in <servletName>-servlet.xml

and every MultiActionControler has a decalartion + bean attach to it for methodNameResolver.
Every MultiActionController has its separate methodNameResolver bean.

There is one more question, how can we set scope of beans defined in <servletName>-servlet.xml.

Regards
selva raja
Ranch Hand

Joined: Nov 24, 2009
Posts: 69
HI,

http://static.springsource.org/spring/docs/2.0.x/reference/beans.html#beans-factory-scopes

Thanks,
SR
Muddassir Ahmad
Greenhorn

Joined: Nov 10, 2010
Posts: 10
Hey all.. this is my code...

1. web.xml



2. action-servlet.xml



3. CityController



Exception ocurs if I run the http://localhost:8080/guitarLesson/city.jsp

it is caling recursively. I am unable to understand what is going on. I need help.

Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DispatcherServlet vs ContextLoaderListener?