2016-09-01 4 views
0

스프링 부트를 사용하고 있으며 CSS, 이미지 및 JS 파일에 액세스 할 수 없습니다.스프링 부트 - 내 애플리케이션에서 정적 리소스를 찾을 수없는 이유는 무엇입니까?

@SpringBootApplication 
public class Application { 

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

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"}

부 파일 입니다.

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

정적 폴더를 사용할 수 있어야합니다, 그래서 당신은 정적 폴더 철에 루트 URL + 상대 경로로 이러한 파일에 액세스합니다.

localhost:8080/css/style.css

관련 문제