{"id":3735,"date":"2018-12-29T07:58:58","date_gmt":"2018-12-28T23:58:58","guid":{"rendered":"http:\/\/switch.linesno.com\/?p=3735"},"modified":"2018-12-29T08:11:27","modified_gmt":"2018-12-29T00:11:27","slug":"thymeleaf%e8%87%aa%e5%ae%9a%e4%b9%89%e6%a0%87%e7%ad%be%e6%96%b9%e8%a8%80%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"http:\/\/switch.linesno.com\/?p=3735","title":{"rendered":"thymeleaf\u81ea\u5b9a\u4e49\u6807\u7b7e\u65b9\u8a00\u5904\u7406"},"content":{"rendered":"<p>\u9879\u76ee\u80cc\u666f:springboot+thymeleaf<\/p>\n<p>thymeleaf\u4e24\u79cd\u65b9\u5f0f\u5904\u7406\u81ea\u5b9a\u4e49\u6807\u7b7e\uff1a<span class=\"hljs-title\">AbstractAttributeTagProcessor \u548c\u00a0AbstractElementTagProcessor<\/span><\/p>\n<p><span class=\"hljs-title\">\u4e00\u3001AbstractAttributeTagProcessor \uff1a<\/span><\/p>\n<p><span class=\"hljs-title\">1. \u5b9a\u4e49dialog<\/span><\/p>\n<div class=\"cnblogs_code\">\n<pre>package com.spt.im.web.config;\r\n\r\nimport java.util.HashSet;\r\nimport java.util.Set;\r\n\r\nimport org.springframework.beans.factory.annotation.Value;\r\nimport org.thymeleaf.dialect.AbstractProcessorDialect;\r\nimport org.thymeleaf.processor.IProcessor;\r\n\r\npublic class CustomDialect extends AbstractProcessorDialect{\r\n    \r\n    private static final String DIALECT_NAME = \"staticFile\";\r\n    private static final String PREFIX = \"W\";\r\n    public static final int PROCESSOR_PRECEDENCE = 1000;\r\n    @Value(\"${im.static.resources}\")\r\n    private String filePath;\r\n    \r\n\r\n    protected CustomDialect() {\r\n        super(DIALECT_NAME, PREFIX, PROCESSOR_PRECEDENCE);\r\n    }\r\n\r\n    @Override\r\n    public Set&lt;IProcessor&gt; getProcessors(String dialectPrefix) {\r\n        final Set&lt;IProcessor&gt; processors = new HashSet&lt;IProcessor&gt;();\r\n        processors.add(new SampleJsTagProcessor(dialectPrefix, filePath));\r\n        processors.add(new SampleCssTagProcessor(dialectPrefix, filePath));\r\n        processors.add(new SampleSrcTagProcessor(dialectPrefix, filePath));\r\n        return processors;\r\n    }\r\n\r\n}<\/pre>\n<\/div>\n<p>2. \u5b9a\u4e49\u5904\u7406\u5668<\/p>\n<div class=\"cnblogs_code\">\n<pre>package com.spt.im.web.config;\r\n\r\nimport org.thymeleaf.IEngineConfiguration;\r\nimport org.thymeleaf.context.ITemplateContext;\r\nimport org.thymeleaf.engine.AttributeName;\r\nimport org.thymeleaf.model.IProcessableElementTag;\r\nimport org.thymeleaf.processor.element.AbstractAttributeTagProcessor;\r\nimport org.thymeleaf.processor.element.IElementTagStructureHandler;\r\nimport org.thymeleaf.standard.expression.IStandardExpression;\r\nimport org.thymeleaf.standard.expression.IStandardExpressionParser;\r\nimport org.thymeleaf.standard.expression.StandardExpressions;\r\nimport org.thymeleaf.templatemode.TemplateMode;\r\n\r\npublic class SampleJsTagProcessor extends AbstractAttributeTagProcessor{\r\n    \r\n     private static final String ATTR_NAME = \"js\";\r\n     private static final String ELE_NAME = \"script\";\r\n     private static final int PRECEDENCE = 10000;\r\n     private String filePath;\r\n\r\n    protected SampleJsTagProcessor(String dialectPrefix, String filePath) {\r\n         super(\r\n                TemplateMode.HTML, \r\n                dialectPrefix,     \r\n                ELE_NAME,             \r\n                false,             \r\n                ATTR_NAME,         \r\n                true,              \r\n                PRECEDENCE,       \r\n                true); \r\n         this.filePath = filePath;\r\n    }\r\n\r\n    @Override\r\n    protected void doProcess(ITemplateContext context,\r\n            IProcessableElementTag tag, AttributeName attributeName,\r\n            String attributeValue, IElementTagStructureHandler structureHandler) {\r\n        final IEngineConfiguration configuration = context.getConfiguration();\r\n        final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);\r\n        final IStandardExpression expression = parser.parseExpression(context, attributeValue);\r\n        final String url = (String) expression.execute(context);\r\n        structureHandler.setAttribute(\"src\", filePath + url);\r\n    }\r\n\r\n}<\/pre>\n<\/div>\n<p>3. \u6dfb\u52a0\u5230\u914d\u7f6e\u4e2d<\/p>\n<div class=\"cnblogs_code\">\n<pre>package com.spt.im.web.config;\r\n\r\nimport org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.Configuration;\r\n\r\n@Configuration\r\npublic class WorkImport {\r\n\r\n    @Bean\r\n    public CustomDialect testDialect(){\r\n        return new CustomDialect();\r\n    }\r\n}<\/pre>\n<\/div>\n<p>4. \u9875\u9762\u4e2d\u4f7f\u7528<\/p>\n<div class=\"cnblogs_code\">\n<pre>&lt;script W:js=\"@{\/static\/js\/pinyin.js}\" type=\"text\/javascript\"&gt;&lt;\/script&gt;<\/pre>\n<\/div>\n<p>\u9879\u76ee\u8fd0\u884c\uff0c\u4e0a\u8ff0\u6807\u7b7e\u4f1a\u88ab\u66ff\u6362\u6210\u76f8\u5e94\u7684\u94fe\u63a5\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76ee\u80cc\u666f:springboot+thymeleaf thymeleaf\u4e24\u79cd\u65b9\u5f0f\u5904\u7406\u81ea\u5b9a\u4e49\u6807\u7b7e\uff1aAbstract [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-3735","post","type-post","status-publish","format-standard","hentry","category-day"],"_links":{"self":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts\/3735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3735"}],"version-history":[{"count":2,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts\/3735\/revisions"}],"predecessor-version":[{"id":3737,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts\/3735\/revisions\/3737"}],"wp:attachment":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3735"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}