1.自定义标签类



import static cn.com.yhxl.common.web.freemarker.DirectiveUtils.OUT_LIST;
import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import cn.com.yhxl.jhahi.entity.pm.PmShopArticle;
import cn.com.yhxl.jhahi.entity.pm.PmShopChannel;
import cn.com.yhxl.jhahi.service.pm.IPmShopArticleService;
import cn.com.yhxl.jhahi.service.pm.IPmShopChannelService;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
* 内容列表标签
*/
public class ArticleListDirective implements TemplateDirectiveModel {
/**
* 模板名称
*/
public static final String TPL_NAME = "content_list";
/**
* 栏目id
*/
public static final String PARAM_CHANNEL_ID = "channelId";
/**
* 是否包含子栏目文章0:自身栏目;1:包含子栏目;
*/
public static final String PARAM_CHANNEL_OPTION = "channelOption";

private List<PmShopChannel> list = new ArrayList<>();

@SuppressWarnings({ "unchecked", "deprecation" })
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
// CmsSite site = FrontUtils.getSite(env);
List<PmShopArticle> list = getList(params, env);

Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
params);
paramWrap.put(OUT_LIST, DEFAULT_WRAPPER.wrap(list));
Map<String, TemplateModel> origMap = DirectiveUtils
.addParamsToVariable(env, paramWrap);
if (body != null) {
body.render(env.getOut());
}
DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
}

protected List<PmShopArticle> getList(Map<String, TemplateModel> params,
Environment env) throws TemplateException {
Long channelId = DirectiveUtils.getLong(PARAM_CHANNEL_ID, params);
Integer channelOption = DirectiveUtils.getInt(PARAM_CHANNEL_OPTION,
params);
if (channelId == null) {
return null;
}
if (channelOption == null) {
channelOption = 0;
}
if (channelOption != null && channelOption == 0) {
if (channelId != null) {
return articleService.getListByChannelId(channelId, null);
}
}else if(channelOption != null && channelOption == 1){
PmShopChannel parentChannel = channelService.findById(channelId);
List<PmShopChannel> list = this.getAllChannelList(channelId);
list.add(parentChannel);
return articleService.getAllChildByChannelId(list);
}
return null;
}

private List<PmShopChannel> getAllChannelList(Long parentChannelId){
List<PmShopChannel> childList = channelService.getChilds(parentChannelId);
if (childList!=null&&childList.size()>0) {
list.addAll(childList);
if (childList!=null&&childList.size()>0) {
for (PmShopChannel channel : childList) {
// list.add(channel);
this.getAllChannelList(channel.getScId());
}
}
}
return this.list;
}

@Autowired
private IPmShopArticleService articleService;
@Autowired
private IPmShopChannelService channelService;

}


2.修改相关配置文件


3.

yhxl-core-context.xml添加如下内容:



<bean id="article_list" class="cn.com.yhxl.common.web.freemarker.ArticleListDirective"/>


yhxl-web.properties添加如下内容:



directive.article_list=article_list