my dog learned polymorphism*
The moose likes Spring and the fly likes How to register  ContextLoaderListener ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to register  ContextLoaderListener ?" Watch "How to register  ContextLoaderListener ?" New topic
Author

How to register ContextLoaderListener ?

Gireesh Mulage
Ranch Hand

Joined: Mar 08, 2010
Posts: 44
HI All,

I am using Spring annotations. I am getting exception while accessing login page : java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?

I have written <context:component-scan base-package="XXX.XXX"/> in dispatcher-servlet.xml file.

Please help me in resolving this issue :

Thanks .
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
For adding ContextLoaderListener, you have to include the below tag in the web.xml.

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

But, how are you trying to load the dispatcher-servlet configuartion file ?

Could you please provide your dispatcher-servlet.xml file and web.xml so that we can drill down to the exact issue?


Naresh Waswani
Gireesh Mulage
Ranch Hand

Joined: Mar 08, 2010
Posts: 44
Thank you for replying ..

web.xml :

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

dispatcher-servlet.xml :

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">



<context:component-scan base-package="com.xx.xx.client" />
</beans>

Do i need to do any thing extra other than this?
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Servlet spec plays a major role in such type of errors. ContextLoaderListener works with all the continers supporting Servlet 2.4 spec and some servers supporting Servlet 2.3 spec.

Which Web container or application server (along with version) are you using ?

Please go through the below link to find more information.

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/context/ContextLoaderServlet.html
Gireesh Mulage
Ranch Hand

Joined: Mar 08, 2010
Posts: 44
I am using Tomcat 6 . It supports servlet 5 . so what must be the problem ?

Thank you .

Gireesh Mulage
Ranch Hand

Joined: Mar 08, 2010
Posts: 44
the controller class & jsp are as follows :

@RequestMapping(value = "/loginForm",method=RequestMethod.GET)
public void loginForm(Model model)
{
System.out.println("in loginForm()##################");
model.addAttribute(new LoginForm());
}

@RequestMapping(value = "/login", method = RequestMethod.POST)
public void onSubmit(@ModelAttribute LoginForm loginFormObj,Model model)
{
System.out.println("userId in onsubmit$$$$$$$$$$$"+loginFormObj.getUserId());
}

login.jsp has the form tag as :
<form:form method="POST" modelAttribute="loginForm" >
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 510

gireesh Mulage wrote:I am using Tomcat 6 . It supports servlet 5



I guess you meant servlet 2.5..
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 510

Try adding

to web.xml.. Not required though, but you can check with this once
Gireesh Mulage
Ranch Hand

Joined: Mar 08, 2010
Posts: 44
Thank you very much for your help .

My problem solved by adding @RequestMapping("login.do") annotation before start of class .
Also , added @RequestMapping(method=RequestMethod.GET) before start of both methods and loginForm() is renamed to showLoginForm()... Thats it!

Gireesh Mulage
Ranch Hand

Joined: Mar 08, 2010
Posts: 44
sorry for onSubmit() we need to use RequestMethod.POST and for showLoginForm we need to use RequestMethod.GET bcz that method will be loaded before loading the login.jsp.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to register ContextLoaderListener ?