thymeleaf-dynamic-url-dialect

dynamic replace resources url for thymeleaf

License

License

Categories

Categories

Leaf Data Databases
GroupId

GroupId

com.github.ayongw
ArtifactId

ArtifactId

thymeleaf-dynamic-url-dialect
Last Version

Last Version

1.3.2
Release Date

Release Date

Type

Type

jar
Description

Description

thymeleaf-dynamic-url-dialect
dynamic replace resources url for thymeleaf
Project URL

Project URL

https://github.com/ayongw/thymeleaf-dynamic-url-dialect
Source Code Management

Source Code Management

https://github.com/ayongw/thymeleaf-dynamic-url-dialect

Download thymeleaf-dynamic-url-dialect

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.ayongw/thymeleaf-dynamic-url-dialect/ -->
<dependency>
    <groupId>com.github.ayongw</groupId>
    <artifactId>thymeleaf-dynamic-url-dialect</artifactId>
    <version>1.3.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.ayongw/thymeleaf-dynamic-url-dialect/
implementation 'com.github.ayongw:thymeleaf-dynamic-url-dialect:1.3.2'
// https://jarcasting.com/artifacts/com.github.ayongw/thymeleaf-dynamic-url-dialect/
implementation ("com.github.ayongw:thymeleaf-dynamic-url-dialect:1.3.2")
'com.github.ayongw:thymeleaf-dynamic-url-dialect:jar:1.3.2'
<dependency org="com.github.ayongw" name="thymeleaf-dynamic-url-dialect" rev="1.3.2">
  <artifact name="thymeleaf-dynamic-url-dialect" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.ayongw', module='thymeleaf-dynamic-url-dialect', version='1.3.2')
)
libraryDependencies += "com.github.ayongw" % "thymeleaf-dynamic-url-dialect" % "1.3.2"
[com.github.ayongw/thymeleaf-dynamic-url-dialect "1.3.2"]

Dependencies

compile (10)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar 2.1.4.RELEASE
org.springframework.boot : spring-boot-configuration-processor Optional jar 2.1.4.RELEASE
org.springframework : spring-webmvc jar
org.thymeleaf : thymeleaf jar 3.0.11.RELEASE
org.thymeleaf : thymeleaf-spring5 jar 3.0.11.RELEASE
com.google.guava : guava jar 21.0
org.apache.commons : commons-lang3 jar 3.8.1
org.apache.commons : commons-io jar 1.3.2
org.apache.commons : commons-collections4 jar 4.3
org.slf4j : slf4j-api jar 1.7.26

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.slf4j : slf4j-simple jar 1.7.26

Project Modules

There are no modules declared in this project.

thymeleaf-dynamic-url

Maven Central

动态替换资源url thymeleaf插件

引入方式

<dependency>
  <groupId>com.github.ayongw</groupId>
  <artifactId>thymeleaf-dynamic-url-dialect</artifactId>
  <version>1.3.2</version>
</dependency>

解决的问题

解决在开发环境下配置的资源地址能够在不更改程序代码的前提下自动替换为实际的各环境地址。
比如开发环境使用本地地址,生产环境可以将地址自动映射到CDN地址上。加快资源文件加载

默认的boot配置

spring:
  thymeleaf:
    dynamic-url:
      enable: true
      enable-local: true
      enable-remote: true
      enable-cache: true
      localSuffix: .min
      minSuffixTypes: css,js
      remotePrefix: /static/lib/
      remoteUrlMap:
        "/demo/local/file.js": "https://demohost.com/file.js"
      excludePaths:
        - /exclude/file1.js
        - /exclude/file2.css

使用示例

HTML示例片段

<link href="../static/lib/bootstrap/dist/css/bootstrap.css" awdl:href="@{/static/lib/bootstrap/dist/css/bootstrap.min.css}" rel="stylesheet" />
<link href="../static/custlib/sb-admin/css/sb-admin-2.css" awdl:href="@{/static/custlib/sb-admin/css/sb-admin-2.css}" rel="stylesheet" />

根据以上示例,

1.会自动根据配置,将"/static/lib/bootstrap/dist/css/bootstrap.min.css"替换为具体的配置地址。

2.对于本地资源文件"/static/custlib/sb-admin/css/sb-admin-2.css",则会根据配置替换为压缩后的文件地址,一般为"/static/custlib/sb-admin/css/sb-admin-2.min.css"

自定义资源文件替换器

默认是使用配置文件的形式来替换CDN(远程资源)文件的,但这仅用于测试、开发阶段是可以,真正的生产环境并不是太友好,
如需可以自定义自己的实现,只需要简单的实现接口,并注入到spring中即可。 
com.github.ayongw.thymeleaf.dynamicurl.service.DynamicResourceLocationService

Versions

Version
1.3.2
1.3.1
1.3.0
1.2.1
1.2.0
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0