Thymeleaf表达式

1、标准变量表达式:th:text="${...}"

需要在属性里面填写

例如:

用户编号:<span th:text="${user.id}"></span><br/>
用户姓名:<span th:text="${user.username}"></span><br/>
用户年龄:<span th:text="${user.age}"></span><br/>

2、选择变量表达式:(星号表达式):*{}(不推荐)

*{}必须使用th:object属性进行绑定对象th:object="${user}",内容必须在绑定对象的标签内部去写。

在子标签中使用 * 来代替绑定对象${user}

例如:

<div th:object="${user}">
    用户编号:<span th:text="*{id}"></span><br/>
    用户姓名:<span th:text="*{username}"></span><br/>
    用户年龄:<span th:text="*{age}"></span><br/>
</div>

3、标准变量表达式与选择变量表达式的混合使用(不推荐)

例如:

用户编号:<span th:text="${user.id}"></span><br/>
用户姓名:<span th:text="${user.username}"></span><br/>
用户年龄:<span th:text="${user.age}"></span><br/>

4、URL路径表达式:@{...}

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>url</title>
</head>
<body>
<h1>url路径表达式:@{...}</h1>

<h2>A标签中的绝对路径(无参数)两者效果无区别</h2>
<a href="http://www.baidu.com">传统写法</a><br/>
<a th:href="@{http://www.baidu.com}">跳转到百度</a><br/>
<a href="http://localhost:8080/user/detail">传统跳转到userDetail</a><br/>
<a th:href="@{http://localhost:8080/user/detail}">跳转至userDetail</a><br/>

<h2>url表达式相对路径(无参数)(实际开发中推荐使用,没有固定的ip和端口号)</h2><br/>
<a th:href="@{/user/detail}">跳转至userDetail</a><br/>

<h2>绝对路径(带参数)(不推荐)</h2><br/>
<a href="http://localhost:8080/test?username='zhangsan'">张三</a><br/>
<a th:href="@{http://localhost:8080/test?username=lisi}">李四</a><br/>

<h2>相对路径(带参数)</h2><br/>
<a th:href="@{/test?username=wangwu}">相对路径,带参</a><br/>

<h2>相对路径(带参数,后台获取参数)</h2><br/>
<a th:href="@{'/test?username='+${id}}">相对路径,后台传参</a><br/>

<h2>相对路径带多参(多参,后台获取)</h2><br/>
<a th:href="@{'/test1?id='+${id}+'&username='+${username}+'&age='+${age}}">多参</a><br/>
<a th:href="@{/test1(id=${id},username=${username},age=${age})}">不拼接</a><br/>

<h2>RESTful风格</h2><br/>
<a th:href="@{'/test2/'+${id}}">请求路径为RESTful方式请求id</a><br/>
<a th:href="@{'/test3/'+${id}+'/'+${username}+'/'+${age}}">请求路径为RESTful方式请求id,username,age</a>
</body>
</html>
package com.bjpowernode.springboot.web;


import com.bjpowernode.springboot.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {

    @RequestMapping(value = "/user/detail")
    public ModelAndView userDetail(){
        ModelAndView mv = new ModelAndView();
        User user = new User();
        user.setId(1001);
        user.setUsername("lio");
        user.setAge(22);

        mv.setViewName("userDetail");
        mv.addObject("user",user);

        return mv;
    }

    @RequestMapping(value = "/url")
    public String url(Model model){
        model.addAttribute("id",1001);
        model.addAttribute("username","zhaoliu");
        model.addAttribute("age",21);
        return "url";
    }

    @RequestMapping(value = "/test")
    public @ResponseBody String test(String username){
        return "请求路径/test,带参数为"+username;
    }
    @RequestMapping(value = "/test1")
    public @ResponseBody String test1(Integer id,String username,Integer age){
        return "请求路径/test1,编号为"+id+",姓名为"+username+",年龄为"+age;
    }
    @RequestMapping(value = "/test2/{id}")
    public @ResponseBody String test2(@PathVariable("id") Integer id){
        return "id=,编号为"+id;
    }
    @RequestMapping(value = "/test3/{id}/{username}/{age}")
    public @ResponseBody String test3(@PathVariable("id") Integer id,
                                      @PathVariable("username") String username,
                                      @PathVariable("age") Integer age){
        return "id,编号为"+id+",姓名为"+username+",年龄为"+age;
    }
}

js、img同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值