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

springboot模板引擎选择(springboot中controller层)

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

springboot模板引擎选择(springboot中controller层)

一:springboot的模板引擎thymeleaf

1.thymeleaf在pom.xml配置


        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

2.模板引擎的作用
模板引擎的作用就是我们来写一个页面模板,比如有些值呢,是动态的,我们写一些表达式。而这些值,从哪来呢,我们来组装一些数据,我们把这些数据找到。然后把这个模板和这个数据交给我们模板引擎,模板引擎按照我们这个数据帮你把这表达式解析、填充到我们指定的位置,然后把这个数据最终生成一个我们想要的内容给我们写出去,这就是我们这个模板引擎。
在themplates目录下的所有页面,只能通过controller来跳转。
这个只能通过thymeleaf跳转。
3.在html文件中配置


   Variable expressions变量:${}
   Selection Variable expressions选择表达式:*{}
   Message expressions信息:#{}
   link URL expressionsurl:@{}
   Fragment expressions片段表达:~{}

样例:
Controller层

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("msg","hello ,Spriboot");
        return "test";
    }
}

templates




    
    Title


4.thymeleaf语法

二:springboot中application.properties常用配置和必记知识

1.application.propertries

/#关闭默认图标
spring.mvc.favicon.enabled=false
#关闭模板引擎的缓存
spring.thymeleaf.cache=false

2.使用@Controller注解跳转页面必须增加thymeleaf配置


            org.springframework.boot
            spring-boot-starter-thymeleaf
        

3.@RestController和@Controller的区别
(1).@RestController无法返回页面,@Controller可以
(2).@RestController可以直接返回数据,可以自己在方法李确定返回数据的类型@Controller必须借助@RequestBody

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

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

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