Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.servlet.ServletException: No adapter for handler

Tags:

spring-mvc

I am learning spring and got stuck to one problem. I tried a lot and also google'd allot. I know that this question has been asked by many and they got there problem resolved , I treid them too but I am still getting the same error. Hence I am posting my files here please help me on this. I am trying too hard but getting no output.

My Web.xml looks like :

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

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

and my spring-servlet.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 http://www.springframework.org/schema/context          http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframew ork.org/schema/context">

   <mvc:resources location="/images/" mapping="/images/**"/>
   <mvc:resources location="/jsp/js/" mapping="/jsp/js/**"/>
   <context:component-scan base-package="org.vinay.spring_mvc.mycontroller"></context:component-scan>
   <context:annotation-config/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"  value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/jsp/" />
    <property name="suffix" value=".jsp" />
    </bean>
    <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="/employee">myController</prop>
    </props>
    </property>
     </bean>
    <bean id="myController" class="org.vinay.spring_mvc.mycontroller.MyController"></bean>
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
    </beans>

and my controller looks like:

package org.vinay.spring_mvc.mycontroller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;



@Controller
@RequestMapping(value = "/employee", method = RequestMethod.GET)
public class MyController {


public ModelAndView helloWorld()
{
    System.out.println("Hello World");
    return new ModelAndView("/hello");
    //return new ModelAndView("helloWorld.jsp");
}
}

and the JSP from which Iam sending request is a welcome file of web.xml. i.e. index.jsp . it looks like:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()path+"/";
%>
  <%System.err.print("Path : "+request.getContextPath()); %>

 <html>
<head>
 <title>Solution Center</title>
    <h5>Welcome To Solution Center</h5>
</head>
<body>
<a href="employee">hello</a>
</body>
</html>

Can anyone please help me why i am getting the below errors:

javax.servlet.ServletException: No adapter for handler   [org.vinay.spring_mvc.mycontroller.MyController@18c8aea]: The DispatcherServlet configuration needs  to include a HandlerAdapter that supports this handler
    org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter    (DispatcherServlet.java:1128)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:903)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:816)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

I tried using annotation based handler adapter also but got the same error. any help on this will be helpful.

I only figured out that I am getting this due to inclusion of mvc:resources tags. I f I Comment them my project works fine. Any help on them would be very helpful . I already have tried lot of other ways please provide me step by step solution. I am not able to solve this problem from last 4 hours.

like image 291
Vinay Pandey Avatar asked Dec 20 '22 03:12

Vinay Pandey


1 Answers

Delete the following from your spring-servlet.xml

<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/employee">myController</prop>
        </props>
    </property>
 </bean>
 <bean id="myController" class="org.vinay.spring_mvc.mycontroller.MyController"></bean>
 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>

Add <mvc:annotation-driven /> which will configure your application context automatically for use with annotated controllers.

See http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enable for more info.

like image 181
Bart Avatar answered May 25 '23 09:05

Bart