• JEECMS自定义标签开发步骤2


    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]

      生成代码:

  • 相关阅读:
    Django 框架 # 51
    Django 框架 介绍# 51
    前端之Bootstrap框架 # 50
    phpcms调用一个指定的栏目的url和栏目名称
    phpcms导航栏调用二级栏目
    彻底弄懂JS的事件冒泡和事件捕获
    toggle 方法的使用
    关于内层DIV设置margin-top不起作用的解决方案
    phpmyadmin导入数据库大小限制修改
    phpcms v9 的表单向导功能的使用方法
  • 原文地址:https://www.cnblogs.com/Jeely/p/11214525.html
Copyright © 2020-2023  润新知