再谈PageServlet及其应用
Roller的PageServlet类实际上是MVC模式中的Controller,只不过在此类中负责多了一项重要的功能:缓存。去掉缓存的逻辑,其实是非常符合典型的MVC模式,区别传统的MVC模式,主要是借助了volocity来渲染页面。
?
所以习惯了传统MVC模式的我,总是视图在roller中建立url和页面的映射关系。roller通过以下代码来获取页面模板:
?
// figure out what template to use ThemeTemplate page = null; // If this is a popup request, then deal with it specially // TODO: do we really need to keep supporting this? if (request.getParameter("popup") != null) { try { // Does user have a popupcomments page? page = weblog.getTheme().getTemplateByName("_popupcomments"); } catch (Exception e) { // ignored ... considered page not found } // User doesn't have one so return the default if (page == null) { page = new StaticThemeTemplate("templates/weblog/popupcomments.vm", "velocity"); } // If request specified the page, then go with that } else if ("page".equals(pageRequest.getContext())) { page = pageRequest.getWeblogPage(); // if we don't have this page then 404, we don't let // this one fall through to the default template if (page == null) { if (!response.isCommitted()) { response.reset(); } response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // If request specified tags section index, then look for custom template } else if ("tags".equals(pageRequest.getContext()) && pageRequest.getTags() == null) { try { page = weblog.getTheme().getTemplateByAction(ThemeTemplate.ACTION_TAGSINDEX); } catch (Exception e) { log.error("Error getting weblog page for action 'tagsIndex'", e); } // if we don't have a custom tags page then 404, we don't let // this one fall through to the default template if (page == null) { if (!response.isCommitted()) { response.reset(); } response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // If this is a permalink then look for a permalink template } else if (pageRequest.getWeblogAnchor() != null) { try { page = weblog.getTheme().getTemplateByAction(ThemeTemplate.ACTION_PERMALINK); } catch (Exception e) { log.error("Error getting weblog page for action 'permalink'", e); } }?
如果我们的需求改变,比如某个企业的网站需要分门别类来展现产品,那么url中可能以相应的模式来管理url,比如url中包含类别等信息,那么PageServlet应该能根据url来取得相应的页面模板。
?
比如有这样的url:/口腔产品/牙刷/u2007,那么PageServlet应该用一个产品详细信息的页面来展现,因为u2007是具体的产品。但,如果是/口腔产品/牙刷/,则应该展现“牙刷”类别下面所有的产品概要页面。
?
显然以上代码是无法满足要求的,需要改造一下。可能要通过策略模式把上面的if-else代码重构一下,以便加入新的逻辑。
?
?
?
?
?