FreeMarker自定义标签编写

本文档详细介绍了如何在Freemarker中实现自定义标签,包括使用宏定义和TemplateDirectiveModel的execute方法。代码示例展示了如何处理参数并结合服务获取和处理数据,最终将结果设置到环境变量中供模板渲染使用。内容涉及到站点ID、栏目ID、当前页数等参数的处理,以及内容的整合和排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、freemarker 实现自定义标签的方法

  1. 使用  <macro></macro>  宏定定义标签
  2. 实现  TemplateDirectiveModel的 execute() 方法,写相应的Java代码(这种方式更加的灵活)

 

二、参数说明:

environment : 是环境变量,在这里我们可以拿到 通过  environment.getOut  拿到 Write 。
map :这里我们可以得到参数,这里的参数是以 键值对的形式存在的。
templateModel : 是所以数据类型的顶级接口,我们  可以通过  templateMole[i]  通过这个i变量来返回我们指定的第几个参数的返回值。
templateBody : 是标签开始和结束的 内容 ,通过这个对象的 render()方法我可以接着执行 自定义标签里的 其他标签(freemarker内置标签或者我们自定义标签)

三、本人代码部分:

@Component
public class ContentDirectiveList implements TemplateDirectiveModel{
    /**
     * 模板名称
     */
    public static  final String TPL_NAME = "cms_content_list";
    //接收参数
    public static  final String SITE_ID = "siteId";//站点id
    public static  final String CHANNEL_ID = "channelId";//栏目id
    public static  final String CURRENTPAGE = "currentPage";//当前页数
    public static  final String COUNT = "count";//本页最大个数  必传
    public static  final String RELEVANT = "relevant";//1:随机推荐 不填默认按sort_num排序
    public static  final String ORDER_BY = "orderBy";//多种排序方式
    public static final String EXCLUDE_ID = "excludeId";//排除指定id 的内容
    /**
     * @author yuze
     * @date 2021/7/26 17:14
     * @param
     * @return
     * 各个栏目下的资源类型
     *    新闻栏目下:newsListImg 、contentImg、newsShowImg、titleImg、newsBannerImg、phoneListImg、typeImg
     *    视频栏目:vedio、singleImg
     *    文库栏目:libraryListImg、librarySharingImg、librarySharingImg
     *    下载栏目:singleImg、focusImg、iconImg、downListImg、detailImg、bannerImg、phoneIconImg
     *    图片栏目:singleImg、iconImg、focusImg
     */

    @Autowired
    public IContentService contentService;
    @Autowired
    public IChannelService channelService;
    @Autowired
    public IContentExtService contentExtService;

    public Object contentDirectiveList(Object siteId, Object count,Object channelId,Object relevant,Object currentPage,Object orderBy,Object excludeId) {
        if (channelId != null) {
            FrontContentVo content = FrontUtils.change(siteId, count, relevant, currentPage, orderBy, excludeId);
                Long[] channelIds = FrontUtils.convertUtils(channelId);
                List<Integer> son = ContentUtils.findSon(channelIds);
                content.setChildrenIds(son);
            List<FrontContentVo> frontContentVo = contentService.getAllByChannelId(content);
            List<FrontContentVo> frontContentVos = ContentUtils.IntegrateResources(frontContentVo);
            return frontContentVos;
        }else {
            FrontContentVo content = FrontUtils.change(siteId, count, relevant, currentPage, orderBy ,excludeId);
            List<Integer> sons = ContentUtils.allSons(Long.valueOf(siteId.toString()));
            content.setChildrenIds(sons);
            if (currentPage != null) {
                content.setCurrentPage((content.getCurrentPage() - 1) * content.getCount());
            }
            List<FrontContentVo> frontContentVo = contentService.listAll(content);
            List<FrontContentVo> frontContentVos = ContentUtils.IntegrateResources(frontContentVo);
            return frontContentVos;

        }


    }

    /**
     *
     * @param environment 运行的环境
     * @param map 方法参数map  方法名和值
     * @param templateModels
     * @param templateDirectiveBody 输出形式
     * @throws TemplateException
     * @throws IOException
     */
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        Object siteId = map.get(this.SITE_ID);
        Object count = map.get(this.COUNT);
        Object channelId = map.get(this.CHANNEL_ID);
        Object relevant = map.get(this.RELEVANT);
        Object currentPage = map.get(this.CURRENTPAGE);
        Object orderBy = map.get(this.ORDER_BY);
        Object excludeId = map.get(this.EXCLUDE_ID);
        environment.setVariable("tag_list", getModel(contentDirectiveList(siteId, count,channelId,relevant,currentPage,orderBy,excludeId)));

        templateDirectiveBody.render(environment.getOut());
    }

    private DefaultObjectWrapper getBuilder() {
        return new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25).build();
    }

    private TemplateModel getModel(Object o) throws TemplateModelException {
        return this.getBuilder().wrap(o);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值