Thymeleaf语法详解

1.变量输出与字符串操作

controller代码:

@RequestMapping("/hello")
  public String show(Model model){
      model.addAttribute("uname","张三ZhangSan");
      model.addAttribute("age",22);
      model.addAttribute("dates",new Date());
      return "hello";
  }

 

html代码:

  • th:text   在页面中输出值

<span th:text="${uname}"></span><br/>
  • th:value  可以将一个值放入到 input 标签的 value

     <input type="text" th:value="${age}"/><br/>

2.判断字符串是否为空等方法

     strings.isEmpty(uname):<span th:text="${#strings.isEmpty(uname)}"></span><br/>
     strings.contains(uname,'张'):<span th:text="${#strings.contains(uname,'张')}"></span><br/>
     strings.startsWith(uname,'张'):<span th:text="${#strings.startsWith(uname,'张')}"></span><br/>
     strings.endsWith(uname,'n'):<span th:text="${#strings.endsWith(uname,'n')}"></span><br/>
     strings.length(uname):<span th:text="${#strings.length(uname)}"></span><br/>
     strings.indexOf(uname,'三'):<span th:text="${#strings.indexOf(uname,'三')}"></span><br/>
     strings.substring(uname,3,6):<span th:text="${#strings.substring(uname,3,6)}"></span><br/>
     strings.toLowerCase(uname):<span th:text="${#strings.toLowerCase(uname)}"></span><br/>
     strings.toUpperCase(uname):<span th:text="${#strings.toUpperCase(uname)}"></span><br/>
     dates.format(dates):<span th:text="${#dates.format(dates)}"></span><br/>
     dates.format(dates,'yyy/MM/dd'):<span th:text="${#dates.format(dates,'yyy/MM/dd')}"></span><br/>
     dates.year(dates):<span th:text="${#dates.year(dates)}"></span><br/>
     dates.month(dates):<span th:text="${#dates.month(dates)}"></span><br/>
     dates.day(dates):<span th:text="${#dates.day(dates)}"></span><br/>

3.条件判断

controller代码:

 @RequestMapping("/hello2")
    public String show2(Model model){
        model.addAttribute("sex","男");
        model.addAttribute("id",2);
        return "hello2";
}

 

html代码:

  • th:if

 <span th:if="${sex}=='男'">性别:男</span>
 <span th:if="${sex}=='女'">性别:女</span>

 

  • th:switch

 <div th:switch="${id}">
     <span th:case="1">ID为1</span>
     <span th:case="2">ID为2</span>
     <span th:case="3">ID为3</span>
 </div>

4.迭代遍历

  • 迭代list

controller代码:

 @RequestMapping("/hello3")
    public String show3(Model model){
        List lists=new ArrayList();
        User user1=new User("张三",23,"女");
        User user2=new User("李四",63,"男");
        User user3=new User("王五",33,"女");
        lists.add(user1);
        lists.add(user2);
        lists.add(user3);
        model.addAttribute("lists",lists);
        return "hello3";
    }

html代码:

<table border="1">
    <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>Index</th>
        <th>Count</th>
        <th>Size</th>
        <th>Even</th>
        <th>Odd</th>
        <th>First</th>
        <th>Last</th>
    </tr>

    <tr th:each="u,var: ${lists}">
        <td th:text="${u.name}"></td>
        <td th:text="${u.age}"></td>
        <td th:text="${u.sex}"></td>
        <td th:text="${var.index}"></td>
        <td th:text="${var.count}"></td>
        <td th:text="${var.size}"></td>
        <td th:text="${var.even}"></td>
        <td th:text="${var.odd}"></td>
        <td th:text="${var.first}"></td>
        <td th:text="${var.last}"></td>

    </tr>
</table>

 

  • 迭代map

controller代码:

@RequestMapping("/hello4")
    public String show4(Model model){
        Map<String,User> maps=new HashMap<String,User>();
        maps.put("user1",new User("张三",23,"女"));
        maps.put("user2",new User("李四",63,"男"));
        maps.put("user3",new User("王五",33,"女"));
        model.addAttribute("maps",maps);
        return "hello4";
    }

 

html代码:

 

<table border="1">
         <tr>
             <th>姓名</th>
             <th>年龄</th>
             <th>性别</th>
         </tr>

         <tr th:each="map : ${maps}">
             <td th:each="entry:${map}" th:text="${entry.value.name}"></td>
             <td th:each="entry:${map}" th:text="${entry.value.age}"></td>
             <td th:each="entry:${map}" th:text="${entry.value.sex}"></td>
         </tr>
     </table>

5.域对象操作

  • HttpServletRequest

  • HttpSession

  • ServletContext

controller代码:

 

@RequestMapping("/hello5")
    public String show5(HttpServletRequest request){
        request.setAttribute("msg","HttpServletRequest");
        request.getSession().setAttribute("msg","HttpSession");
        request.getServletContext().setAttribute("msg","ServletContext");
        return "hello5";
    }

 

html代码: 

 httpServletRequest:<span th:text="${#httpServletRequest.getAttribute('msg')}"></span><br/>
 httpSession:<span th:text="${session.msg}"></span><br/>
 application:<span th:text="${application.msg}"></span><br/>

6.URL表达式语法

  • 绝对路径

  • 相对路径

  • 在URL中实现参数传递

  • 在URL中通过restful风格进行参数传递

controller代码:

@RequestMapping("/{page}")
    public String show6(@PathVariable String page, Integer id, String name){
        return "hello6";
    }

html代码: 

    <a th:href="@{http://www.baidu.com}">绝对路径</a><br/>
    <a th:href="@{/show}">相对路径</a><br/>
    <a th:href="@{~/project2/resourcename}">相对于服务器的根</a><br/>
    <a th:href="@{/show(id=1,name=zhagnsan)}">相对路径-传参</a><br/>
    <a th:href="@{/path/{id}/show(id=1,name=zhagnsan)}">相对路径-传参-restful</a><br/>

 

注:在html中如果,th标签报错,出现以下情况:

需要在html中添加如下内容:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值