亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Thymeleaf:將 webjar CSS 文件的內容插入樣式標簽

Thymeleaf:將 webjar CSS 文件的內容插入樣式標簽

大話西游666 2021-09-15 15:43:42
在某些情況下,內聯 CSS 文件比通過 URL 引用它更受歡迎(例如,在呈現包含所有內容的 HTML 頁面時)。該 CSS 文件可能來自 webjar。我需要什么才能撥打這樣的電話:<style th:insert="/webjars/bootstrap/bootstrap.css"></style>這是在 spring-boot 環境中運行的,沒有任何網絡服務器。
查看完整描述

1 回答

?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

所以我讓它與一個特殊的 TemplateResolver 一起工作。它的邏輯類似于 Spring 的WebJarsResourceResolver,并且使用WebJarAssetLocator.


public class WebJarTemplateResolver extends ClassLoaderTemplateResolver {


    private final static String WEBJARS_PREFIX = "/webjars/";


    private final static int WEBJARS_PREFIX_LENGTH = WEBJARS_PREFIX.length();


    private final WebJarAssetLocator webJarAssetLocator = new WebJarAssetLocator();


    @Override

    protected ITemplateResource computeTemplateResource(IEngineConfiguration configuration, String ownerTemplate, String template, String resourceName, String characterEncoding, Map<String, Object> templateResolutionAttributes) {


        resourceName = findWebJarResourcePath(template);

        if (resourceName == null) {

            return null;

        }


        return super.computeTemplateResource(configuration, ownerTemplate, template, resourceName, characterEncoding, templateResolutionAttributes);

    }


    @Nullable

    protected String findWebJarResourcePath(String templateName) {

        if (!templateName.startsWith(WEBJARS_PREFIX)) {

            return null;

        }


        int startOffset = WEBJARS_PREFIX_LENGTH;

        int endOffset = templateName.indexOf('/', startOffset);


        if (endOffset == -1) {

            return null;

        }


        String webjar = templateName.substring(startOffset, endOffset);

        String partialPath = templateName.substring(endOffset + 1);

        return this.webJarAssetLocator.getFullPathExact(webjar, partialPath);

    }

}

這通過以下配置集成到應用程序中:


@Configuration

public class ThymeleafConfiguration {


    private SpringTemplateEngine templateEngine;


    public ThymeleafConfiguration(SpringTemplateEngine templateEngine) {

        this.templateEngine = templateEngine;

    }


    @PostConstruct

    public void enableWebjarTemplates() {

        templateEngine.addTemplateResolver(new WebJarTemplateResolver());

    }

}


查看完整回答
反對 回復 2021-09-15
  • 1 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號