【问题标题】:Spring boot web application can't find image in WEB-INFSpring Boot Web 应用程序在 WEB-INF 中找不到图像
【发布时间】:2023-03-21 05:25:01
【问题描述】:

这是我的项目内容结构:

spring boot 应用,InternalResourceViewResolver 指向jsp资源:

这是login.jsp,背景标签无法加载WEB-INF/jsp/Image中的图片,但是在spring boot application.java中,我已经指向了资源。我怎样才能得到它?

【问题讨论】:

  • Spring Boot 应用程序是一个罐子而不是一场战争(至少我没有看到任何使您的启动应用程序成为战争的东西)。我建议你看看this guidethis guide
  • 你建议我读过的指南。但我想使用 spring boot 来启动我的程序,而不是 tomcat。我已经找到了几天的解决方案,但什么也没得到!
  • 那么我非常怀疑您是否真的阅读并完成了这些指南。两个指南都使用 spring boot 来启动应用程序。 Spring Boot 是启动 tomcat 的原因。但是 Spring Boot 应用程序没有 web-inf 也没有 web.xml,因为它没有被使用。
  • 好吧,我一定是对spring boot的理解有误,用错了方式。

标签: java jsp spring-mvc spring-boot


【解决方案1】:

我有同样的问题。我了解所有静态资源都应位于 /resources/static 或公共位置(根据 Spring 文档),但它不起作用。

我这样实现了自己的 WebMvcConfigurerAdapter:

@Configuration
public class CustomResourceConfiguration extends WebMvcConfigurerAdapter {

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

现在可以了。希望它可以对某人有所帮助。

【讨论】:

    【解决方案2】:

    图像的路径是相对于 URL 的,而不是相对于 JSP 的位置。 尝试将图像放在这里: src/main/webapp/Images/LoginBg.jpg

    【讨论】:

    • 哦,是的。那是工作。谢谢你。你已经解决了。我还有一个问题,我读过一篇博客说可以使用以下程序:@Configuration public class StaticResourceConfiguration extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("file:/path/to/my/dropbox"); } }
    猜你喜欢
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多