Code example for ModelAndView

Methods: addObjectsetViewsetViewName, ModelAndView.addObject

0
	 * 'expectReply' property is true, it will also generate a response from the reply Message once received. 
	 */ 
	@Override 
	public final ModelAndView handleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse) 
			throws Exception { 
		ModelAndView modelAndView = new ModelAndView(); 
		try { 
			Message<?> replyMessage = super.doHandleRequest(servletRequest, servletResponse); 
			ServletServerHttpResponse response = new ServletServerHttpResponse(servletResponse); 
			if (replyMessage != null) { 
				Object reply = setupResponseAndConvertReply(response, replyMessage); 
				response.close(); 
				modelAndView.addObject(this.replyKey, reply); 
			} 
			else { 
				setStatusCodeIfNeeded(response); 
			} 
 
			if (this.viewExpression != null) { 
				Object view; 
				if (replyMessage != null) { 
					view = this.viewExpression.getValue(this.evaluationContext, replyMessage); 
				} 
				else { 
					view = this.viewExpression.getValue(this.evaluationContext); 
				} 
				if (view instanceof View) { 
					modelAndView.setView((View) view); 
				} 
				else if (view instanceof String) { 
					modelAndView.setViewName((String) view); 
				} 
				else { 
					throw new IllegalStateException("view expression must resolve to a View or String"); 
				} 
			} 
		} 
		catch (Exception e) { 
			MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(), "dummy"); 
			PrintWriter stackTrace = new PrintWriter(new StringWriter()); 
			e.printStackTrace(stackTrace); 
			errors.reject(errorCode, new Object[] { e, e.getMessage(), stackTrace.toString() }, 
					"A Spring Integration handler raised an exception while handling an HTTP request.  The exception is of type " 
							+ e.getClass() + " and it has a message: (" + e.getMessage() + ")"); 
			modelAndView.addObject(errorsKey, errors); 
		} 
		return modelAndView; 
	} 
 
} 
Experience pair programming with AI  Get Codota for Java