ThymeLeaf

作者: 宋song一 | 来源:发表于2018-09-21 17:19 被阅读23次
     <!-- 引入命名空间 -->
      <html xmlns:th="http://www.thymeleaf.org" >
     <body>
    
    <!-- thymeleaf的模板取值显示都是写在标签里面。使用th命名空间来取值 
    
     1.  取值的写法是: ${属性的名称} , 这种写法不能独立存在,
    必须放在th:xxx="${属性名称}"
    
    2. 如果数据要放置到标签的中间形成文本,那么使用th:text,
    如果数据要填充到标签的value属性中,那么使用th:value  -->
    <p th:text="${name}"></p> <br/>
    
    <input type="text" th:value="${name}"/>
      
     </body>  
    
    • 变量表达式

    用于取值 ${变量名}

    • URL表达式

    用于指定超链接 @{路径}

    如果是需要从model中取值的话,写法为

    th:href="@{${model中的name值}}"。

    有的时候我们不止需要从model中进行取值,还需写字符串与model中的值进行拼接,写法为

    th:href="@{'字符串'+${model中的name值}}"。

    3. 存值手法

    a. 使用Model参数存值

     @RequestMapping("test")
     public String test(Model model){
    
         model.addAttribute("address","深圳");
    
         return "testpage";
     }
    

    b. 使用ModelAndView对象存值

    @RequestMapping("test2")
    public ModelAndView test2(){
    
        ModelAndView modelAndView = new ModelAndView();
    
        //1. 存数据
        modelAndView.addObject("address","北京");
    
        //2. 指定视图
        modelAndView.setViewName("testPage");
    
        return modelAndView;
    }
    

    相关文章

      网友评论

          本文标题:ThymeLeaf

          本文链接:https://www.haomeiwen.com/subject/bhlznftx.html