gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:43:50 24 4
gpt4 key购买 nike

我正在尝试基本的 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 的 Controller 类的代码:

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 框架 4.2.2 版
  3. Apache Tomcat v 7.0.64

非常感谢任何帮助。

谢谢,

拉玛斯。

最佳答案

感谢您的回复。我发现了我的错误——“BasicController”类需要在 java 代码中实现“Controller”接口(interface),如本文所示。

关于java - Spring MVC Tutorial 问题 - DispatcherServlet 配置需要包含支持此处理程序的 HandlerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34073363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com