2016-09-01 15 views
0

私はSpring Bootを提供しており、CSS、画像、JSファイルにアクセスすることはできません。スプリングブート - 私のアプリケーションが静的リソースを見つけることができないのはなぜですか?

​​

マイapplication.propertiesファイルを読み取ります:

私はこの@SpringBootApplicationクラスをしました

純粋まだ完全に春ブーツがどのように機能するかを理解していないことで
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 

は、私が振ることができませんでしたこのコンフィギュレーションファイルはオフになります。

@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(); 
    } 
} 

スタティックリソースはlocここでated:

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": "メッセージがありません"、 "path": "/ cms/static/css/bootstrap.min.css"}

Buファイルです。

解決策が見つかりませんでしたが、Spring Boot Reference Guideに解決策が見つかりませんでした。

私は間違って何をやっているのですか?

答えて

1

変更する場合を除き、spring.resources.static-locationsを設定から削除してください。デフォルトでは/static/が含まれています。
staticディレクトリはhttpパスに含まれています。
リソースを参照するときは、staticを含めないでください。
ファイルsrc/main/resources/static/css/bootstrap.min.cssは、ルートURLに直接マッピングされているhttp://localhost:8080/appname/css/bootstrap.min.css

1

静的フォルダで利用可能であるべきであるので、あなたは、静的なフォルダFEにルートURL +相対パスによって、これらのファイルにアクセスします。

localhost:8080/css/style.css

関連する問題