Controller handler方法支持返回类型 [英] Controller handler method supported return types

查看:136
本文介绍了Controller handler方法支持返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习Spring框架时,我在书中注意到 Spring in Action ,作者没有在控制器中使用 ModelandView 方法返回类型。作者声明控制器方法为 String 的返回类型,方法中的return子句只返回一个字符串,如 return/ views / theview;

While learning the Spring framework, I notice in the book Spring in Action, the author doesn't use ModelandView method return type in controllers. The author is declaring the controller methods as return type of String and the return clause in the method is just returning a string such as return "/views/theview";

有人可以详细说明其工作原理的内部差异吗?

Can someone elaborate the internal differences of how this works?

推荐答案

以下是深入了解。

Spring提供 DispatcherServlet 类通常,它会处理您的所有请求。它在 doDispatch(HttpServletRequest请求,HttpServletResponse响应)方法中执行此操作

Spring offers a DispatcherServlet class that, typically, handles all your requests. It does this in its doDispatch(HttpServletRequest request, HttpServletResponse response) method

// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

其中 mv 是最终的 ModelAndView object, ha 是使用 @RequestMapping 。

where mv is the final ModelAndView object, ha is a wrapper to your controller method annotated with @RequestMapping.

这通常会经过一堆方法调用,结束于 ServletInvocableHandlerMethod.invokeAndHandle

This will usually go through a stack of method calls ending up at ServletInvocableHandlerMethod.invokeAndHandle

at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle
at org.springframework.web.servlet.DispatcherServlet.doDispatch

看着来源

public final void invokeAndHandle(ServletWebRequest webRequest,
                ModelAndViewContainer mavContainer, Object... providedArgs) throws Exception {

    Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);

    setResponseStatus(webRequest);

    if (returnValue == null) {
        if (isRequestNotModified(webRequest) || hasResponseStatus() || mavContainer.isRequestHandled()) {
            mavContainer.setRequestHandled(true);
            return;
        }
    } else if (StringUtils.hasText(this.responseReason)) {
        mavContainer.setRequestHandled(true);
        return;
    }

    mavContainer.setRequestHandled(false);

    try {
        this.returnValueHandlers.handleReturnValue(returnValue, getReturnValueType(returnValue), mavContainer, webRequest);
    }
    catch (Exception ex) {
        if (logger.isTraceEnabled()) {
            logger.trace(getReturnValueHandlingErrorMessage("Error handling return value", returnValue), ex);
        }
        throw ex;
    }
}

returnValue @RequestMapping 方法返回的对象。它通过

returnValue is the object returned by your @RequestMapping method. It goes through

this.returnValueHandlers.handleReturnValue

其中Spring确定 HandlerMethodReturnValueHandler 来处理该对象。

where Spring determines a HandlerMethodReturnValueHandler to handle that object.

public void handleReturnValue(
        Object returnValue, MethodParameter returnType,
        ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
        throws Exception {

    HandlerMethodReturnValueHandler handler = getReturnValueHandler(returnType); // returns the appropriate handler
    Assert.notNull(handler, "Unknown return value type [" + returnType.getParameterType().getName() + "]");
    handler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
}

getReturnValueHandler(returnType); 返回适当的处理程序。 HandlerMethodReturnValueHandler 是一个带有 supportsReturnType 方法的接口,它返回 true 如果处理程序支持该类型( String 查看 ResponseEntity 等等支持的返回类型))。所以该方法返回它找到的第一个支持该类型的处理程序并运行它。

getReturnValueHandler(returnType); returns the appropriate handler. The HandlerMethodReturnValueHandler is an interface with a supportsReturnType method that returns true if the handler supports that type (String, View, ResponseEntity, etc. (look for supported return types)). So the method returns the first handler it finds that supports that type and runs it.

Spring在初始化时注册了一大堆的实现HandlerMethodReturnValueHandler 。基本上所有在其javadoc中已知的实现类

Spring, at initialization, registers a whole slew of implementations of HandlerMethodReturnValueHandler. Basically all the known implementing classes in its javadoc.

例如,如果返回一个String,Spring将使用a ViewNameMethodReturnValueHandler 来处理响应。

For example, if you return a String, Spring will use a ViewNameMethodReturnValueHandler to handle the response.

现在,要使用的返回类型是您。如果你想返回一个 Model 所以你可以在你的jsp视图中使用请求属性,你可以让Spring传递一个模型您的方法的实例,或者您可以自己创建模型对象并将其传递给返回的 ModelAndView 。在大多数情况下,这是一种风格问题。

Now, which return type to use is up to you. If you wanted to return a Model so you can use request attributes in your jsp view, you can either have Spring pass a Model instance to your method or you can create the Model object yourself and pass it to a ModelAndView which your return. It's a matter of style in most cases.

这篇关于Controller handler方法支持返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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