zoukankan      html  css  js  c++  java
  • 微服务 第二章: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页面即可。

  • 相关阅读:
    详解实现Android中实现View滑动的几种方式
    一起写一个Android图片轮播控件
    Java核心技术点之多线程
    深入了解整数在计算机内部的表示
    Java核心技术点之接口
    Java核心技术点之内部类
    配置resin web方式部署项目
    rsa加密算法,前后端实现。
    引用百度bcebos jar 503问题
    HashMap get()返回值问题
  • 原文地址:https://www.cnblogs.com/yaohuiqin/p/9365704.html
Copyright © 2011-2022 走看看