JEECMS自定义标签开发步骤

4 篇文章 0 订阅
1 篇文章 0 订阅

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="" />

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值