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

thymeleaf 简介&语法

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

thymeleaf 简介&语法

1 thymeleaf 简介
2 thymeleaf 语法 - th 属性
3 thymeleaf 语法 - 内置方法
4 thymeleaf 语法 - 标准表达式


=========================================================

1 thymeleaf 简介 


  和过去学的jsp很象,是一个模板引擎
  
  1)创建 Springboot maven 项目  
       pom 中的配置如下
      
                org.springframework.boot
                spring-boot-starter-parent
                2.2.9.RELEASE
      

       
     
            
                org.springframework.boot
                  spring-boot-starter-web
            

     

     
  2)导入thymeleaf相关的启动器
      
         org.springframework.boot
           spring-boot-starter-thymeleaf
      

       
    3)创建模板文件
       src/main/resources/ 
       建一个叫 templates 的目录 (必须是这个名字)
       里面放模板文件  demo.html 内容:
               
        
        
        
        
        
        
        
            这是div
            

这是段落  


        
        

 4) 创建控制层 UserController
            package com.controller;
            import org.springframework.stereotype.Controller;
            import org.springframework.ui.ModelMap;
            import org.springframework.web.bind.annotation.RequestMapping;
            
            @Controller
            public class UserController {    
                @RequestMapping("/gogogo")
                public String gotoDemo(ModelMap m) {
                    m.put("msg","模板引擎测试成功");
                    m.put("info","我是打酱油的");
                    return "demo"; //==>src/main/resources/templates/demo.html    
                }
            }
            
    5) 创建启动类
        package com;
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        
        @SpringBootApplication
        public class App {
            public static void main(String[] args) {
                SpringApplication.run(App.class, args);
            }
        }
    
    6) 启动,访问
     http://localhost:8080/gogogo


     2 thymeleaf 语法 - th 属性


        建 配置文件 application.properties 
        放在 src/main/resources/ 
        
        spring.thymeleaf.cache=false
        关闭它的缓存
        
        一般来说,html元素有的属性,thymeleaf 都有
     ① th:text  th:utext   设置文本元素的内容
     ② th:value  设置元素的value值
     ③ th:each 循环
     ④ th:if  判断
     ⑤ th:object 声明变量,一般和 *{} 配合使用
     ⑥ th:fragment 声明片段
     ⑦ th:insert   th:replace  th:include  引入片段
     ⑧ th:attr 访问属性
        
     //控制层
        @RequestMapping("/thymeleaf")
            public String  thymeleaf(ModelMap m) {
                m.put("thText","这是thText的内容");
                m.put("thUext","这是thUext的内容,会解析标签");
                
                m.put("thValue","默认是100");
                
                m.put("myList",Arrays.asList("增加","删除","修改","退出","锁定"));
                
            //    m.put("msg","这是一个不空的字符串,你中奖子");
                m.put("msg",null);
                
                UserInfo user=new UserInfo();
                user.setId(9);
                user.setUserName("刘铁轮");
                user.setPassword("12345");
                user.setNote("超级管理员");
                m.put("user",user);
            
                return "tymeleaf-pages/thymeleaf";
            }
            
    页面:  src/main/resources/templates/tymeleaf-pages/thymeleaf.html
        
        
        
        
        
        
        
            


            


            
            
         
            


                


  •             
                
                


                    


  •             
                
                


                    这是一个段落 ,表示我中奖了
                


                
                
                
                
                
                
                
                    
                    
                    
                    
                
            
            

     

    3 thymeleaf 语法 - 内置方法


      ① strings  处理字符串 (基本上可以认为,String类常用的处理函数它都有)
                                equals,length,trim,toUpperCase, replace,endsWith,contains
      ② numbers 数值格式化 比如 formatDecimal 
      ③ bools 常用的方法有 isTrue,isFalse
      ④ arrays 处理数组 ,toArray,length,isEmpty,contains,contiansAll
      ⑤ lists,sets 集合方法 toList, size,isEmpty,contains,sort
      ⑥ maps 处理map集合 size,containsKey,containsValue ,isEmpty
      ⑦ dates  处理日期  fomat ,year,month,day,hour,createNow 
      
          @RequestMapping("/varexpressions")
        public String  varexpressions(ModelMap m) {
           m.put("str", "this is a cat"); 
           m.put("myArray", new Integer[]{1,3,5,7,9});  //必须是Integer
           m.put("myList", Arrays.asList(9,3,4,6,1,5,2,4,6,8,10));
           
           Map myMap=new HashMap();
           myMap.put("一号","川建国");
           myMap.put("二号","拜振华");
           m.put("myMap",myMap);  
           m.put("birthday",new Date());

           return "tymeleaf-pages/varexpressions";    
        }
      
        在 tymeleaf-pages/varexpressions.html 中:
            
            
            
            
            Insert title here
            
            
              
                           传过来的字符串不空,则显示以下内容:
                     



                     

    原始串:


                     

    变大写:


                     

    equals:


                     

    indexOf:
                     

    截取:
                     

    包含:
              
               
              
                       传过来的数组不空,则显示以下内容:
                     



                     

    长度:


                     

    包含:
              
               
              
                           传过来的list集合不空,则显示以下内容:
                     



                     

    size:  


                     

    包含:


                     

    排序  

     
              
               
              
                       

    size:


                       

    是否包含某个key:


                       

    是否包含某个value:


              
               
              
                     

    原始:

     //Fri Oct 29 15:44:25 CST 2021
                     

    format:

    //2021年10月29日 下午03时44分25秒
                     

    自定制:  

    //2021-10-29 15:48:17
                     

    年:


                     

    月:


                     

    月的中文名:


                     

    周几:


                     

    周几中文名:


                     

    分钟:


                     

    当前时间:


              
            
            
            

     4 thymeleaf 语法 - 标准表达式


            ① ${...}  变量表达式
            ② *{...}  选择表达式
            ③ #{...}  消息表达式
            ④ @{...}  链接表达式
            ⑤ ~{...}  片段表达式
            
            ① ${...}  变量表达式
               变量表达式有丰富的内置方法 
               可以用它访问对象的属性
               可以使用 ctx, vars, locale,request,response,session,servletContext 等内置对象
                 说明: 
                  ctx :上下文对象
                  vars:上下文变量
                  locale: 上下文语言环境
                  request: HttpServltRequest
                  response:HttpServletResponse
                  session: 会话 HttpSession
                  servletContext :ServletContext 对象 (相当于jsp中的 application对象)
                    
                    //例 
                    
                    
                    
          ② *{...}  选择表达式
                
                    
                    
                    
                    
                
                
          ③ #{...}  消息表达式
             从配置文件中提取出信息,一般用于国际化场景
            
             
             配置文件一般有多份
              中文:userName:用户名
              英文:userName:this is user name
              
          ④ @{...}  链接表达式
             不管是静态资源的引入,还是表单的请求,凡是有url的地方,都可以用
             链接表达式 
             
             链接表达式结构
             @{/url}
             
             //例:
                
             上面的写法用链接表达式:
              
              
             //例 
              


                 ...
                         
             //例
                  去 demo.html 
                      上面的写法用链接表达式:
                    去 demo.html
              
               //例
            去 demo.html
                上面的写法用链接表达式:
            去 demo.html
             
                  

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

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

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