繁体   English   中英

Spring Boot Thymeleaf相对网址

[英]spring boot Thymeleaf relative urls

我现在正在使用Spring Boot 1.3和百里香。

这是我的百里香页的一部分:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Boot and Thymeleaf demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <script type="text/javascript" src="" th:src="@{/js/our.min.js}"></script>
</head>
<body>
</body>
</html>

如您所见,我的网址就像

<script type="text/javascript" src="" th:src="@{/js/our.min.js}"></script>

当我的jquery和我的代码在同一服务器上时,这很好用。

但是现在我的js将部署到CDN,并将成为添加版本,可能是这样的:

<script type="text/javascript" src="http://s1.cdn.xxx.com/s/js/our.min.js?v=3QxjsKtdfdfsx"></script>

如您所见,我们做三件事。

首先:添加一个http网址:http: http://s1.cdn.xxx.com/s/ ,可以从配置文件中读取该网址。

第二:添加版本后缀v=3QxjsKtdfdfsx

第三:结合所有这些

http://s1.cdn.xxx.com/s/ + /js/our.min.js + v=3QxjsKtdfdfsx

我不知道弹簧靴或百里香提供这些功能或功能。

谢谢。

据我所知,没有什么开箱即用的东西可以为您处理此行为,但是编写自定义标签(Thymeleaf Processor )来封装逻辑并使其易于在任何地方使用都非常容易。

不久前,我不得不做基本上相同的事情:

首先,创建一个简单的处理器,它只是一个将处理您的自定义属性的类:

@Component
class CdnUrlProcessor extends AbstractStandardSingleAttributeModifierAttrProcessor{

@Autowired UrlService urlService
private final int HIGHEST_PRECEDENCE = Integer.MAX_VALUE


// Here we use a multi attribute matcher - we also link the processor to
// the CDN Dialect - which is just the namespace for the attributes
public CdnUrlProcessor(){
    super( new MultipleAttributeMatcher( CdnDialect, ["src", "href"] ) )
}

// These are just boilerplate overrides
@Override protected String getTargetAttributeName(Arguments arguments, Element element, String attributeName) {
    Attribute.getUnprefixedAttributeName(attributeName)
}

@Override protected ModificationType getModificationType(Arguments arguments, Element element, String attributeName, String newAttributeName) {
    ModificationType.SUBSTITUTION
}

@Override protected boolean removeAttributeIfEmpty(Arguments arguments, Element element, String attributeName, String newAttributeName) {
    false
}

@Override public int getPrecedence() {
    HIGHEST_PRECEDENCE
}


// This is where the magic happens - you can put your logic to build
// the url in here, or you could autowire a service (I did, as I wanted
// the logic available outside of Thymeleaf    
@Override protected String getTargetAttributeValue( Arguments arguments, Element element, String attributeName ){
    urlService.generateCdnUrl( element.getAttributeValue(attributeName) )
}
}

然后,您需要创建方言-如上面的代码注释中所述,这只是一个名称空间/前缀,将用于对处理器进行分组和应用:

@Component class CdnDialect extends AbstractDialect {


@Autowired CdnUrlProcessor cdnUrlProcessor

public CdnDialect() {
    super()
}

public String getPrefix() {
  "cdn"
}

@Override public Set<IProcessor> getProcessors() {
    [ cdnUrlProcessor ] as Set
}

}

这就是Thymeleaf的功能(尽管请检查测试-您显然想为此添加测试,这很容易添加)。

然后,可以在HTML模板中使用带有自定义前缀的处理器,就像将Thymeleaf“ th”属性设置为这样:

<script type="text/javascript" src="" cdn:src="/js/our.min.js"></script>

(顺便说一下,上面的代码是从我的Groovy复制的-因此,如果您使用的是Java,则需要添加return关键字和分号)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM