今天想在给Java客二级版面加入栏目文章统计效果,如下图,

查看JEECMS的源代码发现开发者版本还没有类似现成的统计标签,一种解决的办法是使用现有的JEECMS标签,像这样Struts( [@cms_content_list channel=id]${tag_list?size}[/@cms_content_list]) ,但是这样的做法非常地低效,原因是[@cms_content_list]标签会把所有当前栏目的文章内容对象查询出来,做全表查询!

没办法啊!!!为了网站访问效率,只好自己写一个统计标签吧。那下面就以[@cms_channel_statistic]为例说下如何在JEECMS加入自定义标签。

第一步:编写装载统计信息的Entity。

/**
* 频道统计实体类
* @author www.javake.net
*/
public class ChannelStatistics {
/**
* 文章总数
*/
private long contentAllCount;
/**
* 待审核文章总数
*/
private long contentCheckingCount;
/**
* 评论总数
*/
private long commentCount;
/**
* 阅读总数
*/
private long viewCount;
public long getContentAllCount() {
return contentAllCount;
}
public void setContentAllCount(long contentAllCount) {
this.contentAllCount = contentAllCount;
}
public long getContentCheckingCount() {
return contentCheckingCount;
}
public void setContentCheckingCount(long contentCheckingCount) {
this.contentCheckingCount = contentCheckingCount;
}
public long getCommentCount() {
return commentCount;
}
public void setCommentCount(long commentCount) {
this.commentCount = commentCount;
}
public long getViewCount() {
return viewCount;
}
public void setViewCount(long viewCount) {
this.viewCount = viewCount;
}
}

第二步:编写栏目信息统计的Dao接口。暂时只实现文章总数统计,待审核文章统计,评论总数。

/**
* 栏目信息统计Dao接口
* @author www.javake.net
*/
public interface CmsChannelStatisticDao {
/**
* 当前栏目文章统计
* @param restrictions
* @return
*/
public long contentStatistic(Map<String, Object> restrictions);
/**
* 当前栏目待审核文章统计
* @param restrictions
* @return
*/
public long contentCheckingStatistic(Map<String, Object> restrictions);
/**
* 当前栏目评论统计
* @param restrictions
* @return
*/
public long commentStatistic(Map<String, Object> restrictions);
}

第三步:编写Dao接口的实现。

/**
* 栏目信息统计Dao实现类
* @author www.javake.net
*/
import static com.jeecms.cms.entity.main.Content.ContentStatus.passed;
import static com.jeecms.cms.entity.main.Content.ContentStatus.prepared;
import static com.jeecms.cms.entity.main.Content.ContentStatus.rejected;
import static com.jeecms.cms.statistic.CmsStatistic.CHANNELID;
import static com.jeecms.cms.statistic.CmsStatistic.ISREPLYED;
import static com.jeecms.cms.statistic.CmsStatistic.SITEID;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.jeecms.cms.entity.main.Content.ContentStatus;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateSimpleDao;
@Repository
public class CmsChannelStatisticDaoImpl extends HibernateSimpleDao
implements CmsChannelStatisticDao{
/**
* 获取文章总数
*/
public long contentStatistic(Map<String, Object> restrictions) {
Finder f = createCacheableFinder("select count(*) from Content bean");
Integer channelId = (Integer) restrictions.get(CHANNELID);
if (channelId != null) {
f.append(" join bean.channel channel,Channel parent");
f.append(" where channel.lft between parent.lft and parent.rgt");
f.append(" and channel.site.id=parent.site.id");
f.append(" and parent.id=:parentId");
f.setParam("parentId", channelId);
} else {
f.append(" where bean.site.id=:siteId").setParam("siteId",
restrictions.get(SITEID));
}
return (Long) find(f).iterator().next();
}

private long contentStatistic(Map<String, Object> restrictions,ContentStatus status) {
Finder f = createCacheableFinder("select count(*) from Content bean");
if (prepared == status || passed == status || rejected == status) {
f.append(" join bean.contentCheckSet check");
}
Integer channelId = (Integer) restrictions.get(CHANNELID);
if (channelId != null) {
f.append(" join bean.channel channel,Channel parent");
f.append(" where channel.lft between parent.lft and parent.rgt");
f.append(" and channel.site.id=parent.site.id");
f.append(" and parent.id=:parentId");
f.setParam("parentId", channelId);
} else {
f.append(" where bean.site.id=:siteId").setParam("siteId",
restrictions.get(SITEID));
}
if (prepared == status || passed == status) {
f.append(" and check.rejected=false");
} else if (rejected == status) {
f.append(" and check.rejected=true");
}
return (Long) find(f).iterator().next();
}

/**
* 待审核文章总数
* @param restrictions
* @param status
* @return
*/
public long contentCheckingStatistic(Map<String, Object> restrictions) {
return contentStatistic(restrictions,ContentStatus.prepared);
}
public long commentStatistic(Map<String, Object> restrictions) {
Finder f = createCacheableFinder("select count(*) from CmsComment bean ");
Integer channelId = (Integer) restrictions.get(CHANNELID);
if (channelId != null) {
f.append(" join bean.channel channel,Channel parent");
f.append(" where channel.lft between parent.lft and parent.rgt");
f.append(" and channel.site.id=parent.site.id");
f.append(" and parent.id=:parentId");
f.setParam("parentId", channelId);
} else {
f.append(" where bean.site.id=:siteId").setParam("siteId",
restrictions.get(SITEID));
}
Boolean isReplyed = (Boolean) restrictions.get(ISREPLYED);
if (isReplyed != null) {
if (isReplyed) {
f.append(" and bean.replayTime is not null");
} else {
f.append(" and bean.replayTime is null");
}
}
return (Long) find(f).iterator().next();
}

private Finder createCacheableFinder(String hql) {
Finder finder = Finder.create(hql);
finder.setCacheable(true);
return finder;
}
}

第四步:编写栏目统计的FreeMarker标签类。这里可以输入两个参数,一个是id(栏目id),一个是siteId(站点id)。这两个参数可在使用标签的时候输入。

/**
* 栏目统计
* @author www.javake.net
*/
import static com.jeecms.common.web.freemarker.DirectiveUtils.OUT_BEAN;
import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;
import static com.jeecms.cms.statistic.CmsStatistic.SITEID;
import static com.jeecms.cms.statistic.CmsStatistic.ISREPLYED;
import static com.jeecms.cms.statistic.CmsStatistic.USERID;
import static com.jeecms.cms.statistic.CmsStatistic.CHANNELID;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.jeecms.cms.entity.main.ChannelStatistics;
import com.jeecms.cms.entity.main.CmsSite;
import com.jeecms.cms.statistic.CmsChannelStatisticDao;
import com.jeecms.cms.web.FrontUtils;
import com.jeecms.common.web.freemarker.DirectiveUtils;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
public class CmsChannelStatisticsDirective implements TemplateDirectiveModel{
/**
* 输入参数,站点ID。存在时,获取该站点栏目,不存在时获取当前站点栏目。
*/
public static final String PARAM_SITE_ID = "siteId";
/**
* 输入参数,栏目ID。
*/
public static final String PARAM_ID = "id";
@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
CmsSite site = FrontUtils.getSite(env);
Integer id = DirectiveUtils.getInt(PARAM_ID, params);
ChannelStatistics statistics = null;
Map<String,Object> restrictions = new HashMap<String,Object>();
Integer siteId = DirectiveUtils.getInt(PARAM_SITE_ID, params);
if (siteId == null) {
siteId = site.getId();
}
if (id != null ) {
restrictions.put(CHANNELID, id);
} else {
restrictions.put(SITEID, siteId);
}
long contentCount = channelSatistic.contentStatistic(restrictions);
long contentCheckingCount = channelSatistic.contentCheckingStatistic(restrictions);
long commentCount = channelSatistic.commentStatistic(restrictions);

statistics = new ChannelStatistics();
statistics.setCommentCount(commentCount);
statistics.setContentAllCount(contentCount);
statistics.setContentCheckingCount(contentCheckingCount);

Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
params);
paramWrap.put(OUT_BEAN, DEFAULT_WRAPPER.wrap(statistics));
Map<String, TemplateModel> origMap = DirectiveUtils
.addParamsToVariable(env, paramWrap);
body.render(env.getOut());
DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
}

@Autowired
private CmsChannelStatisticDao channelSatistic;

public void setChannelSatistic(CmsChannelStatisticDao channelSatistic) {
this.channelSatistic = channelSatistic;
}
}

第五步:在jeecms-context.xml文件中加入CmsChannelStatisticsDirective标签类的bean注入代码。

<!— Author:www.javake.net -->

<bean id="cms_lucene_page"class="com.jeecms.cms.lucene.LuceneDirectivePage"/>

<bean id="cms_advertising"class="com.jeecms.cms.action.directive.CmsAdvertisingDirective"/>

<bean id="cms_channel_statistic" class="com.jeecms.cms.action.directive.CmsChannelStatisticsDirective"/>

<!— Author:www.javake.net -->

<property name="freemarkerVariables">

<map>

<entry key="uuid"value-ref="uuid"/>

<entry key="process_time"value-ref="process_time"/>

<entry key="text_cut"value-ref="text_cut"/>

<entry key="html_cut"value-ref="html_cut"/>

<entry key="cms_pagination"value-ref="cms_pagination"/>

<entry key="cms_channel_list"value-ref="cms_channel_list"/>

<entry key="cms_channel_page"value-ref="cms_channel_page"/>

<entry key="cms_channel"value-ref="cms_channel"/>

<entry key="cms_content"value-ref="cms_content"/>

<entry key="cms_content_list"value-ref="cms_content_list"/>

<entry key="cms_content_page"value-ref="cms_content_page"/>

<entry key="cms_tag_list"value-ref="cms_tag_list"/>

<entry key="cms_tag_page"value-ref="cms_tag_page"/>

<entry key="cms_topic_list"value-ref="cms_topic_list"/>

<entry key="cms_topic_page"value-ref="cms_topic_page"/>

<entry key="cms_comment_list"value-ref="cms_comment_list"/>

<entry key="cms_comment_page"value-ref="cms_comment_page"/>

<entry key="cms_guestbook_ctg_list"value-ref="cms_guestbook_ctg_list"/>

<entry key="cms_guestbook_list"value-ref="cms_guestbook_list"/>

<entry key="cms_guestbook_page"value-ref="cms_guestbook_page"/>

<entry key="cms_vote"value-ref="cms_vote"/>

<entry key="cms_friendlink_ctg_list"value-ref="cms_friendlink_ctg_list"/>

<entry key="cms_friendlink_list"value-ref="cms_friendlink_list"/>

<entry key="cms_lucene_list"value-ref="cms_lucene_list"/>

<entry key="cms_lucene_page"value-ref="cms_lucene_page"/>

<entry key="cms_advertising"value-ref="cms_advertising"/>

<entry key="cms_channel_statistic" value-ref="cms_channel_statistic"/>

</map>

</property>

第六步:在jeecms-servlet-front.xml文件中配置

<!— Author:www.javake.net -->

<bean id="freemarkerConfig"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

<property name="freemarkerVariables">

<map>

<entry key="uuid"value-ref="uuid"/>

<entry key="process_time"value-ref="process_time"/>

<entry key="text_cut"value-ref="text_cut"/>

<entry key="html_cut"value-ref="html_cut"/>

<entry key="cms_pagination"value-ref="cms_pagination"/>

<entry key="cms_channel_list"value-ref="cms_channel_list"/>

<entry key="cms_channel_page"value-ref="cms_channel_page"/>

<entry key="cms_channel"value-ref="cms_channel"/>

<entry key="cms_content"value-ref="cms_content"/>

<entry key="cms_content_list"value-ref="cms_content_list"/>

<entry key="cms_content_page"value-ref="cms_content_page"/>

<entry key="cms_tag_list"value-ref="cms_tag_list"/>

<entry key="cms_tag_page"value-ref="cms_tag_page"/>

<entry key="cms_topic_list"value-ref="cms_topic_list"/>

<entry key="cms_topic_page"value-ref="cms_topic_page"/>

<entry key="cms_comment_list"value-ref="cms_comment_list"/>

<entry key="cms_comment_page"value-ref="cms_comment_page"/>

<entry key="cms_guestbook_ctg_list"value-ref="cms_guestbook_ctg_list"/>

<entry key="cms_guestbook_list"value-ref="cms_guestbook_list"/>

<entry key="cms_guestbook_page"value-ref="cms_guestbook_page"/>

<entry key="cms_vote"value-ref="cms_vote"/>

<entry key="cms_lucene_list"value-ref="cms_lucene_list"/>

<entry key="cms_lucene_page"value-ref="cms_lucene_page"/>

<entry key="cms_friendlink_ctg_list"value-ref="cms_friendlink_ctg_list"/>

<entry key="cms_friendlink_list"value-ref="cms_friendlink_list"/>

<entry key="cms_advertising"value-ref="cms_advertising"/>

<entry key="cms_channel_statistic" value-ref="cms_channel_statistic"/>

</map>

</property>

第七步:到目前为止,核心代码和配置编写完毕啦!!!可以在栏目模板中使用标签了!

<!—Author:www.javake.net-->

( [@cms_channel_statisticid=a.id]${tag_bean.contentAllCount}[/@cms_channel_statistic] )
---------------------
作者:Java客
来源:CSDN
原文:https://blog.csdn.net/javafamily/article/details/8544400
版权声明:本文为博主原创文章,转载请附上博文链接!

自己动手编写JEECMS自定义栏目统计标签的更多相关文章

  1. JEECMS自定义标签

    查看JEECMS的源代码发现开发者版本还没有类似现成的统计标签,一种解决的办法是使用现有的JEECMS标签,像这样Struts( [@cms_content_list channel=id]${tag ...

  2. [原创]JEECMS 自定义标签调用广告版位下的所有广告(利用广告管理管理首页幻灯片)

    JEECMS自带的只有[@cms_advertising]标签,并且官方没有给文档,用法: [@cms_advertising id='3']             <img src=&quo ...

  3. JEECMS自定义标签开发步骤2

    JEECMS自带的只有[@cms_advertising]标签,并且官方没有给文档,用法: [@cms_advertising id='3']             <img src=&quo ...

  4. JEECMS自定义标签开发步骤

    JEECMS自带的只有[@cms_advertising]标签,并且官方没有给文档,用法: [@cms_advertising id='3']             <img src=&quo ...

  5. JEECMS站群管理系统-- 标签使用和模板的制作

    1模板规划 1.1资源文件 资源文件就是网页中用到的图片.CSS.JS等元素,在CMS系统中所有的资源文件在网站的根目录中的 /res_base/所属网站定义资源目录/TEMPLEATE/WEB /r ...

  6. JEECMS二次开发 -------标签使用说明

    转载:https://blog.csdn.net/u012176984/article/details/45501771 一:标签套用结构说明 登录后台管理页面,这些嵌套在html中的标签 以[@标签 ...

  7. OWIN系列之自己动手编写中间件

    一.前言 1.基于OWIN的项目摆脱System.Web束缚脱颖而出,轻量级+跨平台,使得ASP.NET应用程序只需依赖这个抽象接口,不用关心所运行的Web服务器. 2.OWIN.dll介绍 使用反编 ...

  8. Django入门--自定义过滤器与标签

    ---恢复内容开始--- 为了让Django找到自定义的模板过滤器和模板标签,需要进行文件路径配置,配置方式分为APP目录下配置和项目路径下配置两种方式: 1.在APP目录下配置:针对某个应用特定的自 ...

  9. 如何在CSDN博客自定义栏目中添加“给我写信”

    在"自定义栏目"中添加"连接"(将自己的微博,QQ空间和CSDN博客关联起来)很多人都做过.但是添加"给我写信"这个功能,用的好像不太多.此 ...

随机推荐

  1. SQL 分组后获取每组中最大值

    场景:sql server 2008 drop table ID CREATE TABLE ID ( id ,) not null, code int , D date, PRIMARY KEY (i ...

  2. Facebook分布式框架—Thrift介绍。

    Thrift介绍 Thrift是一个分布式RPC框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, ...

  3. 6_1.springboot2.x整合JDBC与数据源配置原理解析

    1.引言 对于数据访问层,无论是SQL还是NOSQL,Spring Boot默认采用整合 Spring Data的方式进行统一处理,添加大量自动配置,屏蔽了很多设置.引入各种xxxTemplate,x ...

  4. POJ - 2778 ~ HDU - 2243 AC自动机+矩阵快速幂

    这两题属于AC自动机的第二种套路通过矩阵快速幂求方案数. 题意:给m个病毒字符串,问长度为n的DNA片段有多少种没有包含病毒串的. 根据AC自动机的tire图,我们可以获得一个可达矩阵. 关于这题的t ...

  5. Neo4j全文检索

    全文检索基本概念 搜索 搜索这个行为是用户与搜索引擎的一次交互过程,用户需要找一些数据,他提供给搜索引擎一些约束条件.搜索引擎通过约束条件抽取一些结果给用户 搜索引擎 搜索引擎存在的目的是存储,查找和 ...

  6. Java中9大内置基本数据类型Class实例和数组的Class实例

    1.Java中9大内置几本数据类型: 对于对象来说,可以直接使用对象.getClass()或者Class.forName(className);.类名.class都可以获取Class实例. 但是我们的 ...

  7. spring的mvc对于页面日期格式进行传值到后台

    对于spring的mvc 日期格式从页面传入后台是个问题.string类型和整形都能友好传入.但是对于日期类型date却不能传入.回报403参数不对的错误. 看例子: @RequestMapping( ...

  8. js怎样把URL链接的参数截取出来

    有时候,A页面参数需要传递到B页面,则把参数拼接到跳转B页面的url上,这时怎样在另一个页面截取A页面传递的参数呢,主要代码如下 /** * 获取指定的URL参数值 URL:http://www.qu ...

  9. java实践经验几种常见数据库连接池的使用比较

    经历的几个产品及项目中,包括了各种数据库及应用服务器,基本上几种常见的数据库连接池都用到了,根据使用的情况把这些连接池比较一下吧.(http://m.0834jl.com) 感觉在介绍之前有必要阐述一 ...

  10. React项目开发经验汇总

    博客来源 小寒的博客   定义好全局配置信息 环境变量不要提取出来,配置信息提取出来 UI样式变量 定义好变量的作用不用多说 样式库建设 工具样式,复用性强的样式,这些class成为会是真个网站样式的 ...