Springboot返回html

    注:Springboot的版本2.1.3.RELEASEhtml

    List-1 application.properties文件java

server.port=8080
#url中,项目的前缀
server.servlet.context-path=/project


spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html

    总体结构以下图1所示,html要放在static下,不是templates下web

            

                         图1spring

       List-2 HelloController的内容以下浏览器

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Slf4j
@Controller
public class HelloController {

    @RequestMapping(value = "/hello")
    public String index() {
        log.info("收到请求");
        return "html/hello";
    }
}

    List-3 启动springboot,以后在浏览器中输入spring-mvc

#返回index.html的内容
http://localhost:8080/project/

#返回hello.html的内容
http://localhost:8080/project/hello

    网上不少关于模板的(Thymeleaf 、FreeMarker 等),可是我不须要,我只须要纯的html。springboot

    index.html是springboot的默认welcome page。bash

Reference

  1. https://www.jianshu.com/p/eb4c0fc2dfc4
  2. https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-static-content
  3. https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-welcome-page
相关文章
相关标签/搜索