List of usage examples for org.springframework.web.servlet ModelAndView getView
@Nullable
public View getView()
From source file:com.mystudy.source.spring.mvc.DispatcherServlet.java
/** * Render the given ModelAndView.// w ww .java2s . co m * <p>This is the last stage in handling a request. It may involve resolving the view by name. * @param mv the ModelAndView to render * @param request current HTTP servlet request * @param response current HTTP servlet response * @throws ServletException if view is missing or cannot be resolved * @throws Exception if there's a problem rendering the view */ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception { // Determine locale for request and apply it to the response. Locale locale = this.localeResolver.resolveLocale(request); response.setLocale(locale); View view; if (mv.isReference()) { // We need to resolve the view name. view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request); if (view == null) { throw new ServletException("Could not resolve view with name '" + mv.getViewName() + "' in servlet with name '" + getServletName() + "'"); } } else { // No need to lookup: the ModelAndView object contains the actual View object. view = mv.getView(); if (view == null) { throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " + "View object in servlet with name '" + getServletName() + "'"); } } // Delegate to the View object for rendering. if (logger.isDebugEnabled()) { logger.debug("Rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'"); } view.render(mv.getModelInternal(), request, response); }
From source file:org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public ModelAndView handleActionResponse(GroovyObject controller, Object returnValue, GrailsWebRequest webRequest, Map chainModel, String closurePropertyName, String viewName) { boolean viewNameBlank = !StringUtils.hasLength(viewName); ModelAndView explicitModelAndView = (ModelAndView) controller .getProperty(ControllerDynamicMethods.MODEL_AND_VIEW_PROPERTY); if (!webRequest.isRenderView()) { return null; }/*from w w w. j a va2 s . c o m*/ if (explicitModelAndView != null) { return explicitModelAndView; } if (returnValue == null) { if (viewNameBlank) { return null; } return new ModelAndView(viewName, chainModel); } if (returnValue instanceof Map) { // remove any Proxy wrappers and set the adaptee as the value Map finalModel = new LinkedHashMap(); if (!chainModel.isEmpty()) { finalModel.putAll(chainModel); } Map returnModel = (Map) returnValue; finalModel.putAll(returnModel); removeProxiesFromModelObjects(finalModel); return new ModelAndView(viewName, finalModel); } if (returnValue instanceof ModelAndView) { ModelAndView modelAndView = (ModelAndView) returnValue; // remove any Proxy wrappers and set the adaptee as the value Map modelMap = modelAndView.getModel(); removeProxiesFromModelObjects(modelMap); if (!chainModel.isEmpty()) { modelAndView.addAllObjects(chainModel); } if (modelAndView.getView() == null && modelAndView.getViewName() == null) { if (viewNameBlank) { throw new NoViewNameDefinedException( "ModelAndView instance returned by and no view name defined by nor for closure on property [" + closurePropertyName + "] in controller [" + controller.getClass() + "]!"); } modelAndView.setViewName(viewName); } return modelAndView; } return new ModelAndView(viewName, chainModel); }
From source file:org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController.java
/** * <p>Wraps regular request and response objects into Grails request and response objects. * * <p>It can handle maps as model types next to ModelAndView instances. * * @param request HTTP request/*from w ww .j av a 2 s .com*/ * @param response HTTP response * @return the model */ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // Step 1: determine the correct URI of the request. String uri = urlPathHelper.getPathWithinApplication(request); if (LOG.isDebugEnabled()) { LOG.debug("[SimpleGrailsController] Processing request for uri [" + uri + "]"); } RequestAttributes ra = RequestContextHolder.getRequestAttributes(); Assert.state(ra instanceof GrailsWebRequest, "Bound RequestContext is not an instance of GrailsWebRequest"); GrailsWebRequest webRequest = (GrailsWebRequest) ra; ModelAndView mv = grailsControllerHelper.handleURI(uri, webRequest); if (mv != null) { if (LOG.isDebugEnabled()) { LOG.debug("[SimpleGrailsController] Forwarding model and view [" + mv + "] with class [" + (mv.getView() != null ? mv.getView().getClass().getName() : mv.getViewName()) + "]"); } } return mv; }
From source file:org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper.java
public ModelAndView handleActionResponse(GroovyObject controller, Object returnValue, String closurePropertyName, String viewName) { boolean viewNameBlank = (viewName == null || viewName.length() == 0); // reset the metaclass ModelAndView explicityModelAndView = (ModelAndView) controller .getProperty(ControllerDynamicMethods.MODEL_AND_VIEW_PROPERTY); if (!webRequest.isRenderView()) { return null; } else if (explicityModelAndView != null) { return explicityModelAndView; } else if (returnValue == null) { if (viewNameBlank) { return null; } else {//from w w w . jav a 2 s . co m Map model; if (!this.chainModel.isEmpty()) { model = new CompositeMap(this.chainModel, new BeanMap(controller)); } else { model = new BeanMap(controller); } return new ModelAndView(viewName, model); } } else if (returnValue instanceof Map) { // remove any Proxy wrappers and set the adaptee as the value Map finalModel = new LinkedHashMap(); if (!this.chainModel.isEmpty()) { finalModel.putAll(this.chainModel); } Map returnModel = (Map) returnValue; finalModel.putAll(returnModel); removeProxiesFromModelObjects(finalModel); return new ModelAndView(viewName, finalModel); } else if (returnValue instanceof ModelAndView) { ModelAndView modelAndView = (ModelAndView) returnValue; // remove any Proxy wrappers and set the adaptee as the value Map modelMap = modelAndView.getModel(); removeProxiesFromModelObjects(modelMap); if (!this.chainModel.isEmpty()) { modelAndView.addAllObjects(this.chainModel); } if (modelAndView.getView() == null && modelAndView.getViewName() == null) { if (viewNameBlank) { throw new NoViewNameDefinedException( "ModelAndView instance returned by and no view name defined by nor for closure on property [" + closurePropertyName + "] in controller [" + controller.getClass() + "]!"); } else { modelAndView.setViewName(viewName); } } return modelAndView; } else { Map model; if (!this.chainModel.isEmpty()) { model = new CompositeMap(this.chainModel, new BeanMap(controller)); } else { model = new BeanMap(controller); } return new ModelAndView(viewName, model); } }
From source file:org.shept.org.springframework.web.servlet.mvc.delegation.DelegatingController.java
/** * Processes the return value of a handler method to ensure that it either returns * <code>null</code> or an instance of {@link ModelAndView}. When returning a {@link Map}, * the {@link Map} instance is wrapped in a new {@link ModelAndView} instance. */// w w w .ja v a2 s.c o m @SuppressWarnings("unchecked") protected ModelAndView massageReturnValueIfNecessary(Object returnValue) { if (returnValue instanceof ModelAndView) { ModelAndView mav = (ModelAndView) returnValue; if (mav.getView() instanceof RedirectView) { RedirectView view = (RedirectView) mav.getView(); String fragment = ""; String url = view.getUrl(); if (StringUtils.hasText(url) && url.startsWith("#")) { // check if url begins with jump to anchor fragment = url; url = ""; } if (!StringUtils.hasText(url)) { url = getRedirectUrl(); if (StringUtils.hasText(fragment)) { url = url + fragment; } // NOTE: do NOT expose model attributes as part of the url for internal redirection mav.setView(new RedirectView(url, true, true, false)); } return mav; } else { if (!mav.hasView()) { mav.setViewName(getFormView()); } return mav; } } else if (returnValue instanceof Map) { return new ModelAndView(getFormView(), (Map) returnValue); } else if (returnValue instanceof String) { return new ModelAndView((String) returnValue); } else { // Either returned null or was 'void' return. // We'll assume that the handle method already wrote the response. return null; } }
From source file:org.springframework.integration.http.inbound.HttpRequestHandlingControllerTests.java
@Test public void requestReplyViewExpressionView() throws Exception { final View view = mock(View.class); DirectChannel requestChannel = new DirectChannel(); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override/*from ww w. j a va2 s . c o m*/ protected Message<String> handleRequestMessage(Message<?> requestMessage) { return MessageBuilder.withPayload("foo").setHeader("bar", view).build(); } }; requestChannel.subscribe(handler); HttpRequestHandlingController controller = new HttpRequestHandlingController(true); controller.setBeanFactory(mock(BeanFactory.class)); controller.setRequestChannel(requestChannel); Expression viewExpression = new SpelExpressionParser().parseExpression("headers['bar']"); controller.setViewExpression(viewExpression); controller.afterPropertiesSet(); controller.start(); MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("POST"); request.setContent("hello".getBytes()); request.setContentType("text/plain"); MockHttpServletResponse response = new MockHttpServletResponse(); ModelAndView modelAndView = controller.handleRequest(request, response); assertSame(view, modelAndView.getView()); assertEquals(1, modelAndView.getModel().size()); Object reply = modelAndView.getModel().get("reply"); assertNotNull(reply); assertEquals("foo", reply); }
From source file:org.springframework.web.portlet.DispatcherPortlet.java
/** * Render the given ModelAndView. This is the last stage in handling a request. * It may involve resolving the view by name. * @param mv the ModelAndView to render/* w w w . j a v a2s. c om*/ * @param request current portlet render request * @param response current portlet render response * @throws Exception if there's a problem rendering the view */ protected void render(ModelAndView mv, PortletRequest request, MimeResponse response) throws Exception { View view; if (mv.isReference()) { // We need to resolve the view name. view = resolveViewName(mv.getViewName(), mv.getModelInternal(), request); if (view == null) { throw new PortletException("Could not resolve view with name '" + mv.getViewName() + "' in portlet with name '" + getPortletName() + "'"); } } else { // No need to lookup: the ModelAndView object contains the actual View object. Object viewObject = mv.getView(); if (viewObject == null) { throw new PortletException("ModelAndView [" + mv + "] neither contains a view name nor a " + "View object in portlet with name '" + getPortletName() + "'"); } if (!(viewObject instanceof View)) { throw new PortletException("View object [" + viewObject + "] is not an instance of [org.springframework.web.servlet.View] - " + "DispatcherPortlet does not support any other view types"); } view = (View) viewObject; } // Set the content type on the response if needed and if possible. // The Portlet spec requires the content type to be set on the RenderResponse; // it's not sufficient to let the View set it on the ServletResponse. if (response.getContentType() != null) { if (logger.isDebugEnabled()) { logger.debug("Portlet response content type already set to [" + response.getContentType() + "]"); } } else { // No Portlet content type specified yet -> use the view-determined type. String contentType = view.getContentType(); if (contentType != null) { if (logger.isDebugEnabled()) { logger.debug( "Setting portlet response content type to view-determined type [" + contentType + "]"); } response.setContentType(contentType); } } doRender(view, mv.getModelInternal(), request, response); }
From source file:org.springframework.web.servlet.DispatcherServlet.java
/** * Render the given ModelAndView./*from ww w . j ava 2 s . c o m*/ * <p>This is the last stage in handling a request. It may involve resolving the view by name. * @param mv the ModelAndView to render * @param request current HTTP servlet request * @param response current HTTP servlet response * @throws ServletException if view is missing or cannot be resolved * @throws Exception if there's a problem rendering the view */ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception { // Determine locale for request and apply it to the response. Locale locale = (this.localeResolver != null ? this.localeResolver.resolveLocale(request) : request.getLocale()); response.setLocale(locale); View view; String viewName = mv.getViewName(); if (viewName != null) { // We need to resolve the view name. view = resolveViewName(viewName, mv.getModelInternal(), locale, request); if (view == null) { throw new ServletException("Could not resolve view with name '" + mv.getViewName() + "' in servlet with name '" + getServletName() + "'"); } } else { // No need to lookup: the ModelAndView object contains the actual View object. view = mv.getView(); if (view == null) { throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " + "View object in servlet with name '" + getServletName() + "'"); } } // Delegate to the View object for rendering. if (logger.isDebugEnabled()) { 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) { if (logger.isDebugEnabled()) { logger.debug("Error rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'", ex); } throw ex; } }
From source file:org.springframework.web.servlet.DispatcherServletMod.java
/** * Render the given ModelAndView./*from ww w . j av a 2 s .co m*/ * <p> * This is the last stage in handling a request. It may involve resolving * the view by name. * * @param mv * the ModelAndView to render * @param request * current HTTP servlet request * @param response * current HTTP servlet response * @throws ServletException * if view is missing or cannot be resolved * @throws Exception * if there's a problem rendering the view */ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception { // Determine locale for request and apply it to the response. Locale locale = this.localeResolver.resolveLocale(request); response.setLocale(locale); View view; if (mv.isReference()) { // We need to resolve the view name. view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request); if (view == null) { throw new ServletException("Could not resolve view with name '" + mv.getViewName() + "' in servlet with name '" + getServletName() + "'"); } } else { // No need to lookup: the ModelAndView object contains the actual // View object. view = mv.getView(); if (view == null) { throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " + "View object in servlet with name '" + getServletName() + "'"); } } // Delegate to the View object for rendering. if (logger.isDebugEnabled()) { logger.debug("Rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'"); } try { view.render(mv.getModelInternal(), request, response); } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug("Error rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'", ex); } throw ex; } }
From source file:org.springframework.web.servlet.LogDispatcherServlet.java
/** * Render the given ModelAndView./*from w w w . j av a 2 s . c om*/ * <p>This is the last stage in handling a request. It may involve resolving the view by name. * @param mv the ModelAndView to render * @param request current HTTP servlet request * @param response current HTTP servlet response * @throws ServletException if view is missing or cannot be resolved * @throws Exception if there's a problem rendering the view */ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception { // Determine locale for request and apply it to the response. Locale locale = this.localeResolver.resolveLocale(request); response.setLocale(locale); View view; if (mv.isReference()) { // We need to resolve the view name. view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request); if (view == null) { throw new ServletException("Could not resolve view with name '" + mv.getViewName() + "' in servlet with name '" + getServletName() + "'"); } } else { // No need to lookup: the ModelAndView object contains the actual View object. view = mv.getView(); if (view == null) { throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " + "View object in servlet with name '" + getServletName() + "'"); } } // Delegate to the View object for rendering. if (logger.isDebugEnabled()) { logger.debug("Rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'"); } try { view.render(mv.getModelInternal(), request, response); } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug("Error rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'", ex); } throw ex; } }