aspose file tools*
The moose likes Spring and the fly likes ContextLoaderListener in web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "ContextLoaderListener in web.xml" Watch "ContextLoaderListener in web.xml" New topic
Author

ContextLoaderListener in web.xml

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 217

Hi all,
I have following web.xml file


Why do i have duplicate entries in the context-param and init param for the dispatcher servlet.I am not having the xxx-servlet file which the dispatcher servlet searches by default.I have all the bean definitions in the two xml files given.So,why the duplicate entries?

Thanks.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

I am going to answer both threads you have in this one post.

ContextLoaderListener is a ServletListener. So in the JSP/Servlet spec a Servlet Listener gets called by the container when certain events occur. In this case it gets called right after when the ServletContext is created for the web application. When it gets called, inside the ContextLoaderListener is code to create/instantiate an ApplicationContext and load in the xml file you set with the context-param name and value tags. In those xml files you define your middle tier beans only. No Web Tier beans should be in the xml file/files that is loaded by the ContextLoaderListener.

The DispatcherServlet in its init method also creates an ApplicationContext, but this one should load in only the Web Tier beans like your Controllers, ViewResolvers, MappingHandlers etc.

So the reason why you do this is to separate the web tier from the middle tier. The web tier beans can see the middle tier beans, but not the other way around.

In your example you got it wrong because you put the same xml config files to be loaded by both the ContextLoaderListener and the DispatcherServlet.

So my guess is that your /WEB-INF/mvc-config.xml should be in the DispatcherServlet config and the

repository-config.xml is what should be in the <context-param> with your ContextLoaderListener.

Hope that helps

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 217

Thanks Mark for the reply.
It really helped.
I want to have one more doubt get cleared.In the last xml file i posted,do I have two ApplicationContexts genearted then?One by the DispatcherServlet and other by ContextLoaderListener?Also,do i have all my beans created when my application context gets created/loaded?
Who is called first,DispatcherServlet or ContextLoaderListener?
Thanks...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

All the beans you have configured in your xml have been created twice. You duplicated the xml files in both the ContextLoaderListener and DispatcherServlet web.xml configuration, but one instance of each bean in each application context. You get two ApplicationContexts created.

In JSP/Servlets, what gets created first the ServletContext or a Servlet?

That answers which application context gets created first. DispatcherServlet is a Servlet. ContextLoaderListener gets called right after the ServletContext gets created.
Dakshinamurthy Babu
Greenhorn

Joined: Oct 17, 2012
Posts: 1
I am doing a sample springs extjs application. In that i have a text box and submit button in front end. And i used the handler function as


handler : function()
{

var user=Ext.getCmp('username').getValue();
alert(user);
Ext.Ajax.request({
url : '/hi.do'
})
}


and my controller is


public class HelloWorldController {


@RequestMapping(value = "/hi", method = RequestMethod.POST)
public void display(@RequestParam("username") String name, Model model) {
String message = "Hi " + name + "!";
System.out.println(message);

}

My web.xml is

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>HelloWorldController</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldController</servlet-name>
<url-pattern>/HelloWorldController</url-pattern>
</servlet-mapping>




But i couldnt getting that textbox value in controller.

Am new to springs.. SO please help me. Thanks in advance....

Regrads,
Dakshina
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ContextLoaderListener in web.xml