标签:
/**
* @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
标签:
原文地址:http://www.cnblogs.com/yiyaopeng/p/5130181.html