自己动手编写JEECMS自定义栏目统计标签
来源:互联网 发布:matlab 遍历二维矩阵 编辑:程序博客网 时间:2024/06/06 14:23
今天想在给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;
- }
- }
/** * 频道统计实体类 * @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接口 * @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;
- }
- }
/** * 栏目信息统计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;@Repositorypublic class CmsChannelStatisticDaoImpl extends HibernateSimpleDaoimplements 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;
- }
- }
/** * 栏目统计 * @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);}@Autowiredprivate CmsChannelStatisticDao channelSatistic;public void setChannelSatistic(CmsChannelStatisticDao channelSatistic) {this.channelSatistic = channelSatistic;}}
第五步:在jeecms-context.xml文件中加入CmsChannelStatisticsDirective标签类的bean注入代码。
<!— Author:www.javake.net -->
<beanid="cms_lucene_page"class="com.jeecms.cms.lucene.LuceneDirectivePage"/>
<beanid="cms_advertising"class="com.jeecms.cms.action.directive.CmsAdvertisingDirective"/>
<beanid="cms_channel_statistic"class="com.jeecms.cms.action.directive.CmsChannelStatisticsDirective"/>
<!— Author:www.javake.net -->
<propertyname="freemarkerVariables">
<map>
<entrykey="uuid"value-ref="uuid"/>
<entrykey="process_time"value-ref="process_time"/>
<entrykey="text_cut"value-ref="text_cut"/>
<entrykey="html_cut"value-ref="html_cut"/>
<entrykey="cms_pagination"value-ref="cms_pagination"/>
<entrykey="cms_channel_list"value-ref="cms_channel_list"/>
<entrykey="cms_channel_page"value-ref="cms_channel_page"/>
<entrykey="cms_channel"value-ref="cms_channel"/>
<entrykey="cms_content"value-ref="cms_content"/>
<entrykey="cms_content_list"value-ref="cms_content_list"/>
<entrykey="cms_content_page"value-ref="cms_content_page"/>
<entrykey="cms_tag_list"value-ref="cms_tag_list"/>
<entrykey="cms_tag_page"value-ref="cms_tag_page"/>
<entrykey="cms_topic_list"value-ref="cms_topic_list"/>
<entrykey="cms_topic_page"value-ref="cms_topic_page"/>
<entrykey="cms_comment_list"value-ref="cms_comment_list"/>
<entrykey="cms_comment_page"value-ref="cms_comment_page"/>
<entrykey="cms_guestbook_ctg_list"value-ref="cms_guestbook_ctg_list"/>
<entrykey="cms_guestbook_list"value-ref="cms_guestbook_list"/>
<entrykey="cms_guestbook_page"value-ref="cms_guestbook_page"/>
<entrykey="cms_vote"value-ref="cms_vote"/>
<entrykey="cms_friendlink_ctg_list"value-ref="cms_friendlink_ctg_list"/>
<entrykey="cms_friendlink_list"value-ref="cms_friendlink_list"/>
<entrykey="cms_lucene_list"value-ref="cms_lucene_list"/>
<entrykey="cms_lucene_page"value-ref="cms_lucene_page"/>
<entrykey="cms_advertising"value-ref="cms_advertising"/>
<entrykey="cms_channel_statistic"value-ref="cms_channel_statistic"/>
</map>
</property>
第六步:在jeecms-servlet-front.xml文件中配置
<!— Author:www.javake.net -->
<beanid="freemarkerConfig"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<propertyname="freemarkerVariables">
<map>
<entrykey="uuid"value-ref="uuid"/>
<entrykey="process_time"value-ref="process_time"/>
<entrykey="text_cut"value-ref="text_cut"/>
<entrykey="html_cut"value-ref="html_cut"/>
<entrykey="cms_pagination"value-ref="cms_pagination"/>
<entrykey="cms_channel_list"value-ref="cms_channel_list"/>
<entrykey="cms_channel_page"value-ref="cms_channel_page"/>
<entrykey="cms_channel"value-ref="cms_channel"/>
<entrykey="cms_content"value-ref="cms_content"/>
<entrykey="cms_content_list"value-ref="cms_content_list"/>
<entrykey="cms_content_page"value-ref="cms_content_page"/>
<entrykey="cms_tag_list"value-ref="cms_tag_list"/>
<entrykey="cms_tag_page"value-ref="cms_tag_page"/>
<entrykey="cms_topic_list"value-ref="cms_topic_list"/>
<entrykey="cms_topic_page"value-ref="cms_topic_page"/>
<entrykey="cms_comment_list"value-ref="cms_comment_list"/>
<entrykey="cms_comment_page"value-ref="cms_comment_page"/>
<entrykey="cms_guestbook_ctg_list"value-ref="cms_guestbook_ctg_list"/>
<entrykey="cms_guestbook_list"value-ref="cms_guestbook_list"/>
<entrykey="cms_guestbook_page"value-ref="cms_guestbook_page"/>
<entrykey="cms_vote"value-ref="cms_vote"/>
<entrykey="cms_lucene_list"value-ref="cms_lucene_list"/>
<entrykey="cms_lucene_page"value-ref="cms_lucene_page"/>
<entrykey="cms_friendlink_ctg_list"value-ref="cms_friendlink_ctg_list"/>
<entrykey="cms_friendlink_list"value-ref="cms_friendlink_list"/>
<entrykey="cms_advertising"value-ref="cms_advertising"/>
<entrykey="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] )
- 自己动手编写JEECMS自定义栏目统计标签
- 自己动手编写JEECMS自定义栏目统计标签
- 编JEECMS自定义栏目统计标签
- jeecms自定义标签
- JEECMS自定义标签开发步骤
- JEECMS自定义标签开发步骤
- jeecms栏目模型和内容模型的使用以及对应前台的标签中的属性名
- JSP自定义标签编写
- JSP自定义标签编写
- 编写自定义标签
- 编写JSP自定义标签
- freemarker编写自定义标签
- 编写自定义标签
- 编写自定义标签
- JEECMS二次开发 -------标签使用说明
- JEECMS v6版标签
- 关于jeecms无法删除子栏目
- jeecms学习二-------学习freeMark标签及自定义标签的用法
- Yii-自定义删除确认弹框(zyd)
- C++ 数制转换
- /proc/meminfo详解
- 程序中更改资源加载方式plist文件
- 如何查看ubuntu的版本信息和查看几位 32 or 64位
- 自己动手编写JEECMS自定义栏目统计标签
- 题目1205: 百万富翁问题
- python操作protobuf
- Redmine学习
- 联想 V480c grub 启动硬盘安装 CentOS-6.3
- 字节对齐与sizeof
- Windows系统下,查看端口占用情况杀掉端口占用进程
- Android反编译
- java连接DB2检索一条记录的java代码