Javascript springboot-eleaf相对URL
我现在使用的是spring boot 1.3和thymeleaf 这是我的百里香页面的一部分:Javascript springboot-eleaf相对URL,javascript,spring,spring-mvc,spring-boot,thymeleaf,Javascript,Spring,Spring Mvc,Spring Boot,Thymeleaf,我现在使用的是spring boot 1.3和thymeleaf 这是我的百里香页面的一部分: <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Spring Boot and Thymeleaf demo</title> <meta http-equiv="Content-Type" content="text/htm
<!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>
我想知道SpringBoot或thymeleaf是否提供了这样的功能来完成这些事情或功能
谢谢。据我所知,没有任何现成的东西可以为您处理这种行为,但是编写一个自定义标记(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
}
@组件类CdnDialect扩展了抽象方言{
@自动连线CdnUrlProcessor CdnUrlProcessor
公共CdnDialect(){
超级()
}
公共字符串getPrefix(){
“cdn”
}
@重写公共集getProcessors(){
[cdnUrlProcessor]设置为
}
}
这就是Thymeleaf(尽管查看测试-您显然希望为此添加测试,这非常容易添加)
然后,您可以在HTML模板中使用处理器和自定义前缀,就像使用Thymeleaf“th”属性一样:
<script type="text/javascript" src="" cdn:src="/js/our.min.js"></script>
(顺便说一句,上面的代码是从我的Groovy复制的——因此,如果您使用java,则需要添加返回关键字和分号)
@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
}
<script type="text/javascript" src="" cdn:src="/js/our.min.js"></script>