Jasu
Jasu

Reputation: 35

Spring MVC No handler found

I'm newbie to spring mvc and trying to develop a very basic login webapp. I get the below error while running the project. I have tried almost everything and could not fix this error since last two weeks. Please can someone help me.

May 21, 2013 2:37:12 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringWeb/WEB-INF/jsp/loginnn.jsp] in DispatcherServlet with name 'spring'

My jsp pages resides under WEB-INF/jsp. The method loginpage in my controller is gettting invoked but the view name is not being rendered and resolved. Greatly appreciate your help.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>testspring</display-name>
<servlet>
<servlet-name>frontcontrol</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>frontcontrol</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

frontcontrol-servlet.xml

<context:component-scan base-package ="com.shell.spring.testspringapp">
</context:component-scan>
<bean id ="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />      
</bean>

</beans>

Controller class

@Controller

public class Firstcontrol {

@RequestMapping(value="/")
public ModelAndView invokeme(Model m)
{

ModelAndView mav=new ModelAndView();
mav.setViewName("result");
System.out.println("In method");
return mav;
}

@RequestMapping(value="/submit" ,method=RequestMethod.GET)
public String submit(Model m)
{
System.out.println("In submitmethod");
return "submit";
}
}

Upvotes: 0

Views: 39063

Answers (6)

ARVIND MANNALLE
ARVIND MANNALLE

Reputation: 1

First note down the package name of controller class. now check the package name wherever you have used, might be misspelled. Check mainly in viewResolver class.

Upvotes: 0

biswarup bannerjee
biswarup bannerjee

Reputation: 145

<context:component-scan base-package="your base packege " />

You should check the following section in spring configuration xml. Maybe you copied it from somewhere and forgot to make it your package name. Spring will not be able to scan packages if it so and eventually show this error.

Upvotes: 1

swist
swist

Reputation: 1171

@sanjay and @Will Keeling are both right. It is required to use <mvc:default-servlet-handler/> (as the last handler) and enable both <mvc:annotation-driven/> and <context:Annotation-config/>

Upvotes: 0

sanjay
sanjay

Reputation: 11

I saw same error, turned out that you need enable MVC annotations separately

<mvc:annotation-driven/>

In addition to

<context:Annotation-config/>

Upvotes: 1

Will Keeling
Will Keeling

Reputation: 23014

Since you're mapping your DispatcherServlet to '/' try adding <mvc:default-servlet-handler/> to your spring-servlet.xml

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

In the web.xml file the configuration file (spring-servler.xml) should be specified to be used by the dispatcher servlet, since your not using the conventional name, which is the [servlet-name]-context.xml.

Since Spring cannot find the configuration file for the dispatcher servlet the viewresolver is never registered. I assume that 'spring-servler.xml is located within the WEB-INF folder in my example, so you may need to adjust.

Also notice I switched the servlet mapping to / which helps if you need to resolve static resources, since / acts as a catch all as opposed to mapping everything through the dispatcher.

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-servler.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

You could also try using a regular view resolver, if your not using JSTL.

Replace the jstl view resolver:

<bean id ="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
 <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />      
 </bean>

With:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

Try returning a String from your controller that specifies the view name:

 @RequestMapping(value="/")
 public String loginpage(Model model)
{  
   Employee emp=new Employee();
    ModelAndView mav=new ModelAndView();
    model.addObject("emp", emp);
    return "loginnn";
}

Upvotes: 0

Related Questions