• 微服务 第二章:SpringBoot 创建web项目(用Thymeleaf模板引擎)


        springboot内部对jsp的支持并不是特别理想,而springboot推荐的视图是Thymeleaf。Thymeleaf在其他随笔中有所讲述,这里不再重复。

        码云地址:https://gitee.com/yaohuiqin/SpringBootDemo

    方法一:springboot的非web项目改成web项目

    1、添加maven

      在第一章的代码基础上,添加maven

         <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>
    

    2、在resources文件夹下新建templates包,该包下放置静态的html文件(模板引擎)

    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8"/>
        <title>Title</title>
    </head>
    <body>
     <h1>您好:</h1>
     <h1 th:text="${name}"></h1>
    </body>
    </html>
    

    3、新建一个Controller ,该Controller返回index.html 

    @Controller
    public class IndexController {
        @RequestMapping(value = "/index" ,method = RequestMethod.GET)
        public String returnIndex(ModelMap map){
            map.addAttribute("name", "yaohuiqin");
            return "index";
        }
    }
    

     

    4、运行项目,在浏览器上运行:

     方法2 :idea自动创建springboot的Web项目

     1、file > new >project

    项目创建完成:项目结构如下 

     

    如果用Thymeleaf模板引擎的html,需要添加对应的maven

        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
    

     

    最后新建controller和html页面即可。

  • 相关阅读:
    NOI Online 2020 提高组游记
    【HDU5840】This world need more Zhu
    CSP-S 2019 AFO记
    防错笔记
    关于Blog
    题解 【UER #6】逃跑
    动态规划杂题选记
    有趣计数题选做
    题解 [POI2012] Leveling Ground
    xioa han 带画家!
  • 原文地址:https://www.cnblogs.com/yaohuiqin/p/9365704.html
Copyright © 2020-2023  润新知