1. 程式人生 > >springMVC controller返回值void 檢視解析器如何解析

springMVC controller返回值void 檢視解析器如何解析

當springMVC的controller層返回值為null時或者返回值為void  則在DispartcherServlet中doDispatch()方法中執行applyDefaultViewName()方法設定預設請求路徑給試圖解析

/**
 * Do we need view name translation?
 */
private void applyDefaultViewName(HttpServletRequest request, ModelAndView mv) throws Exception {
   if (mv != null && !mv.hasView()) {
      mv.setViewName(getDefaultViewName(request));
} }
/**
 * Translate the supplied request into a default view name.
 * @param request current HTTP servlet request
 * @return the view name (or {@code null} if no default found)
 * @throws Exception if view name translation failed
 */
protected String getDefaultViewName(HttpServletRequest request) throws 
Exception { return this.viewNameTranslator.getViewName(request); }
/**
 * Translates the request URI of the incoming {@link HttpServletRequest}
 * into the view name based on the configured parameters.
 * @see org.springframework.web.util.UrlPathHelper#getLookupPathForRequest
 * @see #transformPath
*/ @Override public String getViewName(HttpServletRequest request) { String lookupPath = this.urlPathHelper.getLookupPathForRequest(request); return (this.prefix + transformPath(lookupPath) + this.suffix); }