Witam
Piszę właśnie swój pierwszy program przy użyciu spring boot-a i thymeleaf-a (używam IDE Eclipse Neon). Natrafiłem na problem z konfiguracją źródeł statycznych, a konkretniej plików typu javascript. Okazuje się że program dobrze wczytuje pliki z folderów .css i .others (w załączniku schemat drzewa), natomiast za nic na świecie nie mogę go zmusić żeby załączył mi pliki z folderu .js (w sensie nie widać żadnych zmian w widoku po odpaleniu programu, które powinny być spowodowane przez plik z tego katalogu, ale z .css i .others działa w porządku). Pomyślałem że być może ja coś nie dobrze napisałem, ale wkleiłem ten plik .js inline do .html i działa w porządku.
Klasa konfiguracyjna:
package org.webapp;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter
{
@Bean
public AuthenticationInterceptor authenticationInterceptor()
{
return new AuthenticationInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry)
{
registry.addInterceptor(authenticationInterceptor());
}
private static final String[] CLASSPATH_RESOURCE_LOCATIONS =
{
"classpath:/static/css/", "classpath:/static/js/",
"classpath:/static/others/"
};
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
}
Plik html:
<!DOCTYPE html>
<html lang="pl" xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8"/>
<script type="text/javascript" src="MainPage.js"></script>
</head>
<body>
<h1 th:text="'Witaj ' + ${user?.username}"></h1>
<p id="date"></p>
</body>
</html>
Plik MainPage.js:
n = new Date();
y = n.getFullYear();
m = n.getMonth() + 1;
d = n.getDate();
document.getElementById("date").innerHTML = d + "/" + m + "/" + y;