org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver Java Examples

The following examples show how to use org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void mavResolver() throws ServletException, IOException {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			ModelAndViewResolver[] mavResolvers = new ModelAndViewResolver[] {new MyModelAndViewResolver()};
			adapterDef.getPropertyValues().add("modelAndViewResolvers", mavResolvers);
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, ModelAndViewResolverController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("myValue", response.getContentAsString());

}
 
Example #2
Source File: JsonBodyExceptionResolver.java    From rocket-console with Apache License 2.0 6 votes vote down vote up
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
                                          Object _handler, Exception ex) {
    HandlerMethod handler = (HandlerMethod) _handler;
    if (handler == null) {
        return null;
    }
    Method method = handler.getMethod();
    if (method.isAnnotationPresent(JsonBody.class) && ex != null) {
        logger.error("server is error", ex);
        Object value = null;
        if (ex instanceof ServiceException) {
            value = new JsonResult<Object>(((ServiceException) ex).getCode(), ex.getMessage());
        } else {
            value = new JsonResult<Object>(-1, ex.getMessage());
        }
        try {
            JsonUtil.writeValue(response.getWriter(), value);
        } catch (IOException e) {
            Throwables.propagateIfPossible(e);
        }
        return ModelAndViewResolver.UNRESOLVED;
    }
    return null;
}
 
Example #3
Source File: ModelAndViewResolverMethodReturnValueHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public ModelAndView resolveModelAndView(Method method, Class handlerType, Object returnValue,
		ExtendedModelMap model, NativeWebRequest request) {
	if (returnValue != null && returnValue.getClass().equals(returnValueType)) {
		return new ModelAndView("viewName", "modelAttrName", returnValue);
	}
	else {
		return ModelAndViewResolver.UNRESOLVED;
	}
}
 
Example #4
Source File: ModelAndViewResolverMethodReturnValueHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	mavResolvers = new ArrayList<ModelAndViewResolver>();
	handler = new ModelAndViewResolverMethodReturnValueHandler(mavResolvers);
	mavContainer = new ModelAndViewContainer();
	request = new ServletWebRequest(new MockHttpServletRequest());
}
 
Example #5
Source File: ModelAndViewResolverMethodReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (this.mavResolvers != null) {
		for (ModelAndViewResolver mavResolver : this.mavResolvers) {
			Class<?> handlerType = returnType.getContainingClass();
			Method method = returnType.getMethod();
			Assert.state(method != null, "No handler method");
			ExtendedModelMap model = (ExtendedModelMap) mavContainer.getModel();
			ModelAndView mav = mavResolver.resolveModelAndView(method, handlerType, returnValue, model, webRequest);
			if (mav != ModelAndViewResolver.UNRESOLVED) {
				mavContainer.addAllAttributes(mav.getModel());
				mavContainer.setViewName(mav.getViewName());
				if (!mav.isReference()) {
					mavContainer.setView(mav.getView());
				}
				return;
			}
		}
	}

	// No suitable ModelAndViewResolver...
	if (this.modelAttributeProcessor.supportsReturnType(returnType)) {
		this.modelAttributeProcessor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
	}
	else {
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example #6
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void mavResolver() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		ModelAndViewResolver[] mavResolvers = new ModelAndViewResolver[] {new MyModelAndViewResolver()};
		adapterDef.getPropertyValues().add("modelAndViewResolvers", mavResolvers);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, ModelAndViewResolverController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("myValue", response.getContentAsString());

}
 
Example #7
Source File: ModelAndViewResolverMethodReturnValueHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public ModelAndView resolveModelAndView(Method method, Class<?> handlerType, Object returnValue,
		ExtendedModelMap model, NativeWebRequest request) {

	if (returnValue != null && returnValue.getClass().equals(returnValueType)) {
		return new ModelAndView("viewName", "modelAttrName", returnValue);
	}
	else {
		return ModelAndViewResolver.UNRESOLVED;
	}
}
 
Example #8
Source File: ModelAndViewResolverMethodReturnValueHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (this.mavResolvers != null) {
		for (ModelAndViewResolver mavResolver : this.mavResolvers) {
			Class<?> handlerType = returnType.getContainingClass();
			Method method = returnType.getMethod();
			ExtendedModelMap model = (ExtendedModelMap) mavContainer.getModel();
			ModelAndView mav = mavResolver.resolveModelAndView(method, handlerType, returnValue, model, webRequest);
			if (mav != ModelAndViewResolver.UNRESOLVED) {
				mavContainer.addAllAttributes(mav.getModel());
				mavContainer.setViewName(mav.getViewName());
				if (!mav.isReference()) {
					mavContainer.setView(mav.getView());
				}
				return;
			}
		}
	}

	// No suitable ModelAndViewResolver...
	if (this.modelAttributeProcessor.supportsReturnType(returnType)) {
		this.modelAttributeProcessor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
	}
	else {
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example #9
Source File: ModelAndViewResolverMethodReturnValueHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (this.mavResolvers != null) {
		for (ModelAndViewResolver mavResolver : this.mavResolvers) {
			Class<?> handlerType = returnType.getContainingClass();
			Method method = returnType.getMethod();
			Assert.state(method != null, "No handler method");
			ExtendedModelMap model = (ExtendedModelMap) mavContainer.getModel();
			ModelAndView mav = mavResolver.resolveModelAndView(method, handlerType, returnValue, model, webRequest);
			if (mav != ModelAndViewResolver.UNRESOLVED) {
				mavContainer.addAllAttributes(mav.getModel());
				mavContainer.setViewName(mav.getViewName());
				if (!mav.isReference()) {
					mavContainer.setView(mav.getView());
				}
				return;
			}
		}
	}

	// No suitable ModelAndViewResolver...
	if (this.modelAttributeProcessor.supportsReturnType(returnType)) {
		this.modelAttributeProcessor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
	}
	else {
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example #10
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void mavResolver() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		ModelAndViewResolver[] mavResolvers = new ModelAndViewResolver[] {new MyModelAndViewResolver()};
		adapterDef.getPropertyValues().add("modelAndViewResolvers", mavResolvers);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, ModelAndViewResolverController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("myValue", response.getContentAsString());

}
 
Example #11
Source File: ModelAndViewResolverMethodReturnValueHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public ModelAndView resolveModelAndView(Method method, Class<?> handlerType, Object returnValue,
		ExtendedModelMap model, NativeWebRequest request) {

	if (returnValue != null && returnValue.getClass().equals(returnValueType)) {
		return new ModelAndView("viewName", "modelAttrName", returnValue);
	}
	else {
		return ModelAndViewResolver.UNRESOLVED;
	}
}
 
Example #12
Source File: ModelAndViewResolverMethodReturnValueHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (this.mavResolvers != null) {
		for (ModelAndViewResolver mavResolver : this.mavResolvers) {
			Class<?> handlerType = returnType.getContainingClass();
			Method method = returnType.getMethod();
			ExtendedModelMap model = (ExtendedModelMap) mavContainer.getModel();
			ModelAndView mav = mavResolver.resolveModelAndView(method, handlerType, returnValue, model, webRequest);
			if (mav != ModelAndViewResolver.UNRESOLVED) {
				mavContainer.addAllAttributes(mav.getModel());
				mavContainer.setViewName(mav.getViewName());
				if (!mav.isReference()) {
					mavContainer.setView(mav.getView());
				}
				return;
			}
		}
	}

	// No suitable ModelAndViewResolver...
	if (this.modelAttributeProcessor.supportsReturnType(returnType)) {
		this.modelAttributeProcessor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
	}
	else {
		throw new UnsupportedOperationException("Unexpected return type: " +
				returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
	}
}
 
Example #13
Source File: RequestMappingHandlerAdapter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return the configured {@link ModelAndViewResolver}s, or {@code null}.
 */
public List<ModelAndViewResolver> getModelAndViewResolvers() {
	return this.modelAndViewResolvers;
}
 
Example #14
Source File: ModelAndViewResolverMethodReturnValueHandler.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new instance.
 */
public ModelAndViewResolverMethodReturnValueHandler(List<ModelAndViewResolver> mavResolvers) {
	this.mavResolvers = mavResolvers;
}
 
Example #15
Source File: RequestMappingHandlerAdapter.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Return the configured {@link ModelAndViewResolver}s, or {@code null}.
 */
public List<ModelAndViewResolver> getModelAndViewResolvers() {
	return modelAndViewResolvers;
}
 
Example #16
Source File: ModelAndViewResolverMethodReturnValueHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create a new instance.
 */
public ModelAndViewResolverMethodReturnValueHandler(List<ModelAndViewResolver> mavResolvers) {
	this.mavResolvers = mavResolvers;
}
 
Example #17
Source File: ModelAndViewResolverMethodReturnValueHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Create a new instance.
 */
public ModelAndViewResolverMethodReturnValueHandler(@Nullable List<ModelAndViewResolver> mavResolvers) {
	this.mavResolvers = mavResolvers;
}
 
Example #18
Source File: RequestMappingHandlerAdapter.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Return the configured {@link ModelAndViewResolver ModelAndViewResolvers}, or {@code null}.
 */
@Nullable
public List<ModelAndViewResolver> getModelAndViewResolvers() {
	return this.modelAndViewResolvers;
}
 
Example #19
Source File: ModelAndViewResolverMethodReturnValueHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Create a new instance.
 */
public ModelAndViewResolverMethodReturnValueHandler(@Nullable List<ModelAndViewResolver> mavResolvers) {
	this.mavResolvers = mavResolvers;
}
 
Example #20
Source File: RequestMappingHandlerAdapter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Return the configured {@link ModelAndViewResolver ModelAndViewResolvers}, or {@code null}.
 */
@Nullable
public List<ModelAndViewResolver> getModelAndViewResolvers() {
	return this.modelAndViewResolvers;
}
 
Example #21
Source File: RequestMappingHandlerAdapter.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Provide custom {@link ModelAndViewResolver ModelAndViewResolvers}.
 * <p><strong>Note:</strong> This method is available for backwards
 * compatibility only. However, it is recommended to re-write a
 * {@code ModelAndViewResolver} as {@link HandlerMethodReturnValueHandler}.
 * An adapter between the two interfaces is not possible since the
 * {@link HandlerMethodReturnValueHandler#supportsReturnType} method
 * cannot be implemented. Hence {@code ModelAndViewResolver}s are limited
 * to always being invoked at the end after all other return value
 * handlers have been given a chance.
 * <p>A {@code HandlerMethodReturnValueHandler} provides better access to
 * the return type and controller method information and can be ordered
 * freely relative to other return value handlers.
 */
public void setModelAndViewResolvers(@Nullable List<ModelAndViewResolver> modelAndViewResolvers) {
	this.modelAndViewResolvers = modelAndViewResolvers;
}
 
Example #22
Source File: AnnotationMethodHandlerAdapter.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Set a custom ModelAndViewResolvers to use for special method return types.
 * Such a custom ModelAndViewResolver will kick in first, having a chance to
 * resolve an return value before the standard ModelAndView handling kicks in.
 */
public void setCustomModelAndViewResolver(ModelAndViewResolver customModelAndViewResolver) {
	this.customModelAndViewResolvers = new ModelAndViewResolver[]{customModelAndViewResolver};
}
 
Example #23
Source File: AnnotationMethodHandlerAdapter.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Set one or more custom ModelAndViewResolvers to use for special method return types.
 * Any such custom ModelAndViewResolver will kick in first, having a chance to
 * resolve an return value before the standard ModelAndView handling kicks in.
 */
public void setCustomModelAndViewResolvers(ModelAndViewResolver... customModelAndViewResolvers) {
	this.customModelAndViewResolvers = customModelAndViewResolvers;
}
 
Example #24
Source File: RequestMappingHandlerAdapter.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Provide custom {@link ModelAndViewResolver}s.
 * <p><strong>Note:</strong> This method is available for backwards
 * compatibility only. However, it is recommended to re-write a
 * {@code ModelAndViewResolver} as {@link HandlerMethodReturnValueHandler}.
 * An adapter between the two interfaces is not possible since the
 * {@link HandlerMethodReturnValueHandler#supportsReturnType} method
 * cannot be implemented. Hence {@code ModelAndViewResolver}s are limited
 * to always being invoked at the end after all other return value
 * handlers have been given a chance.
 * <p>A {@code HandlerMethodReturnValueHandler} provides better access to
 * the return type and controller method information and can be ordered
 * freely relative to other return value handlers.
 */
public void setModelAndViewResolvers(List<ModelAndViewResolver> modelAndViewResolvers) {
	this.modelAndViewResolvers = modelAndViewResolvers;
}
 
Example #25
Source File: RequestMappingHandlerAdapter.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Provide custom {@link ModelAndViewResolver}s.
 * <p><strong>Note:</strong> This method is available for backwards
 * compatibility only. However, it is recommended to re-write a
 * {@code ModelAndViewResolver} as {@link HandlerMethodReturnValueHandler}.
 * An adapter between the two interfaces is not possible since the
 * {@link HandlerMethodReturnValueHandler#supportsReturnType} method
 * cannot be implemented. Hence {@code ModelAndViewResolver}s are limited
 * to always being invoked at the end after all other return value
 * handlers have been given a chance.
 * <p>A {@code HandlerMethodReturnValueHandler} provides better access to
 * the return type and controller method information and can be ordered
 * freely relative to other return value handlers.
 */
public void setModelAndViewResolvers(List<ModelAndViewResolver> modelAndViewResolvers) {
	this.modelAndViewResolvers = modelAndViewResolvers;
}
 
Example #26
Source File: RequestMappingHandlerAdapter.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Provide custom {@link ModelAndViewResolver ModelAndViewResolvers}.
 * <p><strong>Note:</strong> This method is available for backwards
 * compatibility only. However, it is recommended to re-write a
 * {@code ModelAndViewResolver} as {@link HandlerMethodReturnValueHandler}.
 * An adapter between the two interfaces is not possible since the
 * {@link HandlerMethodReturnValueHandler#supportsReturnType} method
 * cannot be implemented. Hence {@code ModelAndViewResolver}s are limited
 * to always being invoked at the end after all other return value
 * handlers have been given a chance.
 * <p>A {@code HandlerMethodReturnValueHandler} provides better access to
 * the return type and controller method information and can be ordered
 * freely relative to other return value handlers.
 */
public void setModelAndViewResolvers(@Nullable List<ModelAndViewResolver> modelAndViewResolvers) {
	this.modelAndViewResolvers = modelAndViewResolvers;
}