IT教程 | 加入收藏 | 简体 | 繁体
JavaCMS:科汛CMSPhpCMS织梦CMSShopCMS论坛CMSJavaCMS
当前位置: 首页 > CMS教程 > JavaCMS

JEECMS 自定义广告标签:调用广告版位下的所有广告

2015-09-01来源:[www.121ask.com]热度:0℃ 【字体:

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:

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

步骤二:

jeecms.properties添加cms_advertising_space标签

 
1 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;
}

 

标签写法:

 
1 2 3 4 5 6 [@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]

  生成代码:

 
1 2 3 <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="" />

  

 

以上IT文章《JEECMS 自定义广告标签:调用广告版位下的所有广告》由名客技术网[www.121ask.com]提供阅读!
名客技术网是一个免费为广大IT技术人员提供IT教程,绿色软件,asp/php/jsp/java/net等实例视频教程,CMS教程,破解软件下载以及学习相关源码、知识的IT网站。
名客技术网如果无意之中侵犯了您的版权,请来电告知,本站将在3个工作日内删除 QQ:175352796
Copyright 2011-2017 Powered by 121ASK.COM, All Rights Reserved.
备案号:鄂ICP备11013833号-1