Springboot不使用模板引擎加载html

原文地址:点我

源码地址: 点我

一、使用maven构建springboot项目

目录结构:

1.pom文件:


    4.0.0

    cn.itxsl
    springboot-example08
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.5.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

 

2.启动类及视图映射:

package cn.itxsl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @Author itxsl
 * @Description
 * @Date 2018/10/8 11:03
 */
@Controller
@SpringBootApplication
public class Start {

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

    @GetMapping({"/index","/"})
    public String index(){
        return "index";
    }

}

 

3.html和js:




    
    首页


哈哈

4.application.yml配置文件:

用法一、文件放在resources下

server:
  port: 8080
spring:
  mvc:
    view:
      suffix: .html
    static-path-pattern: /**
  resources:
    static-locations: classpath:/templates/,classpath:/static/

用法二、文件放在任意路径下

server:
  port: 8080
spring:
  mvc:
    view:
      suffix: .html
    static-path-pattern: /**
  resources:
    static-locations: file:D:\gzzg\springboot-example08\templates\,file:D:\gzzg\springboot-example08\static

最后结果图:

你可能感兴趣的:(Spring,boot)