使用 Thymeleaf 在 Spring Boot 中加载静态资源 [英] Load static resource in Spring Boot with Thymeleaf

查看:72
本文介绍了使用 Thymeleaf 在 Spring Boot 中加载静态资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 thymeleaf 制作页面.但是我对静态文件有一些问题.我调查过问题(

test.html

<头><script src="js/test.js" th:src="@{/test.js}"/><身体><button onclick="testFunction('test value')">按钮</button></html>

test.js

function testFunction(test) {控制台日志(测试);}

配置类

@Configuration@EnableWebMvc公共类 WebMvcConfig 扩展了 WebMvcConfigurerAdapter {@覆盖public void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/**").addResourceLocations("classpath:/static/js/");super.addResourceHandlers(注册表);}}

还有问题,当我加载 test.html 文件时没有加载 javascript.

@GetMapping(value = "web/test")公共字符串 getTestHtmlPage() {返回测试";}

/api/v1application.properties => 中的一个配置;server.servlet-path=/api/v1

我做错了什么?你能帮我吗?

谢谢大家!

解决方案

为了更好地理解 registry.addResourceHandler("/**").addResourceLocations("classpath:/static/js/");

我写了一个更彻底的在此处回答,讨论无效的资源位置映射.它没有收到任何赞成票,所以我希望它不可怕.:-)

简而言之,通过您映射 classpath:/static/js/,然后访问 /js/test.js 的方式,您告诉Spring 在 /static/js/js/test.js 中查找.

您可能想要的是classpath:/static/.在这种情况下,当您尝试访问 /js/test.js 时,它会在 /static/js/test.js 中查找文件.

至于 Thymeleaf,我从未使用过它,但文档表明您应该使用 th:src 而不是 th:href 加载脚本.th:href 似乎仅适用于 HTML 内容.

I want to make page using thymeleaf. But I have some problem with the static files. I've investigated questions(1,2,3) with similar problem, but it didn't help me.

I use Spring Boot framework in the application. My files look like:

test.html

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <script src="js/test.js" th:src="@{/test.js}"/>
</head>
<body>
<button onclick="testFunction('test value')">Button</button>
</body>
</html>

test.js

function testFunction(test) {
    console.log(test);
}

Configuration class

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/js/");
        super.addResourceHandlers(registry);
    }
}

And problem, when I load test.html file with javascript not loaded.

@GetMapping(value = "web/test")
public String getTestHtmlPage() {
    return "test";
}

/api/v1 is a configuration in application.properties => server.servlet-path=/api/v1

What do I do wrong? Can you help me?

Thanks all!

解决方案

For a better understanding of registry.addResourceHandler("/**").addResourceLocations("classpath:/static/js/");

I wrote a more thorough answer here that discusses invalid resource location mappings. It didn't receive any upvotes, so I hope it's not terrible. :-)

In short, with the way you're mapping classpath:/static/js/, and then accessing, /js/test.js, you're telling Spring to look in /static/js/js/test.js.

What you probably want is classpath:/static/. In that case, when you try to access /js/test.js, it's looking in /static/js/test.js for the file instead.

As for Thymeleaf, I've never used it, but docs indicate you should load scripts with th:src instead of th:href. th:href appears to only be for HTML content.

这篇关于使用 Thymeleaf 在 Spring Boot 中加载静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆