码迷,mamicode.com
首页 > 其他好文 > 详细

thymeleaf 自定义标签

时间:2016-01-14 16:07:57      阅读:134      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!