以编程方式调用@Controller [英] Programmatically call @Controller

查看:109
本文介绍了以编程方式调用@Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在转换使用Spring MVC的Controller实现的代码来使用注释构造型@Controller。除了一个问题,一切都很顺利:

I am transitioning code that used implementations of Spring MVC's Controller to use the annotation stereotype @Controller. Everything is going fine except for one issue:

给定请求/响应,如何以编程方式处理基于注释的控制器的请求?

Given a request/response, how do I programmatically handle requests for annotation-based controllers?

以前,(无论实施情况如何)我都可以致电:

Previously, (regardless of implementation) I was able to call:

controller.handleRequest(request, response)

带注释的等价物是什么?我曾经假设会有一些(可能是静态的)实用程序类:

What is the equivalent with annotations? I had assumed there would be some (perhaps static?) utility class along the lines of:

SpringAnnotationBasedControllerUtils.handleRequest(<? extends @Controller> handlerObject, HttpServletRequest request, HttpServletResponse response);

处理将请求映射到@Controller构造型允许的动态签名的详细信息,但是我找不到这样的东西。

to handle the details of mapping a request to the dynamic signatures allowed by the @Controller stereotype, but I can't find such a thing.

有什么建议吗?

(请不要评论为什么这是一个这是维护中的代码,并且必须尽可能无创,因此目前无法完全重写。)

(Please no comments on why this is a bad idea or should be unnecessary with a "good" design, etc. This is code under maintenance and must be as noninvasive as possible so a complete re-write is not an option at this time.)

谢谢!

推荐答案

您只需在对象上调用该方法即可。这是注释驱动控制器的一大优势,您可以只编写代码所需的方法,而不必弯曲变形以符合接口。所以只需将控制器bean连接到你的代码中,然后直接调用该方法。

You just call the method on the object. That's one of the big benefits of annotation-driven controllers, you can write just the methods your code needs without having to bend things out of shape to conform to the interfaces. So just wire your controller bean into your code, and call the method directly.

或者你是说你想重新发明Spring将请求映射到的机制注释控制器?如果是这样,请查看Spring的 AnnotationMethodHandlerAdapter DefaultAnnotationHandlerMapping 类的源代码。但它很复杂 - 注释控制器使编写控制器变得更容易,但底层机制令人不快。

Or are you saying you want to re-invent the mechanism by which Spring maps requests on to annotated-controllers? If so, have a look at the source for Spring's AnnotationMethodHandlerAdapter and DefaultAnnotationHandlerMapping classes. It's complex, though - annotated controllers make it easier to write controllers, but the underlying mechanism is unpleasant.

这篇关于以编程方式调用@Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆