【问题标题】:Sprinboot webapp doesn't load webjars and cssSpring Boot web 应用程序不加载 webjars 和 css
【发布时间】:2019-04-26 06:29:07
【问题描述】:

我有带有 springboot 和 jsp 的 maven webapp。我正在尝试导入 webjars 以在我的 jsp 上使用引导程序,我在我的 pom 上添加了依赖项(包括定位器),并将资源处理程序放在我的 webconfig 上:

<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7-1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<version>0.30</version>
</dependency>

现在我在我的 webconfig 中添加引用:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

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

然后我尝试在我的 jsp 中导入 jars:

<script src="/webjars/jquery/3.1.1/jquery.min.js"></script>

但不起作用..我将 css 放在 webapp 文件夹中,但我无法将其链接到我的 jsp 中..

我该如何解决这个问题?怎么了??谢谢大家

Ps:我在 web-inf/jsp/pages 中有我的 jsp,并且我已经向 application.properties 添加了一个上下文路径(但我认为与 WebConfig 类有一些冲突)

【问题讨论】:

    标签: jsp spring-boot web-applications webjars


    【解决方案1】:

    通过使用@EnableWebMvc,您已向 Spring Boot 表明您希望完全控制配置 Spring MVC。这意味着它所有的静态资源自动配置、Web jar 支持等都已关闭。

    您可以完全删除您的 WebConfig 类,因为它复制了 Spring Boot 会自动为您执行的配置。如果你需要自定义默认配置,你应该像你一样实现WebMvcConfigurer,但是省略@EnableWebMvc注解。

    【讨论】:

    • 感谢它的工作!为了加载 css,我在资源中创建了静态文件夹。我把我的css文件夹放在那里,然后通过&lt;link rel="stylesheet" type="text/css" href="css/package.css"&gt;导入它。 PS:我还删除了你所说的类,并将我添加到属性文件中的上下文添加到 jars 路径中
    【解决方案2】:

    添加下面的类。

    @Configuration
    @EnableWebSecurity//(debug=true)
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().requestMatchers(CorsUtils::isPreFlightRequest)
            .antMatchers("/webjars/**");
        }
    
    }
    

    【讨论】:

    • 这个问题没有提到 Spring Security,所以这段代码很有可能无法编译。
    【解决方案3】:

    添加 resourceChain 并将其设置为 false,如下所示:

     registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/").resourceChain(false);
    

    这显然不是 webjar-locator 问题。调用 resourceChain() 方法应该是配置 webjar-locator 所必需的。

    【讨论】:

    • 这对我有帮助,但不幸的是链接已损坏,我找不到相应的问题
    • @evandor,感谢您的评论,我删除了链接。
    【解决方案4】:

    尝试将${pageContext.request.contextPath} 添加到您的JSP 文件中。这为我解决了问题。

     <link href="${pageContext.request.contextPath}/webjars/bootstrap/4.6.0/css/bootstrap.min.css"
              rel="stylesheet">
        <script src="${pageContext.request.contextPath}/webjars/jquery/3.5.1/jquery.min.js" defer></script>
        <script src="${pageContext.request.contextPath}/webjars/bootstrap/4.6.0/js/bootstrap.min.js" defer></script>
    

    【讨论】:

      猜你喜欢
      • 2019-05-16
      • 2014-11-13
      • 2019-10-30
      • 1970-01-01
      • 2021-08-13
      • 2023-01-03
      • 2018-09-27
      • 2015-12-17
      • 2019-06-16
      相关资源
      最近更新 更多