2017-03-06 32 views
3

thymeleafを使ってページを作りたいと思います。しかし、私は静的ファイルにいくつか問題があります。私は同様の問題で(123)の質問を調査していますが、それは私を助けません。Spring Bootで静的リソースをThymeleafでロードする

私はアプリケーションでSpring Bootフレームワークを使用します。 私のファイルは、のように見える: enter image description here

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クラス

@Configuration 
@EnableWebMvc 
public class WebMvcConfig extends WebMvcConfigurerAdapter { 

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

問題が発生しました。test.htmlファイルをロードしてもJavaScriptがロードされません。

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

enter image description here

/api/v1私が間違って何application.properties => server.servlet-path=/api/v1

で構成ですか?手伝って頂けますか?

ありがとうございます! registry.addResourceHandler("/**").addResourceLocations("classpath:/static/js/");

をより良く理解するために

+1

あなたはtest.jsにエラーを含めなかった...それは404ですか?実際のパスは 'js/test.js'ではなく' test.js'でなければなりません –

+0

はい、404エラーです。私が '

関連する問題