我正在试用 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
结果是:
{"timestamp":1472734923645,"status":404,"error":"Not Found","message":"No message available","path":"/cms/static/css/bootstrap.min .css”
但是文件是在那里...
我浏览了一下,但在 Spring Boot Reference Guide 找不到解决方案.
我做错了什么以及如何让它发挥作用?
最佳答案
删除 spring.resources.static-locations
从你的配置,除非你想改变它,/static/
默认包含。
资源在 static
目录包含在 http 路径中。
引用资源时不包括 static
.
文件src/main/resources/static/css/bootstrap.min.css
应该可以在 http://localhost:8080/appname/css/bootstrap.min.css
上找到
关于javascript - Spring Boot - 为什么我的应用程序找不到静态资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39272452/