【问题标题】:Spring Boot: Adding static content to web applicationSpring Boot:向 Web 应用程序添加静态内容
【发布时间】:2017-03-01 18:25:13
【问题描述】:

我正在使用 Spring Boot,我想知道我们如何在我的 JSP 文件中准确提及静态内容的路径? 我试图在 src/main/resources/static/css 中制作它们,但它不起作用,在我的 JSP 中我使用以下方法调用它们:

<link href="<c:url value="/css/bootstrap.min.css" />" rel="stylesheet" type="text/css">

我的 SpringBoot 类中没有特殊配置,只是调用 SpringApplication.run(...)

非常感谢您的帮助!

【问题讨论】:

  • 你配置扩展 WebMvcAutoConfigurationAdapter 吗?
  • 不,我的配置只是一个用 @SpringBootApplication 注释的简单类

标签: css spring jsp spring-mvc spring-boot


【解决方案1】:

您必须具有扩展 WebMvcAutoConfigurationAdapter 的配置,它具有自动扫描某些默认位置并将它们添加到类路径的注册表实现

  • /META-INF/resources/
  • /资源/
  • /静态/
  • /公共/

只需添加,

@Configuration
@EnableWebMvc
@ComponentScan
public class ServerConfiguration extends WebMvcAutoConfiguration{
}   

【讨论】:

    【解决方案2】:

    使用 springboot 1.5.6.RELEASE 我的文件夹结构看起来像

    ~/repos/static-content-example/src > tree
    .
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── example
    │   │           └── demo
    │   │               ├── DemoApplication.java
    │   │               └── MvcConfig.java
    │   └── resources
    │       ├── application.properties
    │       ├── public
    │       │   └── test.html
    │       └── templates
    └── test
        └── java
            └── com
                └── example
                    └── demo
                        └── DemoApplicationTests.java
    

    当我启动服务器时,我可以浏览到

    1. http://localhost:8080/test.html
    2. http://localhost:8080/public/test.html

    默认情况下,可以在上下文根目录中访问“public”文件夹中的任何内容(上面的#1)。 MvcConfig.java 允许#2。我总是设置该别名,这样我就可以忽略任何以/public 开头的 URL 的安全性。为了做到这一点没有 MvcConfig 设置,您必须在公共文件夹中放置一个名为public 的文件夹,这只是令人困惑。

    我不知道为什么 spring 在默认情况下不这样做......似乎它会消除很多混乱......

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 1970-01-01
      • 2015-05-24
      • 2018-02-10
      • 2017-07-19
      • 2021-10-16
      • 2014-05-14
      • 2019-03-04
      • 1970-01-01
      相关资源
      最近更新 更多