当前位置: 代码迷 >> 综合 >> freemarker 自定义标签
  详细解决方案

freemarker 自定义标签

热度:30   发布时间:2024-01-26 12:45:05.0

以下是常用的标签,以<#开头

<#include
<#list
<#if

当这些标签不满足需求时,可以自定标签  使用的时候以<@开头

编写后台标签解析类。

@Component
public class ContentsDirective extends TemplateDirective {@Autowiredprivate PostService postService;@Autowiredprivate ChannelService channelService;@Overridepublic String getName() {//指定标签名return "contents";}@Overridepublic void execute(DirectiveHandler handler) throws Exception {//获取参数Integer channelId = handler.getInteger("channelId", 0);String order = handler.getString("order", Consts.order.NEWEST);Set<Integer> excludeChannelIds = new HashSet<>();if (channelId <= 0) {List<Channel> channels = channelService.findAll(Consts.STATUS_CLOSED);if (channels != null) {channels.forEach((c) -> excludeChannelIds.add(c.getId()));}}Pageable pageable = wrapPageable(handler, Sort.by(Sort.Direction.DESC, BeanMapUtils.postOrder(order)));Page<PostVO> result = postService.paging(pageable, channelId, excludeChannelIds);//返回数据handler.put("results", result).render();}
}

前台标签的使用

//使用标签            
<@contents channelId=channel.id pageNo=pageNo order=order><div class="posts"><ul class="posts-list"><#include "/classic/inc/posts_item.ftl"/>//得到后台返回的数据<#list results.content as row><@posts_item row/></#list><#if  results.content?size == 0><li class="content"><div class="content-box posts-aside"><div class="posts-item">该目录下还没有内容!</div></div></li></#if></ul></div></@contents>