Spring MVC教程问题 - DispatcherServlet配置需要包含支持此处理程序的HandlerAdapter

时间:2015-12-03 18:18:34

标签: java spring spring-mvc

我正在尝试基本的Spring MVC教程,遇到以下错误 -

javax.servlet.ServletException: No adapter for handler [com.srs.springapp.web.BasicController@e0fd2a]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
    org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1163)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

“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"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>SpringWebApp</display-name>

    <servlet>
        <servlet-name>springapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springapp</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

“springapp-servlet.xml”文件如下:

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

  <!-- the application context definition for the springapp DispatcherServlet -->
  <bean name="/hello.htm" class="com.srs.springapp.web.BasicController"/></beans>

以下是名为BasicController.java的控制器类的代码:

package com.srs.springapp.web;

import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;

public class BasicController {
    protected final Log logger = LogFactory.getLog(getClass());

    public ModelAndView handleRequest(HttpServletRequest request,     HttpServletResponse response)
        throws ServletException, IOException {

    logger.info("Returning hello view");

    return new ModelAndView("hello.htm");
    }
}

我查看了此论坛中的类似帖子,但无法获得正确的解决方案。任何输入都非常感谢。我使用的是以下版本的软件 -

  1. Java v1.8.0_512
  2. Spring Framework v 4.2.2
  3. Apache Tomcat v 7.0.64
  4. 非常感谢任何帮助。

    谢谢,

    拉​​马。

1 个答案:

答案 0 :(得分:2)

感谢您的回复。我发现了我的错误 - “BasicController”类需要在java代码中实现“Controller”接口,如此帖子所示。