• 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]

      生成代码:

  • 相关阅读:
    管理学课程都包括哪些方面的内容?
    关于如何创业的好书推荐:《创业必读12篇》
    关于企业文化的书籍,这本最经典
    如何开展行政管理工作?这些书可以告诉你答案
    企业家必读书籍有哪些?适合企业高管看的书推荐
    如何学好PHP?
    现在入门还有必要学PHP吗?
    Python实现递归二分法查找
    SAP Control framework–CL_GUI_TOOLBAR
    SAP Control framework–DIALOGBOX_CONTAINER
  • 原文地址:https://www.cnblogs.com/Jeely/p/11214525.html
Copyright © 2020-2023  润新知