`
liuguofeng
  • 浏览: 437955 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Freemarker自定义标签

 
阅读更多
/**
 * 栏目对象标签
 * 
 * @author liufang
 * 
 */
public class ChannelDirective implements TemplateDirectiveModel {
	/**
	 * 输入参数,栏目ID。
	 */
	public static final String PARAM_ID = "id";
	/**
	 * 输入参数,栏目路径。
	 */
	public static final String PARAM_PATH = "path";
	/**
	 * 输入参数,站点ID。存在时,获取该站点栏目,不存在时获取当前站点栏目。
	 */
	public static final String PARAM_SITE_ID = "siteId";

	@SuppressWarnings("unchecked")
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		CmsSite site = FrontUtils.getSite(env);
		Integer id = DirectiveUtils.getInt(PARAM_ID, params);
		Channel channel;
		if (id != null) {
			channel = channelMng.findById(id);
		} else {
			String path = DirectiveUtils.getString(PARAM_PATH, params);
			if (StringUtils.isBlank(path)) {
				// 如果path不存在,那么id必须存在。
				throw new ParamsRequiredException(PARAM_ID);
			}
			Integer siteId = DirectiveUtils.getInt(PARAM_SITE_ID, params);
			if (siteId == null) {
				siteId = site.getId();
			}
			channel = channelMng.findByPathForTag(path, siteId);
		}

		Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
				params);
		paramWrap.put("tag_bean", DEFAULT_WRAPPER.wrap(channel));
		Map<String, TemplateModel> origMap = DirectiveUtils
				.addParamsToVariable(env, paramWrap);
		body.render(env.getOut());
		DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
	}

	@Autowired
	private ChannelMng channelMng;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics