首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹簧引导+ Thymeleaf + webjars引导4

弹簧引导+ Thymeleaf + webjars引导4
EN

Stack Overflow用户
提问于 2018-03-07 08:26:50
回答 4查看 17K关注 0票数 3

我试图使用Thymeleaf将引导程序添加到我的Spring项目中。

index.html

代码语言:javascript
复制
<!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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
    @Configuration
public class MvcConfig implements WebMvcConfigurer {

   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry) {
      registry
              .addResourceHandler("/webjars/**")
              .addResourceLocations("/webjars/")
              .resourceChain(false);
   }
}

样式不适用于页和抛出错误:

在jquery中,引导程序,popper。

我怎么能解决这个问题?

谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-04-25 23:17:26

为了更容易,我得到了webjars定位器:

代码语言:javascript
复制
 <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>

在我的页面最后,我说:

代码语言:javascript
复制
<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类:

代码语言:javascript
复制
@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引导中的任何其他设置来使用。

票数 16
EN

Stack Overflow用户

发布于 2018-03-07 20:08:54

如果在org.webjars中使用pom.xml依赖项,则可能应该在webjars/...之前添加斜杠/,并将属性rel="stylesheet"保留在header.html中。

代码语言:javascript
复制
<link rel="stylesheet" th:href="@{/webjars/bootstrap/4.0.0-1/css/bootstrap.min.css} "/>

还可以尝试在没有MvcConfig配置类的情况下运行它。

票数 2
EN

Stack Overflow用户

发布于 2018-07-04 11:43:58

使用Spring,WebJars是自动配置的.不需要添加任何处理程序或其他配置。当您运行应用程序(没有任何信任)时,您可以看到这样的日志:

代码语言:javascript
复制
... Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

因此,删除MvcConfig类可以解决这个问题。

事实上,我的Spring应用程序类似于您的应用程序(但没有配置文件),并且工作得很好。当我将您的配置添加到我的应用程序中时,WebJars不再工作了!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49147216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档