一、freemarker 实现自定义标签的方法 :
- 使用 <macro></macro> 宏定定义标签
- 实现 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);
}
}