如何在Thymeleaf 3.0.x中为自定义方言获取处理器中的标签正文

时间:2017-09-06 11:55:13

标签: java thymeleaf

我最近为thymeleaf 3.0.7编写了一个自定义方言,使用标签处理器将片段与自定义标签的主体合并。

例如,我有一个像这样的片段

<!-- file fragments/myfrag.html -->
<div th:fragment="content(header)">
    <h1 th:text="${header}">Header 1</h1>
    <my:content/>
    <span>Some span</span>
</div>

这样的自定义标签
<my:merger my:mergefragment="fragments/myfrag :: content('Hello world')">
    <h2>Header 2</h2>
    <button>Test</button>
</my:merger>

结果应该像

<div>
    <h1>Hello world</h1>
    <h2>Header 2</h2>   <!-- Content of my:merger -->
    <button>Test</button> <!-- Content of my:merger -->
    <span>Some span</span>
</div>

我的问题是如何获取自定义标记<my:merger>的正文?目前我有以下处理器。

public class MergerElementTagProcessor extends AbstractElementTagProcessor {

    private static final String TAG_NAME = "merger";

    public MergerElementTagProcessor(final String dialectPrefix) {
        super(TemplateMode.HTML, dialectPrefix, TAG_NAME, true, null, false, 1000);
    }

    @Override
    protected void doProcess(ITemplateContext context, IProcessableElementTag tag, IElementTagStructureHandler structureHandler) {

        final IEngineConfiguration configuration = context.getConfiguration();

        final Fragment fragment = (Fragment) computeFragment(context, tag.getAttributeValue("mergefragment"));
        final Writer stringWriter = new FastStringWriter(200);
        configuration.getTemplateManager().process(fragment.getTemplateModel(), context, stringWriter);

        String elementBody = getBody(tag); // how do I get the body of my <my:merger></<my:merger> tag at his point?

        structureHandler.replaceWith(merge(stringWriter.toString(), elementBody), false);
    }

    private String merge(String fragment, String elementBody) {
        return fragment.replace("<my:content/>", elementBody);
    }
}

感谢您提供任何帮助或建议。

1 个答案:

答案 0 :(得分:1)

作为 Metroids 提示的答案,我用以下代码完成了它:

public class MergerElementTagProcessor extends AbstractElementModelProcessor {

    private static final String TAG_NAME = "merger";

    public MergerElementTagProcessor(final String dialectPrefix) {
        super(TemplateMode.HTML, dialectPrefix, TAG_NAME, true, null, false, 1000);
    }

    @Override
    protected void doProcess(ITemplateContext context, IModel model,
            IElementModelStructureHandler structureHandler) {

        IProcessableElementTag tag = processElementTag(context, model);  // convert IModel to IProcessableElementTag
        Map<String, String> attrMap = processAttribute(context, tag); // get attributes of tag

        IModel baseModelClone = model.cloneModel(); // clone base model
        IModel fragmentModel = (IModel) computeFragment(context, tag.getAttributeValue("mergefragment")); // get fragment as IModel

        model.reset(); // clear the model reference
        model.addModel(mergeModel(fragmentModel, baseModelClone, "<my:content/>")); // merge the two IModels
    }

    protected IModel mergeModel(IModel fragment, IModel body, final String replaceTag) {
        IModel mergedModel = insert(fragment, body, replaceTag);
        mergedModel = remove(mergedModel, replaceTag);
        mergedModel = remove(mergedModel, replaceTag);
        return mergedModel;
    }

    private IModel insert(IModel fragment, IModel body, final String replaceTag) {
        IModel mergedModel = fragment.cloneModel();
        int size = mergedModel.size();
        ITemplateEvent event = null;
        for (int i = 0; i < size; i++) {
            event = mergedModel.get(i);
            if (event instanceof IOpenElementTag) {
                if (event.toString().contains(replaceTag)) {
                    mergedModel.insertModel(i, body);
                    break;
                }
            }
        }
        return mergedModel;
    }

    private IModel remove(IModel fragment, final String replaceTag) {
        IModel mergedModel = fragment.cloneModel();
        int size = mergedModel.size();
        ITemplateEvent event = null;
        for (int i = 0; i < size; i++) {
            event = mergedModel.get(i);
            if (event instanceof IOpenElementTag || event instanceof ICloseElementTag) {
                if (event.toString().contains(replaceTag)) {
                    mergedModel.remove(i);
                    break;
                }
            }
        }
        return mergedModel;
    }

}