Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to register ContextLoaderListener ?

 
Gireesh Mulage
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Gireesh Mulage
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Tomcat 6 . It supports servlet 5 . so what must be the problem ?

Thank you .

 
Gireesh Mulage
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 667
4
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gireesh Mulage wrote:I am using Tomcat 6 . It supports servlet 5



I guess you meant servlet 2.5..
 
Prasad Krishnegowda
Ranch Hand
Posts: 667
4
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding

to web.xml.. Not required though, but you can check with this once
 
Gireesh Mulage
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic