Add HttpStatus to ModelAndView
Issue: SPR-13560
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -1240,6 +1240,9 @@ public class DispatcherServlet extends FrameworkServlet {
|
||||
logger.debug("Rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'");
|
||||
}
|
||||
try {
|
||||
if (mv.getStatus() != null) {
|
||||
response.setStatus(mv.getStatus().value());
|
||||
}
|
||||
view.render(mv.getModelInternal(), request, response);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.web.servlet;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
@@ -36,6 +37,7 @@ import org.springframework.util.CollectionUtils;
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Rob Harrop
|
||||
* @author Rossen Stoyanchev
|
||||
* @see DispatcherServlet
|
||||
* @see ViewResolver
|
||||
* @see HandlerAdapter#handle
|
||||
@@ -49,6 +51,9 @@ public class ModelAndView {
|
||||
/** Model Map */
|
||||
private ModelMap model;
|
||||
|
||||
/** Optional status for the response */
|
||||
private HttpStatus status;
|
||||
|
||||
/** Indicates whether or not this instance has been cleared with a call to {@link #clear()} */
|
||||
private boolean cleared = false;
|
||||
|
||||
@@ -115,6 +120,24 @@ public class ModelAndView {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates new ModelAndView given a view name, model, and status.
|
||||
* @param viewName name of the View to render, to be resolved
|
||||
* by the DispatcherServlet's ViewResolver
|
||||
* @param model Map of model names (Strings) to model objects
|
||||
* (Objects). Model entries may not be {@code null}, but the
|
||||
* model Map may be {@code null} if there is no model data.
|
||||
* @param status an alternative status code to use for the response.
|
||||
*/
|
||||
public ModelAndView(String viewName, Map<String, ?> model, HttpStatus status) {
|
||||
this.view = viewName;
|
||||
if (model != null) {
|
||||
getModelMap().addAllAttributes(model);
|
||||
}
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenient constructor to take a single model object.
|
||||
* @param viewName name of the View to render, to be resolved
|
||||
@@ -215,6 +238,21 @@ public class ModelAndView {
|
||||
return getModelMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the status to use for the response.
|
||||
* @since 4.3
|
||||
*/
|
||||
public void setStatus(HttpStatus status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configured status for the response.
|
||||
*/
|
||||
public HttpStatus getStatus() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add an attribute to the model.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -98,6 +98,7 @@ public class ModelAndViewMethodReturnValueHandler implements HandlerMethodReturn
|
||||
}
|
||||
}
|
||||
}
|
||||
mavContainer.setStatus(mav.getStatus());
|
||||
mavContainer.addAllAttributes(mav.getModel());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -916,7 +916,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
|
||||
return null;
|
||||
}
|
||||
ModelMap model = mavContainer.getModel();
|
||||
ModelAndView mav = new ModelAndView(mavContainer.getViewName(), model);
|
||||
ModelAndView mav = new ModelAndView(mavContainer.getViewName(), model, mavContainer.getStatus());
|
||||
if (!mavContainer.isViewReference()) {
|
||||
mav.setView((View) mavContainer.getView());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user