栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

4.2 Spring boot 中使用模版引掣

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

4.2 Spring boot 中使用模版引掣

在springboot项目中如果要使用动态页面就需要使用到模版引掣,目前比较常用的引掣有:JSP、Velocity、Freemarker、Thymeleaf 而在springboot中推荐使用Thymeleaf ,其语法更简单,功能更强大。下面内容是介绍如何使用Thymeleaf

1.引入Thymeleaf

idea中thymeleaf的导入比较简单,将thymeleaf依赖加入到pom.xml文件中即可实现

找到Spring官方文档:“https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/htmlsingle/#using-boot-starter” 

找到相对应thymeleaf配置文件pom配置信息,并将thymeleaf相关信息配置到应用的pom.xml中

 

2.Thymeleaf使用

只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;
使用:

1)编写Controller请求 方法success()

package com.atguigu.springboot.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class HelloController {
    Logger logger =LoggerFactory.getLogger(getClass());

   


    @RequestMapping("/success")
    public String success(){
        //classpath:/templates/success.html
        return "success";//返回字符串用于找到对应的success.html
    }
}

2)页面中导入thymeleaf的名称空间

html lang="en" xmlns:th="http://www.thymeleaf.org">




    
    Title


 成功!!!!

3)启动应用进行访问

自动跳转到了对应的页面 

3.使用thymeleaf语法

1)在方法中写入需要传到前台的参数

//查出一些数据在页面展示
    @RequestMapping("/success")
    public String success(Map map){
        map.put("hello","你好");
        //classpath:/templates/success.html
        return "success";
    }

2)页面中展示后台所传值




    
    Title


 成功!!!!
 


3)重启应用访问

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/764227.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号