• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ContextLoaderListener in web.xml

 
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic