2017-05-20 5 views
0

Ich habe ein Problem mit meinen js und CSS-Dateien im Frühjahr Boot. Ich hatte gestern ein anderes Problem mit .html Dateien, aber ich löste es und jetzt versuche ich, dieses Problem zu lösen. Es scheint ähnlich mit .html Problem, aber ich konnte nicht lösen.Java Spring Boot Statische Inhalte - Wo soll ich setzen

Hier ist mein Code.

APPLICATION.JAVA (MAIN)

@SpringBootApplication 
public class Application extends WebMvcAutoConfiguration{ 

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

INDEX.HTML

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
    <head lang="en"> 
     <title>Home</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>   
     <link type="text/css" href="css/bootstrap.min.css" rel="stylesheet" /> 
     <link type="text/css" href="css/index.css" rel="stylesheet" /> 
     <script src="js/bootstrap.min.js"></script> 
     <script type="text/javascript" th:src="@{/js/bootstrap.min.js}"></script> 
    </head> 
    <body> 
     <h1 class="topFrame">Hello World!</h1> 
    </body> 
</html> 

TYMELEAF CONFIG

@Configuration 
public class ThymeleafConfig { 

    @Bean 
    public ServletContextTemplateResolver templateResolver() { 
     ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(); 
     resolver.setPrefix("/WEB-INF/templates/"); 
     resolver.setSuffix(".html"); 
     resolver.setTemplateMode("HTML5"); 
     resolver.setOrder(1); 
     return resolver; 
    } 
} 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.berkin</groupId> 
    <artifactId>kodgemisi.hr</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.3.RELEASE</version> 
    </parent> 

    <properties> 
    <maven.compiler.source>1.8</maven.compiler.source> 
    <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-thymeleaf</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.h2database</groupId> 
      <artifactId>h2</artifactId> 
      <version>1.4.190</version><!--$NO-MVN-MAN-VER$--> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

HOMECONTROLLER.JAVA

@Controller 
public class HomeController { 

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

} 

PICS

404 ERROR

FILE ORDER

Bin ich habe eine andere Konfigurationsdatei für CSS und JS-Dateien zu schreiben? Dank


SOLUTION

Ich habe dieses Problem nun gelöst. Sie müssen config nicht verwenden, wenn Sie Ihre Vorlagen nicht im Ordner src/main/resources ablegen. Ich habe alles in diesen Ordner verschoben, als die Konfigurationsdatei zu entfernen. Alles funktioniert jetzt perfekt.

SOLUTION

Antwort

0

siehe Bild unten für Verzeichnisposition: -

enter image description here

enter image description here

Verwandte Themen