首页 > 其他 > 详细

thymeleaf 自定义标签

时间:2016-01-14 16:04:43      阅读:1611      评论:0      收藏:0      [点我收藏+]
/**
* @author candy
* @since 16/1/13.
*/
@Component
public class CmsDialect extends AbstractDialect {

@Override
public String getPrefix() {
return "crm";
}
@Override
public Set<IProcessor> getProcessors() {
final Set<IProcessor> processors = new HashSet<>();
processors.add(new FragmentElementProcessor());
return processors;
}
}

@Component
public class FragmentElementProcessor extends AbstractMarkupSubstitutionElementProcessor {


public FragmentElementProcessor() {
super("fragment");
}

@Override
protected List<Node> getMarkupSubstitutes(Arguments arguments, Element element) {


final ApplicationContext appCtx = ((SpringWebContext)arguments.getContext()).getApplicationContext();
final String path = element.getAttributeValue("path");
final FragmentManager fragmentManager =appCtx.getBean(FragmentManager.class);
final String content = fragmentManager.findReleasedContent(path);
final Element container = new Element("div");
final Text text = new Text(content);
container.addChild(text);

/*
* The abstract IAttrProcessor implementation we are using defines
* that a list of nodes will be returned, and that these nodes
* will substitute the tag we are processing.
*/
final List<Node> nodes = new ArrayList<>();
nodes.add(container);
return nodes;
}

@Override
public int getPrecedence() {
return 1000;
}
}

自定义标签的使用

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org" xmlns:crm="http://thymeleafexamples">
<crm:fragment path="/fragment.php"/>
</html>

官网的链接地址:http://www.thymeleaf.org/doc/tutorials/2.1/extendingthymeleaf.html#attribute-processors

thymeleaf 自定义标签

原文:http://www.cnblogs.com/yiyaopeng/p/5130181.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!