Thymeleaf 域对象操作 URL表达式

从 HttpServletRequest 中获取数据
从 HttpSession 中获取数据
从 ServletContext 中获取数据

@GetMapping("/scope")
public String scope(HttpServletRequest request, Model model) {
    request.setAttribute("req", "HttpServletRequest");
    request.getSession().setAttribute("ses", "HttpSession");
    request.getSession().getServletContext().setAttribute("app", "Application");
    return "scope";
}

使用 Idea 查看源码可以得到 session 和 application 都是一个 Map 对象,而 HttpServletRequest 是一个内置对象。

Request:<span th:text="${#httpServletRequest.getAttribute('req')}"></span> <br />
Session:<span th:text="${session.ses}"></span> <br />
Session:<span th:text="${application.app}"></span> <br />

th:href
th:src

基本语法构成:
@{}

绝对路径

<a th:href="@{http://www.baidu.com}">百度</a>

这种写法和下面的原本的写法是一致的

<a href="http://www.baidu.com">百度</a>

相对路径
相对于当前项目的根

<a th:href="@{/show}">相对路径</a>

这个会调用 controller 中的 show 请求方法
相对于当前服务器路径的根

<a th:href="@{~/project2/show}">

相对于服务器的根 就是添加一个 ~

在 url 中实现参数传递

<a th:href="@{/show(id=1,name='cc')}">相对路径-传参</a> 

这个相当与请求: /show?id=1&name=cc

你可能感兴趣的:(Thymeleaf)