spring 处理程序异常没有适配器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3896013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:09:51  来源:igfitidea点击:

No adapter for handler exception

springspring-mvc

提问by Harish

I'm developing a web application using spring mvc framework. I want to to both annotation and beanName based url mappings.

我正在使用 spring mvc 框架开发 Web 应用程序。我想要基于注释和 beanName 的 url 映射。

I've the following configurations in my context file

我的上下文文件中有以下配置

<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

My annotation based controllers are working fine but beanNamed based url mapping are throwing the exception "No adapter for handler".

我的基于注释的控制器工作正常,但基于 beanNamed 的 url 映射抛出异常“没有处理程序的适配器”。

What am I doing wrong here?

我在这里做错了什么?

Thank you

谢谢

回答by Arun P Johny

By default the spring mvc defines 3 different request handler adapters, they are

默认情况下,spring mvc 定义了 3 个不同的请求处理程序适配器,它们是

org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

So you need not have to define them in your context file, but if you define at least one handler adapter in your context files, spring will not create the default adapters.

所以你不需要在你的上下文文件中定义它们,但是如果你在你的上下文文件中定义了至少一个处理程序适配器,spring 将不会创建默认适配器。

In your configuraion you are using <mvc:annotation-driven />, according to thisspring documentation this will cause the context to define both DefaultAnnotationHandlerMappingand AnnotationMethodHandlerAdapter. Since we are creating the AnnotationMethodHandlerAdapterin our context definition spring will not create the other two handlerAdapters. That is why you are getting the said exception.

在您使用的配置中<mvc:annotation-driven />,根据这个spring 文档,这将导致上下文同时定义DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter。由于我们AnnotationMethodHandlerAdapter在上下文定义中创建了spring,因此不会创建其他两个 handlerAdapter。这就是为什么您会收到上述异常。

The beanNameUrlMapping needs the handlerAdapter SimpleControllerHandlerAdapter.

beanNameUrlMapping 需要 handlerAdapter SimpleControllerHandlerAdapter

To resolve this exception you can simply create a new bean of type "SimpleControllerHandlerAdapter" in your context.

要解决此异常,您只需在您的上下文中创建一个类型为“SimpleControllerHandlerAdapter”的新 bean。

<bean
    class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
</bean>

回答by Ankit

The same thing happened to me when I was trying to integrate spring MVC 2.5 with spring web flow. For webflow I have given FlowHandlerdAdapter in the configuration file but none for MVC. Thereby as said above we need to define a BEAN of type simplecontrollerhandleradapter in this case which will solve the problem.

当我尝试将 spring MVC 2.5 与 spring web flow 集成时,同样的事情发生在我身上。对于 webflow,我在配置文件中提供了 FlowHandlerdAdapter,但没有为 MVC 提供。因此,如上所述,在这种情况下,我们需要定义一个 simplecontrollerhandleradapter 类型的 BEAN,这将解决问题。