原文地址:http://www.glxxw2018.com/study/blog/detail/MGnPTaqWUm.html
在使用Thymeleaf时,有时候需要自定义一些标签,以方便在Thymeleaf模板中使用,Thymeleaf方言很强大,不仅可以自定义简单的html标签,还可以自定义复杂的标签,这个标签可以使用后台处理并访问数据库等以显示相关格式的信息,大大简化的开发复杂度,可复用性加强了。
本篇博客仅讲关于自定义简单的标签,如引用前端js库的标签。
如:
<head> <title>页面模板</title> <strive:common/> <strive:import lib="jquery"/> <strive:import lib="vue"/> <strive:import lib="elementui"/> </head>
这里的<strive:import lib="jquery" />就是引入了Jquery相关的js库,是不是很简单。下面看看如何来实现。这里需要添加两个类,一个是自定义方言,还有一个就是元素标签处理器,这里的元素标签处理器就是处理strive标签的。源代码如下:
自定义方言类:CustomDialect.java
package com.fight.strive.sys.extend.thymeleaf; import org.thymeleaf.dialect.AbstractProcessorDialect; import org.thymeleaf.processor.IProcessor; import org.thymeleaf.standard.StandardDialect; import java.util.HashSet; import java.util.Set; public class CustomDialect extends AbstractProcessorDialect { private static final String prefix = "strive"; public CustomDialect() { super("Strive Dialect", prefix, StandardDialect.PROCESSOR_PRECEDENCE); } @Override public Set<IProcessor> getProcessors(String dialectPrefix) { final Set<IProcessor> processors = new HashSet<IProcessor>(); processors.add(new CustomElementTagProcessor(dialectPrefix)); processors.add(new CustomElementTagCommonProcessor(dialectPrefix)); return processors; } }
自定义strive标签处理器类:CustomElementTagProcessor.java
package com.fight.strive.sys.extend.thymeleaf; import org.thymeleaf.context.ITemplateContext; import org.thymeleaf.context.WebEngineContext; import org.thymeleaf.model.AttributeValueQuotes; import org.thymeleaf.model.IModel; import org.thymeleaf.model.IModelFactory; import org.thymeleaf.model.IProcessableElementTag; import org.thymeleaf.processor.element.AbstractElementTagProcessor; import org.thymeleaf.processor.element.IElementTagStructureHandler; import org.thymeleaf.templatemode.TemplateMode; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CustomElementTagProcessor extends AbstractElementTagProcessor { private static final String TAG_NAME = "import"; private static final int PRECEDENCE = 1000; public CustomElementTagProcessor(final String dialectPrefix) { super(TemplateMode.HTML, dialectPrefix, TAG_NAME, true, null, false, PRECEDENCE); } @Override protected void doProcess(ITemplateContext context, IProcessableElementTag tag, IElementTagStructureHandler structureHandler) { /** * 获取应用程序的上下文参数 */ WebEngineContext ctx = (WebEngineContext) context; final String contextPath = ctx.getServletContext().getContextPath(); /** * 获取导入js库的名称 */ final String name = tag.getAttributeValue("lib"); /** * 创建引入js库的DOM元素模型 */ final IModelFactory modelFactory = context.getModelFactory(); final IModel model = modelFactory.createModel(); Map<String, String> attributes_script = null; Map<String, String> attributes_link = null; List<Map<String, String>> scripts = new ArrayList<>(); List<Map<String, String>> links = new ArrayList<>(); switch (name) { case "jquery": attributes_script = new HashMap<>(); attributes_script.put("src", "http://libs.baidu.com/jquery/2.1.1/jquery.min.js"); scripts.add(attributes_script); break; case "vue": attributes_script = new HashMap<>(); attributes_script.put("src", "https://cdn.jsdelivr.net/npm/vue"); scripts.add(attributes_script); break; case "elementui": //添加样式 attributes_link = new HashMap<>(); attributes_link.put("href", "https://unpkg.com/element-ui/lib/theme-chalk/index.css"); links.add(attributes_link); //添加js attributes_script = new HashMap<>(); attributes_script.put("src", "https://unpkg.com/element-ui/lib/index.js"); scripts.add(attributes_script); break; } if (!links.isEmpty()) { for (Map<String, String> attr : links) { attr.put("rel", "stylesheet"); model.add(modelFactory.createOpenElementTag("link", attr, AttributeValueQuotes.DOUBLE, false)); model.add(modelFactory.createCloseElementTag("link")); } } if (!scripts.isEmpty()) { for (Map<String, String> attr : scripts) { model.add(modelFactory.createOpenElementTag("script", attr, AttributeValueQuotes.DOUBLE, false)); model.add(modelFactory.createCloseElementTag("script")); } } //将元素替换为指定的模型 structureHandler.replaceWith(model, false); } }
strive框架的源码见:https://gitee.com/zxstrive/strive