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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章