我正在使用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/<application-name>/static/css/bootstrap.min.css
结果是:
{&#34; timestamp&#34;:1472734923645,&#34; status&#34;:404,&#34; error&#34;:&#34; Not Found&#34;,&#34; message& #34;:&#34;没有可用消息&#34;,&#34;路径&#34;:&#34; /cms/static/css/bootstrap.min.css"}
但文件 那里......
我浏览了一下但无法在Spring Boot Reference Guide找到解决方案。
我做错了什么以及如何让它发挥作用?
答案 0 :(得分: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
答案 1 :(得分:1)
静态文件夹直接映射到根URL,因此您将通过root URL +相对于静态文件夹fe的路径访问这些文件。
localhost:8080/css/style.css