jeecms 2012 源码分析(5)后台生成栏目页静态化分析
StaticPageDaoImpl 中
public int channelStatic(Integer siteId, Integer channelId,boolean containChild, Configuration conf, Map<String, Object> data)throws IOException, TemplateException {Finder finder = Finder.create("select bean from Channel bean");if (channelId != null) {if (containChild) {finder.append(",Channel parent where").append(" bean.lft between parent.lft and parent.rgt").append(" and parent.site.id=bean.site.id").append(" and parent.id=:channelId");finder.setParam("channelId", channelId);} else {finder.append(" where bean.id=:channelId");finder.setParam("channelId", channelId);}} else if (siteId != null) {finder.append(" where bean.site.id=:siteId");finder.setParam("siteId", siteId);}Session session = getSession();ScrollableResults channels = finder.createQuery(session).setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);int count = 0;CmsSite site;Channel c;PageInfo info;Writer out = null;Template tpl;String real, filename;File f, parent;int quantity, totalPage;if (data == null) {data = new HashMap<String, Object>();}while (channels.next()) {c = (Channel) channels.get(0);site = c.getSite();FrontUtils.frontData(data, site, null, null, null);// 如果是外部链接或者不需要生产静态页,则不生成if (!StringUtils.isBlank(c.getLink()) || !c.getStaticChannel()) {continue;}// 没有内容或者有子栏目,则只生成一页int childs = childsOfChannel(c.getId());if (!c.getModel().getHasContent()) {totalPage = 1;} else {if (c.getListChild()) {quantity = childs;} else {if(!c.getListChild() && childs > 0){quantity=contentsOfParentChannel(c.getId());}else{quantity = contentsOfChannel(c.getId());}}if (quantity <= 0) {totalPage = 1;} else {totalPage = (quantity - 1) / c.getPageSize() + 1;}}for (int i = 1; i <= totalPage; i++) {filename = c.getStaticFilename(i);real = realPathResolver.get(filename.toString());f = new File(real);parent = f.getParentFile();if (!parent.exists()) {parent.mkdirs();}tpl = conf.getTemplate(c.getTplChannelOrDef());String urlStatic = c.getUrlStatic(i);info = URLHelper.getPageInfo(filename.substring(filename.lastIndexOf("/")), null);FrontUtils.frontPageData(i, info.getHref(), info.getHrefFormer(), info.getHrefLatter(), data);FrontUtils.putLocation(data, urlStatic);data.put("channel", c);try {// FileWriter不能指定编码确实是个问题,只能用这个代替了。out = new OutputStreamWriter(new FileOutputStream(f), UTF8);tpl.process(data, out);log.info("create static file: {}", f.getAbsolutePath());} finally {if (out != null) {out.close();}}}if (++count % 20 == 0) {session.clear();}}return count;}新闻栏目为例
内容页:/WEB-INF/t/cms/www/red/channel/新闻栏目.html
生成的静态页面 D:\tomcat-4.0\webapps\jeecms2012\news\index.html