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. //如果配置了String类型的view名字,那么就解析出来 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. //视图对象已经在ModelAndView 对象里边,直接拿来用 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() + "'"); } //不同的视图类型调用各自的render方法 view.render(mv.getModelInternal(), request, response); }
// Determine which request handle to expose to the RequestDispatcher. //判断将那个请求的处理交给RequestDispatcher HttpServletRequest requestToExpose = getRequestToExpose(request);
// Expose the model object as request attributes. //对数据进行处理,放到servletcontext中 exposeModelAsRequestAttributes(model, requestToExpose);
// Expose helpers as request attributes, if any. exposeHelpers(requestToExpose);
// Determine the path for the request dispatcher. //得到InternalResourceView定义的内部资源路径 String dispatcherPath = prepareForRendering(requestToExpose, response);
// Obtain a RequestDispatcher for the target resource (typically a JSP). //把请求转发到前面获取的资源路径中去 RequestDispatcher rd = getRequestDispatcher(requestToExpose, dispatcherPath); if (rd == null) { throw new ServletException("Could not get RequestDispatcher for [" + getUrl() + "]: Check that the corresponding file exists within your web application archive!"); }
// If already included or response already committed, perform include, else forward. if (useInclude(requestToExpose, response)) { response.setContentType(getContentType()); if (logger.isDebugEnabled()) { logger.debug("Including resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'"); } rd.include(requestToExpose, response); }
else { // Note: The forwarded resource is supposed to determine the content type itself. //转发请求到之前内部定义好的资源上去,比如jsp页面,jsp页面的展现是有容器负责,在这种情况下,view只是起到转发 //请求的作用 exposeForwardRequestAttributes(requestToExpose); if (logger.isDebugEnabled()) { logger.debug("Forwarding to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'"); } rd.forward(requestToExpose, response); } }
String path = getUrl(); if (this.preventDispatchLoop) { //从request中获取URL路径 String uri = request.getRequestURI(); if (path.startsWith("/") ? uri.equals(path) : uri.equals(StringUtils.applyRelativePath(uri, path))) { throw new ServletException("Circular view path [" + path + "]: would dispatch back " + "to the current handler URL [" + uri + "] again. Check your ViewResolver setup! " + "(Hint: This may be the result of an unspecified view, due to default view name generation.)"); } } return path; }
// Create the Excel document from the source. if (logger.isDebugEnabled()) { logger.debug("Loading Excel workbook from " + inputFile); } //创建excel文本对象 POIFSFileSystem fs = new POIFSFileSystem(inputFile.getInputStream()); return new HSSFWorkbook(fs); }
public void testPdf() throws Exception { final String text = "this should be in the PDF"; MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse();
int diffCount = 0; for (int i = 0; i < pdfContent.length; i++) { if (pdfContent[i] != baosContent[i]) { diffCount++; } } assertTrue("difference only in encryption", diffCount < 70); }