enderMergedOutputModel()实现中,这个留给InternalResourceView来实现。
我们看看这个实现:
复制代码
/**
* Render the internal resource given the specified model.
* This includes setting the model as request attributes.
*/
@Override
protected void renderMergedOutputModel(
Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Determine which request handle to expose to the RequestDispatcher.
HttpServletRequest requestToExpose = getRequestToExpose(request);
// Expose the model object as request attributes.
exposeModelAsRequestAttributes(model, requestToExpose);
// Expose helpers as request attributes, if any.
exposeHelpers(requestToExpose);
// Determine the path for the request dispatcher.
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.
if (logger.isDebugEnabled()) {
logger.debug("Forwarding to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
}
rd.forward(requestToExpose, response);
}
}
复制代码
流程可以归纳为以下几步:
1. 包装request,供RequestDispatcher来使用;
2. 将map中的属性和值作为属性放入包装的request;
3. 将不同实现类的helper放入包装的request中;
4. 渲染前的准备,确定request dispatcher要跳向(或者inclue)的路径
5. 获取request dispatcher。
6. 根据request中是否包含include uri属性来确实是forward或者include方法。
forward是跳向服务器的servlet, JSP文件, 或者 HTML文件。
Includes the content of a resource (servlet, JSP page,HTML file) in the response.
小结:
可以看到视图的渲染过程是把model包装成map形式通过request的属性带到服务器端。