我试图使用Thymeleaf将引导程序添加到我的Spring项目中。
index.html
<!DOCTYPE html >
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="fragments/header :: header"> </head>
<body>
<div th:replace="@{'views/' + ${content}} :: ${content}"></div>
<div lang="en" th:replace="fragments/footer :: footer"> </div>
</body>
</html>footer.html
<div xmlns:th="http://www.thymeleaf.org" th:fragment="footer" >
<script th:src="@{/webjars/jquery/3.3.1/jquery.min.js}"></script>
<script th:src="@{/webjars/popper.js/1.12.9-1/umd/popper.min.js}"></script>
<script th:src="@{/webjars/bootstrap/4.0.0-1/js/bootstrap.min.js}" ></script>
</div>header.html
<head xmlns:th="http://www.thymeleaf.org" th:fragment="header" >
<meta charset="UTF-8">
<title>Модуль планирования ПД</title>
<link th:rel="stylesheet" th:href="@{webjars/bootstrap/4.0.0-1/css/bootstrap.min.css} "/>
</head>MvcConfig.java
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/webjars/**")
.addResourceLocations("/webjars/")
.resourceChain(false);
}
}样式不适用于页和抛出错误:

在jquery中,引导程序,popper。
我怎么能解决这个问题?
谢谢。
发布于 2018-04-25 23:17:26
为了更容易,我得到了webjars定位器:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.0.0-2</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<version>0.30</version>
</dependency>在我的页面最后,我说:
<script th:src="@{/webjars/jquery/jquery.min.js}"></script>
<script th:src="@{/webjars/popper.js/umd/popper.min.js}"></script>
<script th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"></script>并需要设置这个WebMvcConfigure类:
@Configuration
public class SpringMVCConfiguration implements WebMvcConfigurer {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"};
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
registry.addResourceHandler("/webjars/**")
.addResourceLocations("/webjars/").resourceChain(false);
}
}我没有使用Spring引导中的任何其他设置来使用。
发布于 2018-03-07 20:08:54
如果在org.webjars中使用pom.xml依赖项,则可能应该在webjars/...之前添加斜杠/,并将属性rel="stylesheet"保留在header.html中。
<link rel="stylesheet" th:href="@{/webjars/bootstrap/4.0.0-1/css/bootstrap.min.css} "/>还可以尝试在没有MvcConfig配置类的情况下运行它。
发布于 2018-07-04 11:43:58
使用Spring,WebJars是自动配置的.不需要添加任何处理程序或其他配置。当您运行应用程序(没有任何信任)时,您可以看到这样的日志:
... Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]因此,删除MvcConfig类可以解决这个问题。
事实上,我的Spring应用程序类似于您的应用程序(但没有配置文件),并且工作得很好。当我将您的配置添加到我的应用程序中时,WebJars不再工作了!
https://stackoverflow.com/questions/49147216
复制相似问题