设为首页 加入收藏

TOP

spring mvc DispatcherServlet详解之---视图渲染过程(三)
2014-11-23 17:33:35 】 浏览:825
Tags:spring mvc DispatcherServlet 详解 --- 渲染 过程
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的属性带到服务器端。
首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java数据结构系类之――链表(1).. 下一篇[Nhibernate]SchemaExport工具的..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目