【问题标题】:Spring boot - why can't my application find static resources?Spring boot - 为什么我的应用程序找不到静态资源?
【发布时间】:2017-01-09 09:25:13
【问题描述】:

我正在尝试使用 Spring Boot,但无法访问 CSS、图像和 JS 文件。

我有这个@SpringBootApplication 类:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我的application.properties 文件内容如下:

spring.datasource.url=jdbc:mysql://localhost:3306/<schema>?autoReconnect=true&useSSL=false
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.name=<sql-databasename>
spring.datasource.username=<sql-username>
spring.datasource.password=<sql-password>
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=false 
spring.jpa.hibernate.ddl-auto=update

# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.context-path=/<application-name>
server.display-name=<application-name>
server.port=8080
server.session.cookie.max-age=3600

# SPRING MVC (WebMvcProperties)
spring.mvc.favicon.enabled=true
spring.mvc.date-format=dd/MM/yyyy
spring.mvc.locale=pt_BR
spring.mvc.locale-resolver=accept-header
spring.mvc.servlet.load-on-startup=-1
spring.mvc.static-path-pattern=/**

# SPRING RESOURCES HANDLING (ResourceProperties)
spring.resources.add-mappings=true
spring.resources.cache-period=1
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/**,classpath:/public/**

# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.cache=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html

由于还没有完全理解 Spring Boot 的工作原理,我一直无法摆脱这个配置文件:

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = "b.c.g.c")
public class CmsBootApplicationConfiguration extends WebMvcConfigurerAdapter {

    @Bean(name = "messageSource")
    @Description("Spring message resolver")
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("i18n/messages");
        messageSource.setFallbackToSystemLocale(false);
        messageSource.setCacheSeconds(0);
        messageSource.setDefaultEncoding("utf-8");
        return messageSource;
    }

    @Bean(name = "localeResolver")
    @Description("Spring locale resolver")
    public LocaleResolver localeResolver() {
        SessionLocaleResolver resolver = new SessionLocaleResolver();
        resolver.setDefaultLocale(new Locale("pt", "BR"));
        return resolver;
    }

    @Bean(name = "multipartResolver")
    public StandardServletMultipartResolver resolver() {
        return new StandardServletMultipartResolver();
    }

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

    @Bean
    public UrlTemplateResolver urlTemplateResolver() {
        return new UrlTemplateResolver();
    }
}

静态资源位于此处:

src/main/resources
    static
        css/
        img/
        js/

但是,当我尝试访问任何静态资源时,例如浏览器调用如下:

http://localhost:8080/&lt;application-name&gt;/static/css/bootstrap.min.css

结果是:

{"timestamp":1472734923645,"status":404,"error":"Not Found","message":"No message available","path":"/cms/static/css/bootstrap.min .css"}

但是文件那里...

我浏览了一下,但在Spring Boot Reference Guide 找不到解决方案。

我做错了什么以及如何让它发挥作用?

【问题讨论】:

    标签: javascript java css spring spring-boot


    【解决方案1】:

    从您的配置中删除spring.resources.static-locations,除非您想更改它,/static/ 默认包含在其中。
    资源static 目录中包含在 http 路径中。
    引用资源时不包括static
    文件src/main/resources/static/css/bootstrap.min.css 应该在http://localhost:8080/appname/css/bootstrap.min.css 上可用

    【讨论】:

      【解决方案2】:

      静态文件夹直接映射到根 URL,因此您将通过根 URL + 相对于静态文件夹 fe 的路径访问这些文件。

      localhost:8080/css/style.css

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-01
        • 2014-11-10
        • 1970-01-01
        • 1970-01-01
        • 2014-09-15
        • 1970-01-01
        • 1970-01-01
        • 2015-07-07
        相关资源
        最近更新 更多