SpringBoot02(带模板页面)

1.目录结构:

SpringBoot02(带模板页面)_第1张图片

2.pom.xml:引入相关依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.0.RELEASE
         
    
    com.example
    springboot-helloworld
    0.0.1-SNAPSHOT
    springboot-mybatis-mycat
    Demo project for Spring Boot

    
        1.8
    

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

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


3.application.properties 配置页面的相关配置

#设置应用端口
server.port=8090

#指定页面的所在位置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false

4.HelloController控制器:

package com.cxb.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value;
/**
 * descripiton:
 * helloworld
 * 小石潭记
 */
@Controller
public class HelloController {

    /**
     * 返回页面
     * @return
     */
    @GetMapping(value = "/")
    public String hello() {
        return "hello" ;
    }

    /**
     * @ResponseBody 就会返回字符串
     * @return
     */
    @GetMapping(value = "/say")
    @ResponseBody
    public String say() {
        return "say hello world!" ;
    }

}

5.hello.html




    
    hello


    hello 页面

6.启动类 这里不连接数据库,启动时不加载数据库:

(exclude={DataSourceAutoConfiguration.class})

package com.cxb.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class SpringbootHelloWorldApplication {

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

}

7.启动测试:

http://localhost:8090/ 返回页面

SpringBoot02(带模板页面)_第2张图片

http://localhost:8090/say 返回字符串

SpringBoot02(带模板页面)_第3张图片

springboot集成页面成功!

 

你可能感兴趣的:(springboot,thymeleaf)