2017-05-12 136 views
1

好吧,完全難倒了。我的啓動應用程序沒有提供任何靜態資源。 CSS未被應用,圖像未被加載。沒有安全性,所以我不認爲這是問題。Springboot - 將資源解釋爲樣式表,但使用MIME類型text/html傳輸

檢查控制檯輸出I得到

Resource interpreted as Stylesheet but transferred with MIME type text/html 

一切都是200

Shows everything loading

但是,如果你看一下響應......這不是引導

enter image description here

我相信我使用的是正確的默認文件結構

enter image description here

下面的代碼:

的pom.xml

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-actuator</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 
    <!-- WEB JARS --> 
    <dependency> 
     <groupId>org.webjars</groupId> 
     <artifactId>bootstrap</artifactId> 
     <version>3.3.7-1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.webjars</groupId> 
     <artifactId>jquery</artifactId> 
     <version>3.2.0</version> 
    </dependency> 
    <!-- JDBC --> 
    <dependency> 
     <groupId>net.sourceforge.jtds</groupId> 
     <artifactId>jtds</artifactId> 
     <version>1.3.1</version> 
    </dependency> 
    <!-- LOGGING --> 
    <dependency> 
     <groupId>net.logstash.logback</groupId> 
     <artifactId>logstash-logback-encoder</artifactId> 
     <version>4.7</version> 
    </dependency> 
    <!-- SCOPED JARS--> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-devtools</artifactId> 
     <scope>runtime</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
      <configuration> 
       <executable>true</executable> 
      </configuration> 
     </plugin> 
    </plugins> 
     <finalName>${project.artifactId}</finalName> 
</build> 

模板/ index.html的

<!DOCTYPE HTML> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head lang="en" th:include="fragments/headerinc :: head"> 
    <title>Demo App</title> 
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8"/> 
</head> 
<body> 
<div class="container-fluid"> 
    <nav class="navbar navbar-inverse navbar-static-top"> 
     <div class="container-fluid"> 
      <div class="navbar-header"> 
       <button type="button" class="navbar-toggle collapsed" 
         data-toggle="collapse"> 
        <span class="sr-only">Toggle navigation</span> <span 
         class="icon-bar"></span> <span class="icon-bar"></span> <span 
         class="icon-bar"></span> 
       </button> 
       <a class="navbar-brand" href="#"> 
        <img style="width: 75px;" th:src="@{~/images/logo.png}"/>My APP</a> 
      </div> 
      <div id="navbar5" class="navbar-collapse collapse"> 
       <ul class="nav navbar-nav navbar-right"> 
        <li class="active"><a href="#">Home</a></li> 
        <li><a href="#">Journal</a></li> 
        <li><a href="#">Etc</a></li> 
       </ul> 
      </div> 
     </div> 
    </nav> 
</div> 

模板/片段/ headerinc.html

<!DOCTYPE HTML> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head lang="en" th:fragment="head"> 
    <meta http-equiv="content-type" content="text/html" charset="UTF-8"/> 
    <link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" 
      th:href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" 
      rel="stylesheet" media="screen" /> 
    <link href="../css/styles.css" th:href="@{/css/styles.css}" 
      type="text/css" rel="stylesheet" media="all"/> 
</head> 
<body> 
</body> 
</html> 

任何建議,將不勝感激!

+0

您爲[/ webjars/**]模式添加了資源處理程序(https://spring.io/blog/2014/01/03/utilizing-webjars-in-spring-boot)嗎? – manish

+0

它不正確地提供所有靜態內容。例如localhost/css/styles.css不會提供404,而是呈現index.html。 – datGnomeLife

+0

你是如何解決這個問題的? – imdzeeshan

回答

1

在使用Webjars通過使用行家依賴性的情況下,你的控制器請求方法應規定即使URI僅僅是一個/,像下面。

@RequestMapping(value = "/") 
public String index() { 
    return "welcome"; 
} 

但是,如果你把所有CSSJS資源/靜態文件夾,春天會自動發出相應的響應。

相關問題