做了一个jeecmsV7版本的留言统计功能,具体实现过程如下:
(1)创建textq.ftl模板
<#--
<input type="text"/>
-->
<#macro textq
maxlength="" readonly="" value=""
label="" noHeight="false" required="false" colspan="" width="100" help="" helpPosition="2" colon=":" hasColon="true"
id="" name="" class="" style="" size="" title="" disabled="" tabindex="" accesskey=""
vld="" equalTo="" maxlength="" minlength="" max="" min="" rname="" rvalue=""
onclick="" ondblclick="" onmousedown="" onmouseup="" onmouseover="" onmousemove="" onmouseout="" onfocus="" onblur="" onkeypress="" onkeydown="" onkeyup="" onselect="" onchange=""
>
<input type="text"<#rt/>
<#if id!=""> id="${id}"</#if><#rt/>
<#if maxlength!=""> maxlength="${maxlength}"</#if><#rt/>
<#if max?string!=""> max="${max}"</#if><#rt/>
<#if min?string!=""> min="${min}"</#if><#rt/>
<#if readonly!=""> readonly="${readonly}"</#if><#rt/>
<#if rname!=""> rname="${rname}"</#if><#rt/>
<#if rvalue!=""> rvalue="${rvalue}"</#if><#rt/>
<#if title?? && title?string!=""> title="<@s.m '${title}'/>"</#if><#rt/>
<#if value?? && value?string!=""> value="${value?html}"</#if><#rt/>
<#include "common-attributes.ftl"/><#rt/>
<#include "scripting-events.ftl"/><#rt/>
/><#rt/>
</#macro>
(2) 在index.ftl中引入定义的模板文件
<#include "textq.ftl"/>
(3) 在CmsGuestbookAct.java中创建listCount方法
@RequiresPermissions("guestbookCount:v_list")
@RequestMapping("/guestbookCount/v_list.do")
public String listCount(Integer queryCtgId, String beginDate,
String endDate, Boolean queryRecommend, Boolean queryChecked,
Integer pageNo, HttpServletRequest request, ModelMap model) {
CmsSite site = CmsUtils.getSite(request);
Pagination pagination = manager.getPageCount(site.getId(), queryCtgId,
beginDate, endDate, null, queryRecommend, queryChecked, true,
false, cpn(pageNo), CookieUtils.getPageSize(request));
List<CmsGuestbookCtg> ctgList = cmsGuestbookCtgMng
.getList(site.getId());
Map<String, String> ctgMap = new HashMap<String, String>();
for (CmsGuestbookCtg ctg : ctgList) {
ctgMap.put(ctg.getId() + "", ctg.getName());
}
Long visitor=site.getVisitorTotal();
model.addAttribute("pagination", pagination);
model.addAttribute("visitor", visitor);
model.addAttribute("ctgMap", ctgMap);
model.addAttribute("queryCtgId", queryCtgId);
model.addAttribute("beginDate", beginDate);
model.addAttribute("endDate", endDate);
model.addAttribute("pageNo", pagination.getPageNo());
return "guestbookCount/list";
}
(4) CmsGuestbookMng.java中创建getPageCount方法
public Pagination getPageCount(Integer siteId, Integer ctgId,String beginDate,String endDate,Integer userId, Boolean recommend,
Boolean checked, boolean desc, boolean cacheable, int pageNo,
int pageSize);
(5) CmsGuestbookMngImpl.java中创建getPageCount方法
public Pagination getPageCount(Integer siteId, Integer ctgId,String beginDate,String endDate,Integer userId, Boolean recommend,
Boolean checked, boolean desc, boolean cacheable, int pageNo,
int pageSize) {
return dao.getPageCount(siteId, ctgId,beginDate,endDate,userId, recommend, checked, desc, cacheable,
pageNo, pageSize);
}
(6) CmsGuestbookDao.java中创建getPageCount方法
public Pagination getPageCount(Integer siteId, Integer ctgId,String beginDate,String endDate,Integer userId, Boolean recommend,
Boolean checked, boolean desc, boolean cacheable, int pageNo,
int pageSize);
(7) CmsGuestbookDaoImpl.java中
1.创建getPageCount方法
public Pagination getPageCount(Integer siteId, Integer ctgId,String beginDate,String endDate,Integer userId,Boolean recommend,
Boolean checked, boolean asc, boolean cacheable, int pageNo,
int pageSize) {
Finder f = createFinderCount(siteId, ctgId,beginDate,endDate,userId,recommend, checked, asc,
cacheable);
return findCount(f, pageNo, pageSize);
}
2.创建createFinderCount方法
private Finder createFinderCount(Integer siteId, Integer ctgId,String beginDate,String endDate,Integer userId,
Boolean recommend, Boolean checked, boolean desc, boolean cacheable) {
Finder f = Finder.create("SELECT MAX(b.ctg_name) ctgName,COUNT(a.guestbookctg_id) total,MAX(a.is_checked) checked " +
"FROM jc_guestbook a LEFT JOIN jc_guestbook_ctg b ON a.guestbookctg_id = b.guestbookctg_id ");
f.append(" where 1=1 ");
if (ctgId != null) {
f.append(" and a.guestbookctg_id="+ctgId);
}
if (siteId != null) {
f.append(" and a.site_id="+siteId);
}
if(beginDate!=null&&!"".equals(beginDate.trim())){
f.append(" and a.create_time>='"+beginDate+"'");
}
if(endDate!=null&&!"".equals(endDate.trim())){
f.append(" and a.create_time<='"+endDate+" 25'");
}
f.append(" GROUP BY a.guestbookctg_id,a.is_checked ");
f.append(" ORDER BY a.guestbookctg_id desc ");
f.setCacheable(cacheable);
return f;
}
(8) HibernateSimpleDao中创建findCount方法
@SuppressWarnings("unchecked")
protected Pagination findCount(Finder finder, int pageNo, int pageSize) {
int totalCount = countQueryResultCount(finder);
Pagination p = new Pagination(pageNo, pageSize, totalCount);
if (totalCount < 1) {
p.setList(new ArrayList());
return p;
}
Query query = getSession().createSQLQuery(finder.getOrigHql());
finder.setParamsToQuery(query);
query.setFirstResult(p.getFirstResult());
query.setMaxResults(p.getPageSize());
if (finder.isCacheable()) {
query.setCacheable(true);
}
List<Object[]> list = query.list();
Map<String,CmsGuestbookCount> maps = new HashMap<String,CmsGuestbookCount>();
for(Object[] obj:list){
String ctgName = (String) obj[0];
BigInteger count = (BigInteger) obj[1];
Boolean checked = (Boolean) obj[2];
if(maps.containsKey(ctgName)){
CmsGuestbookCount bean = maps.get(ctgName);
if(checked){
bean.setDeployed(count);
}else{
bean.setUndeployed(count);
}
bean.setTotal(bean.getTotal().add(count));
maps.put(ctgName, bean);
}else{
CmsGuestbookCount bean = new CmsGuestbookCount();
if(checked){
bean.setDeployed(count);
}else{
bean.setUndeployed(count);
}
bean.setCtgName(ctgName);
bean.setTotal(count);
maps.put(ctgName, bean);
}
}
List<CmsGuestbookCount> lists = new ArrayList<CmsGuestbookCount>();
for(String ctgName:maps.keySet()){
lists.add(maps.get(ctgName));
}
p.setList(lists);
return p;
}
(9) 创建CmsGuestbookCount类
public class CmsGuestbookCount implements Serializable{
private static final long serialVersionUID = 1L;
private String ctgName;
private BigInteger total=new BigInteger("0");
private BigInteger undeployed=new BigInteger("0");
private BigInteger deployed=new BigInteger("0");
public String getCtgName() {
return ctgName;
}
public void setCtgName(String ctgName) {
this.ctgName = ctgName;
}
public BigInteger getTotal() {
return total;
}
public void setTotal(BigInteger total) {
this.total = total;
}
public BigInteger getUndeployed() {
return undeployed;
}
public void setUndeployed(BigInteger undeployed) {
this.undeployed = undeployed;
}
public BigInteger getDeployed() {
return deployed;
}
public void setDeployed(BigInteger deployed) {
this.deployed = deployed;
}
}
(10) 在jeecms_sys文件夹下创建guestbookCount文件夹并创建list.html,内容如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title></title>
<#include "/jeecms_sys/head.html"/>
<script type="text/javascript">
function getTableForm() {
return document.getElementById('tableForm');
}
function optDelete() {
if(Pn.checkedCount('ids')<=0) {
$.alert("<@s.m 'global.prompt'/>","<@s.m 'error.checkRecord'/>");
return;
}
$.confirm(doDelete,"<@s.m 'global.confirm'/>","<@s.m 'global.confirm.delete'/>");
}
function doDelete(){
var f = getTableForm();
f.action="o_delete.do";
f.submit();
}
function optCheck() {
if(Pn.checkedCount('ids')<=0) {
$.alert("<@s.m 'global.prompt'/>","<@s.m 'error.checkRecord'/>");
return;
}
var f = getTableForm();
f.action="o_check.do";
f.submit();
}
function optCheckCancel() {
if(Pn.checkedCount('ids')<=0) {
$.alert("<@s.m 'global.prompt'/>","<@s.m 'error.checkRecord'/>");
return;
}
var f = getTableForm();
f.action="o_check_cancel.do";
f.submit();
}
</script>
<style>
.pn-sp{
display:none;}
.totleNum{
border-collapse:collapse;
width:100%;}
.totleNum td{
text-align:center;
border:1px solid #fff;
border-top:0;
}
</style>
</head>
<body>
<div class="box-positon">
<div class="rpos"><@s.m "global.position"/>: <@s.m "cmsGuestbook.function"/> - <@s.m "global.list"/></div>
<div class="clear"></div>
</div>
<div class="body-box">
<form action="v_list.do" method="post" style="padding-top:5px; padding-left:10px;">
<div>
<div style="float:left;">
<@p.textq style="width:90px;" value=beginDate name="beginDate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="Wdate" maxlength="100"/>
至
<@p.textq style="width:90px;" value=endDate name="endDate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="Wdate" maxlength="100"/>
留言类别:<@p.select name="queryCtgId" value=queryCtgId list=ctgMap headerKey="" headerValue="所有类型"/>
<input class="query" type="submit" value="<@s.m "global.query"/>"/>
</div>
<div style="clear:both;"></div>
</div>
</form>
<#assign x=0>
<#assign z=0>
<#assign h=0>
<form id="tableForm" method="post">
<input type="hidden" name="pageNo" value="${pageNo!}"/>
<div id="div1" style="display:none">
<@p.table value=pagination;cmsGuestbookCount,i,has_next><#rt/>
<@p.column code="cmsGuestbookCtg.function" align="center">${cmsGuestbookCount.ctgName!?html}</@p.column><#t/>
<@p.column code="信息总数目" align="center">${cmsGuestbookCount.total!?html}</@p.column><#t/>
<!--<@p.column code="待发布数目" align="center">${cmsGuestbookCount.undeployed!?html}</@p.column><#t/>
<@p.column code="已发布数目" align="center">${cmsGuestbookCount.deployed!?html}</@p.column><#t/>-->
<#assign z=z+1/>
<#if z!=1>
<#assign x=cmsGuestbookCount.total+x>
</#if>
</@p.table>
</div>
<div id="div2">
<@p.table value=pagination;cmsGuestbookCount,i,has_next><#rt/>
<@p.column code="cmsGuestbookCtg.function" align="center">${cmsGuestbookCount.ctgName!?html}</@p.column><#t/>
<@p.column code="信息总数目" align="center">${cmsGuestbookCount.total!?html}</@p.column><#t/>
<@p.column code="待发布数目" align="center">${cmsGuestbookCount.undeployed!?html}</@p.column><#t/>
<@p.column code="已发布数目" align="center">${cmsGuestbookCount.deployed!?html}</@p.column><#t/>
<@p.column code="所占百分比" align="center">${cmsGuestbookCount.total/x*100!}%</@p.column><#t/>
</@p.table>
<table class="totleNum">
<tr>
<td width='27%'>留言总数:</td>
<td width='73%'>${x}</td>
</tr>
</table>
</div>
</form>
</div>
<#include "/common/alert_message.html"/>
</body>
</html>