• springboot模板引擎之模板整合之thymeleaf(三)


    1在pom.xml中添加依赖

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

     2在application中添加配置文件

    #整合thymeleaf相关配置
    #开发时关闭缓存,不然没法看见实时页面
    spring.thymeleaf.cache=false
    spring.thymeleaf.mode=HTML5
    #thymeleaf路径
    spring.thymeleaf.prefix=classpath:/templates/tl/
    #thymeleaf编码格式
    spring.thymeleaf.encoding=UTF-8
    #thymeleaf类型
    spring.thymeleaf.servlet.content-type=text/html; charset=utf-8
    #thymeleaf名称后缀
    spring.thymeleaf.suffix=.html

     注意: thymeleaf的路径,我在templates中新建了一个文件夹 tl

     里面随便建了一个index.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    模版搜索引擎thymeleaf index.html
    </body>
    </html>

    在tl中新建一个admin的文件夹,里面新建一个info.html的页面

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    模版搜索引擎thymeleaf index.html
    </body>
    </html>

    3

    新建类ServiceSetting

     然后在application.properties中加入注解

    4在controller中新建一个包thymeleaf,在里面新建一个ThymeleafController

    package com.example.demo.controller.thymeleaf;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;

    import com.example.demo.domain.ServiceSetting;

    @Controller
    @RequestMapping("/thymeleaf")
    public class ThymeleafController {

    @Autowired
    private ServiceSetting setting;
    /**
    * 接口访问地址:localhost:8080//thymeleaf/hello
    *

    * @return
    */
    @GetMapping("hello")
    public String index() {
    return "index";//不用加后缀,在配置文件中已经配置了后缀
    }

    /**
    * 接口访问地址:localhost:8080//thymeleaf/info
    *

    * @param modelMap
    * @return
    */
    @GetMapping("info")
    public String admin(ModelMap modelMap) {
    modelMap.addAttribute("setting",setting);
    return "admin/info";//不用加后缀,在配置文件中已经配置了后缀
    }
    }

    5run as  application  ,可以查看对应的结果

  • 相关阅读:
    解决使用intellij idea开发MAVEN项目在target目录下不存在mapper.xml文件
    Mybatis中接口和对应的mapper文件位置配置详解
    Mybatis(1、核心配置文件、Properties、Settings、typeAliases...)
    nginx的常用负载均衡算法,分别是
    修改JVM的参数、Jstat、Jstack、gclog
    shiro 系列
    sso简单原理及实现
    Thymeleaf3.0内容
    Thymeleaf模板引擎+Spring整合使用方式的介绍
    给 IIS Express 配置虚拟目录
  • 原文地址:https://www.cnblogs.com/zhushilai/p/13569880.html
Copyright © 2020-2023  润新知