SpringMVC02DispatchServlet的执行过程8processDispatchResult()

编程

/**

* Handle the result of handler selection and handler invocation, which is

* either a ModelAndView or an Exception to be resolved to a ModelAndView.

*/

private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,

@Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv,

@Nullable Exception exception) throws Exception {

boolean errorView = false;

if (exception != null) {

if (exception instanceof ModelAndViewDefiningException) {

logger.debug("ModelAndViewDefiningException encountered", exception);

mv = ((ModelAndViewDefiningException) exception).getModelAndView();

}

else {

Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);

mv = processHandlerException(request, response, handler, exception);

errorView = (mv != null);

}

}

// Did the handler return a view to render?

if (mv != null && !mv.wasCleared()) {

render(mv, request, response);

if (errorView) {

WebUtils.clearErrorRequestAttributes(request);

}

}

else {

if (logger.isTraceEnabled()) {

logger.trace("No view rendering, null ModelAndView returned.");

}

}

if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {

// Concurrent handling started during a forward

return;

}

if (mappedHandler != null) {

mappedHandler.triggerAfterCompletion(request, response, null);

}

}

processDispatchResult()方法调用render()方法完成的对视图层的渲染,render()方法代码如下:

/**

* Render the given ModelAndView.

* <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.isTraceEnabled()) {

logger.trace("Rendering view [" + view + "] ");

}

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 + "]", ex);

}

throw ex;

}

}

 

以上是 SpringMVC02DispatchServlet的执行过程8processDispatchResult() 的全部内容, 来源链接: utcz.com/z/513241.html

回到顶部