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

JEECMS自带的只有[@cms_advertising]标签,并且官方没有给文档,用法:

[@cms_advertising id='3'] 
            <img src="${(tag_bean.attr.image_url)!?html}" width="${(tag_bean.attr.image_width)!?html}" height="${(tag_bean.attr.image_height)!?html}" /> 
 [/@cms_advertising]

这个是根据广告ID的来调用单独的一条广告,首页幻灯片一般都有多张,不能满足需求。

其实jeecms也有另一个js调用已经实现了的,但是不能生成到html中,用户体验不好。(com.jeecms.cms.action.front.AdvertisingAct类,<script src="/ad.jspx"  ……)

回到正题,自己增加一个标签,名字叫做:cms_advertising_space

步骤一:

jeecms-context.xml中增加一个bean:

	<!-- 新增按照广告牌取所有广告标签 -->
<bean id="cms_advertising_space" class="com.jeecms.cms.action.directive.CmsAdvertisingSpaceDirective"/>

步骤二:

jeecms.properties添加cms_advertising_space标签

directive.cms_advertising_space=cms_advertising_space

  

步骤三:

新增一个类:CmsAdvertisingSpaceDirective

package com.jeecms.cms.action.directive;

import static com.jeecms.common.web.freemarker.DirectiveUtils.OUT_BEAN;
import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER; import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import static com.jeecms.common.web.freemarker.DirectiveUtils.OUT_LIST;
import com.jeecms.cms.entity.assist.CmsAdvertising;
import com.jeecms.cms.entity.assist.CmsAdvertisingSpace;
import com.jeecms.cms.manager.assist.CmsAdvertisingMng;
import com.jeecms.cms.manager.assist.CmsAdvertisingSpaceMng;
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 CmsAdvertisingSpaceDirective implements TemplateDirectiveModel {
/**
* 输入参数,广告ID。
*/
public static final String PARAM_ID = "id"; @SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
Integer id = DirectiveUtils.getInt(PARAM_ID, params);
List<CmsAdvertising> ads = null;
CmsAdvertisingSpace adspace;
if (id != null) { //ad = cmsAdvertisingMng.findById(id);
ads= cmsAdvertisingMng.getList(id, true);
}
Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
params);
if (ads!=null){
paramWrap.put(OUT_LIST, DEFAULT_WRAPPER.wrap(ads));
} Map<String, TemplateModel> origMap = DirectiveUtils
.addParamsToVariable(env, paramWrap);
body.render(env.getOut());
DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
} @Autowired
private CmsAdvertisingMng cmsAdvertisingMng;
@Autowired
private CmsAdvertisingSpaceMng cmsAdvertisingSpaceMng;
}

标签写法:

[@cms_advertising_space id='7']
[#list tag_list as c] <img src="${(c.attr.image_url)!?html}" width="${(c.attr.image_width)!?html}" height="${(c.attr.image_height)!?html}" />
[/#list]
[/@cms_advertising_space]

  生成代码:

<img src="http://127.0.0.1:8080/r/cms/kingyon_zh/uploads/7-140611092P2415.jpg" width="" height="" /> 

<img src="http://127.0.0.1:8080/r/cms/kingyon_zh/uploads/3-140611092P2415.jpg" width="" height="" />

  

上一篇:转:WIN7上搭建Windows Phone 8 开发环境——VMware Workstation下Win8 “无法安装Hyper-V, 某个虚拟机监控程序正在运行”问题解决的办法


下一篇:Bash编程(5) Shell方法