javax.servlet.ServletException: No adapter for handler

时间:2020-04-11
本文章向大家介绍javax.servlet.ServletException: No adapter for handler,主要包括javax.servlet.ServletException: No adapter for handler使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

问题描述:

 我的web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
<!--前端控制器DispactherServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

springmvc.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--扫包-->
    <context:component-scan base-package="com.courage"></context:component-scan>

    <!--映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
    <!--适配器-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
    <!--解析器-->
    <bean id="internalResourceViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="WEB-INF/page/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean id="/test01" class="com.courage.controller.FirstController"></bean>

</beans>

Controller如下:

package com.courage.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FirstController {

    @RequestMapping("/test01")
    public String test01(){
        System.out.println("test01");
        return "success";
    }
}

解决办法:

springmvc.xml中删掉:

 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
 <bean id="/test01" class="com.courage.controller.FirstController"></bean>

springmvc.xml中添加:

<mvc:annotation-driven />

原因:

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc

 

原文地址:https://www.cnblogs.com/Courage129/p/12679092.html