Example usage for org.springframework.web.method.annotation ModelFactory initModel

List of usage examples for org.springframework.web.method.annotation ModelFactory initModel

Introduction

In this page you can find the example usage for org.springframework.web.method.annotation ModelFactory initModel.

Prototype

public void initModel(NativeWebRequest request, ModelAndViewContainer container, HandlerMethod handlerMethod)
        throws Exception 

Source Link

Document

Populate the model in the following order:
  1. Retrieve "known" session attributes listed as @SessionAttributes .

    Usage

    From source file:co.paralleluniverse.springframework.web.servlet.mvc.method.annotation.FiberRequestMappingHandlerAdapter.java

    /**
     * Invoke the {@link RequestMapping} handler method preparing a {@link ModelAndView}
     * if view resolution is required./*from w  ww  .j  a  v a  2  s.  c o  m*/
     */
    private ModelAndView invokeHandleMethod(HttpServletRequest request, HttpServletResponse response,
            HandlerMethod handlerMethod) throws Exception {
        ServletWebRequest webRequest = new ServletWebRequest(request, response);
    
        WebDataBinderFactory binderFactory = getDataBinderFactory(handlerMethod);
        ModelFactory modelFactory = getModelFactory(handlerMethod, binderFactory);
        FiberServletInvocableHandlerMethod requestMappingMethod = createRequestMappingMethod(handlerMethod,
                binderFactory);
    
        ModelAndViewContainer mavContainer = new ModelAndViewContainer();
        mavContainer.addAllAttributes(RequestContextUtils.getInputFlashMap(request));
        modelFactory.initModel(webRequest, mavContainer, requestMappingMethod);
        mavContainer.setIgnoreDefaultModelOnRedirect(this.ignoreDefaultModelOnRedirect);
    
        AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response);
        asyncWebRequest.setTimeout(this.asyncRequestTimeout);
    
        final WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
        asyncManager.setTaskExecutor(this.taskExecutor);
        asyncManager.setAsyncWebRequest(asyncWebRequest);
        asyncManager.registerCallableInterceptors(this.callableInterceptors);
        asyncManager.registerDeferredResultInterceptors(this.deferredResultInterceptors);
    
        if (asyncManager.hasConcurrentResult()) {
            Object result = asyncManager.getConcurrentResult();
            mavContainer = (ModelAndViewContainer) asyncManager.getConcurrentResultContext()[0];
            asyncManager.clearConcurrentResult();
    
            if (logger.isDebugEnabled()) {
                logger.debug("Found concurrent result value [" + result + "]");
            }
            requestMappingMethod = requestMappingMethod.wrapConcurrentResult(result);
        }
    
        requestMappingMethod.invokeAndHandle(webRequest, mavContainer);
    
        if (asyncManager.isConcurrentHandlingStarted()) {
            return null;
        }
    
        return getModelAndView(mavContainer, modelFactory, webRequest);
    }
    

    From source file:org.springframework.web.method.annotation.ModelFactoryOrderingTests.java

    private void runTest(Object controller) throws Exception {
        HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
        resolvers.addResolver(new ModelAttributeMethodProcessor(false));
        resolvers.addResolver(new ModelMethodProcessor());
        WebDataBinderFactory dataBinderFactory = new DefaultDataBinderFactory(null);
    
        Class<?> type = controller.getClass();
        Set<Method> methods = MethodIntrospector.selectMethods(type, METHOD_FILTER);
        List<InvocableHandlerMethod> modelMethods = new ArrayList<>();
        for (Method method : methods) {
            InvocableHandlerMethod modelMethod = new InvocableHandlerMethod(controller, method);
            modelMethod.setHandlerMethodArgumentResolvers(resolvers);
            modelMethod.setDataBinderFactory(dataBinderFactory);
            modelMethods.add(modelMethod);/*from  www . j ava2 s  . c o  m*/
        }
        Collections.shuffle(modelMethods);
    
        SessionAttributesHandler sessionHandler = new SessionAttributesHandler(type, this.sessionAttributeStore);
        ModelFactory factory = new ModelFactory(modelMethods, dataBinderFactory, sessionHandler);
        factory.initModel(this.webRequest, this.mavContainer, new HandlerMethod(controller, "handle"));
        if (logger.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder();
            for (String name : getInvokedMethods()) {
                sb.append(" >> ").append(name);
            }
            logger.debug(sb);
        }
    }