|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2012 the original author or authors. |
| 2 | + * Copyright 2002-2014 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import javax.servlet.http.HttpServletRequest;
|
20 | 20 | import javax.servlet.http.HttpServletResponse;
|
21 | 21 |
|
| 22 | +import org.springframework.http.HttpStatus; |
22 | 23 | import org.springframework.web.servlet.ModelAndView;
|
| 24 | +import org.springframework.web.servlet.View; |
23 | 25 | import org.springframework.web.servlet.support.RequestContextUtils;
|
24 | 26 |
|
25 | 27 | /**
|
26 |
| - * <p>Trivial controller that always returns a named view. The view |
27 |
| - * can be configured using an exposed configuration property. This |
28 |
| - * controller offers an alternative to sending a request straight to a view |
29 |
| - * such as a JSP. The advantage here is that the client is not exposed to |
30 |
| - * the concrete view technology but rather just to the controller URL; |
31 |
| - * the concrete view will be determined by the ViewResolver. |
32 |
| - * |
33 |
| - * <p>An alternative to the ParameterizableViewController is a |
34 |
| - * {@link org.springframework.web.servlet.mvc.multiaction.MultiActionController MultiActionController}, |
35 |
| - * which can define a variety of handler methods that just return a plain |
36 |
| - * ModelAndView instance for a given view name. |
37 |
| - * |
38 |
| - * <p><b><a name="workflow">Workflow |
39 |
| - * (<a href="AbstractController.html#workflow">and that defined by superclass</a>):</b><br> |
40 |
| - * <ol> |
41 |
| - * <li>Request is received by the controller</li> |
42 |
| - * <li>call to {@link #handleRequestInternal handleRequestInternal} which |
43 |
| - * just returns the view, named by the configuration property |
44 |
| - * {@code viewName}. Nothing more, nothing less</li> |
45 |
| - * </ol> |
46 |
| - * </p> |
47 |
| - * |
48 |
| - * <p><b><a name="config">Exposed configuration properties</a> |
49 |
| - * (<a href="AbstractController.html#config">and those defined by superclass</a>):</b><br> |
50 |
| - * <table border="1"> |
51 |
| - * <tr> |
52 |
| - * <td><b>name</b></td> |
53 |
| - * <td><b>default</b></td> |
54 |
| - * <td><b>description</b></td> |
55 |
| - * </tr> |
56 |
| - * <tr> |
57 |
| - * <td>viewName</td> |
58 |
| - * <td><i>null</i></td> |
59 |
| - * <td>the name of the view the viewResolver will use to forward to |
60 |
| - * (if this property is not set, a null view name will be returned |
61 |
| - * directing the caller to calculate the view name from the current request)</td> |
62 |
| - * </tr> |
63 |
| - * </table> |
64 |
| - * </p> |
| 28 | + * Trivial controller that always returns a pre-configured view and optionally |
| 29 | + * sets the response status code. The view and status can be configured using |
| 30 | + * the provided configuration properties. |
65 | 31 | *
|
66 | 32 | * @author Rod Johnson
|
67 | 33 | * @author Juergen Hoeller
|
68 | 34 | * @author Keith Donald
|
| 35 | + * @author Rossen Stoyanchev |
69 | 36 | */
|
70 | 37 | public class ParameterizableViewController extends AbstractController {
|
71 | 38 |
|
72 |
| - private String viewName; |
| 39 | + private Object view; |
| 40 | + |
| 41 | + private HttpStatus statusCode; |
| 42 | + |
| 43 | + private boolean statusOnly; |
73 | 44 |
|
74 | 45 |
|
75 | 46 | /**
|
76 |
| - * Set the name of the view to delegate to. |
| 47 | + * Set a view name for the ModelAndView to return, to be resolved by the |
| 48 | + * DispatcherServlet via a ViewResolver. Will override any pre-existing |
| 49 | + * view name or View. |
77 | 50 | */
|
78 | 51 | public void setViewName(String viewName) {
|
79 |
| - this.viewName = viewName; |
| 52 | + this.view = viewName; |
80 | 53 | }
|
81 | 54 |
|
82 | 55 | /**
|
83 |
| - * Return the name of the view to delegate to. |
| 56 | + * Return the name of the view to delegate to, or {@code null} if using a |
| 57 | + * View instance. |
84 | 58 | */
|
85 | 59 | public String getViewName() {
|
86 |
| - return this.viewName; |
| 60 | + return (this.view instanceof String ? (String) this.view : null); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Set a View object for the ModelAndView to return. |
| 65 | + * Will override any pre-existing view name or View. |
| 66 | + * @since 4.1 |
| 67 | + */ |
| 68 | + public void setView(View view) { |
| 69 | + this.view = view; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Return the View object, or {@code null} if we are using a view name |
| 74 | + * to be resolved by the DispatcherServlet via a ViewResolver. |
| 75 | + * @since 4.1 |
| 76 | + */ |
| 77 | + public View getView() { |
| 78 | + return (this.view instanceof View ? (View) this.view : null); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Configure the HTTP status code that this controller should set on the |
| 83 | + * response. |
| 84 | + * |
| 85 | + * <p>When a "redirect:" prefixed view name is configured, there is no need |
| 86 | + * to set this property since RedirectView will do that. However this property |
| 87 | + * may still be used to override the 3xx status code of {@code RedirectView}. |
| 88 | + * For full control over redirecting provide a {@code RedirectView} instance. |
| 89 | + * |
| 90 | + * <p>If the status code is 204 and no view is configured, the request is |
| 91 | + * fully handled within the controller. |
| 92 | + * |
| 93 | + * @since 4.1 |
| 94 | + */ |
| 95 | + public void setStatusCode(HttpStatus statusCode) { |
| 96 | + this.statusCode = statusCode; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Return the configured HTTP status code or {@code null}. |
| 101 | + * @since 4.1 |
| 102 | + */ |
| 103 | + public HttpStatus getStatusCode() { |
| 104 | + return this.statusCode; |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + /** |
| 109 | + * The property can be used to indicate the request is considered fully |
| 110 | + * handled within the controller and that no view should be used for rendering. |
| 111 | + * Useful in combination with {@link #setStatusCode}. |
| 112 | + * <p>By default this is set to {@code false}. |
| 113 | + * @since 4.1 |
| 114 | + */ |
| 115 | + public void setStatusOnly(boolean statusOnly) { |
| 116 | + this.statusOnly = statusOnly; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Whether the request is fully handled within the controller. |
| 121 | + */ |
| 122 | + public boolean isStatusOnly() { |
| 123 | + return this.statusOnly; |
87 | 124 | }
|
88 | 125 |
|
89 | 126 | /**
|
90 | 127 | * Return a ModelAndView object with the specified view name.
|
91 |
| - * The content of {@link RequestContextUtils#getInputFlashMap} is also added to the model. |
| 128 | + * |
| 129 | + * <p>The content of the {@link RequestContextUtils#getInputFlashMap |
| 130 | + * "input" FlashMap} is also added to the model. |
| 131 | + * |
92 | 132 | * @see #getViewName()
|
93 | 133 | */
|
94 | 134 | @Override
|
95 | 135 | protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
|
96 | 136 | throws Exception {
|
97 |
| - return new ModelAndView(getViewName(), RequestContextUtils.getInputFlashMap(request)); |
| 137 | + |
| 138 | + String viewName = getViewName(); |
| 139 | + |
| 140 | + if (getStatusCode() != null) { |
| 141 | + if (getStatusCode().is3xxRedirection()) { |
| 142 | + request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, getStatusCode()); |
| 143 | + viewName = (viewName != null && !viewName.startsWith("redirect:") ? "redirect:" + viewName : viewName); |
| 144 | + } |
| 145 | + else { |
| 146 | + response.setStatus(getStatusCode().value()); |
| 147 | + if (isStatusOnly() || (getStatusCode().equals(HttpStatus.NO_CONTENT) && getViewName() == null)) { |
| 148 | + return null; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + ModelAndView modelAndView = new ModelAndView(); |
| 154 | + modelAndView.addAllObjects(RequestContextUtils.getInputFlashMap(request)); |
| 155 | + |
| 156 | + if (getViewName() != null) { |
| 157 | + modelAndView.setViewName(viewName); |
| 158 | + } |
| 159 | + else { |
| 160 | + modelAndView.setView(getView()); |
| 161 | + } |
| 162 | + |
| 163 | + return (isStatusOnly() ? null : modelAndView); |
98 | 164 | }
|
99 | 165 |
|
100 | 166 | }
|
0 commit comments