zoukankan      html  css  js  c++  java
  • 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  ,可以查看对应的结果

  • 相关阅读:
    在禅道中实现WORD等OFFICE文档转换为PDF进行在线浏览
    慎用 supportedRuntime
    定时开关机方案
    谨慎使用Sql server data tool 架构比对排除
    生成数据字典
    Cordova开发速记
    SQLSERVER 2012 收缩日志
    QService 服务容器
    使用用户自定义类型 CLR UDT
    使用DOTNETZIP过滤并压缩相对目录
  • 原文地址:https://www.cnblogs.com/zhushilai/p/13569880.html
Copyright © 2011-2022 走看看