DispatcherPortlet 是Spring MVC Portlet的前端控制器:
(1)发送Portlet请求到恰当的处理器(Controller)
- protected HandlerExecutionChain getHandler(PortletRequest request)
- throws Exception
- {
- for (HandlerMapping hm : this.handlerMappings) {
- if (this.logger.isDebugEnabled()) {
- this.logger.debug(
- "Testing handler map [" + hm + "] in DispatcherPortlet with name '" + getPortletName() + "'");
- }
- HandlerExecutionChain handler = hm.getHandler(request);
- if (handler != null) {
- return handler;
- }
- }
- return null;
- }
(2) 用ViewResolver来根据视图名解析对应的要映射到的视图
- protected View resolveViewName(String viewName, Map model, PortletRequest request)
- throws Exception
- {
- for (ViewResolver viewResolver : this.viewResolvers) {
- View view = viewResolver.resolveViewName(viewName, request.getLocale());
- if (view != null) {
- return view;
- }
- }
- return null;
- }
(3) 分发请求(包含最终被解析的视图名+Model中的数据)到ViewRenderRequest来渲染视图
- protected void doRender(View view, Map model, PortletRequest request, MimeResponse response)
- throws Exception
- {
- request.setAttribute(ViewRendererServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, getPortletApplicationContext());
- request.setAttribute(ViewRendererServlet.VIEW_ATTRIBUTE, view);
- request.setAttribute(ViewRendererServlet.MODEL_ATTRIBUTE, model);
- getPortletContext().getRequestDispatcher(this.viewRendererUrl).include(request, response);
- }