How can I serve static html from spring boot?

前端 未结 6 916
感情败类
感情败类 2020-12-05 06:18

I ran the spring-boot-sample-web-static project from here, made this alteration to the pom


    org.springframework.boot&l         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 07:04

    You can quickly serve static content in JAVA Spring-boot App via thymeleaf (ref: source)

    I assume you have already added Spring Boot plugin apply plugin: 'org.springframework.boot' and the necessary buildscript

    Then go ahead and ADD thymeleaf to your build.gradle ==>

    dependencies {
        compile('org.springframework.boot:spring-boot-starter-web')
        compile("org.springframework.boot:spring-boot-starter-thymeleaf")
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }
    

    Lets assume you have added home.html at src/main/resources To serve this file, you will need to create a controller.

    package com.ajinkya.th.controller;
    
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
    
      @Controller
      public class HomePageController {
    
          @RequestMapping("/")
          public String homePage() {
            return "home";
          }
    
      }
    

    Thats it ! Now restart your gradle server. ./gradlew bootRun

提交回复
热议问题