thymeleaf中跳轉地址的使用


最近在使用thymeleaf時,遇到一個問題,比如要跳轉的地址是localhost:8080/pro/a/b,跳轉后發現變成了

localhost:8080/pro/path/a/b,這樣地址中多了個path,顯然是錯誤的。其實在跳轉地址前加 / 和不加 / 是不一樣的。

頁面跳轉地址前加 /,意味着跳轉到項目的根目錄,不加 / ,意味着從當前頁地址跳轉。

解決的辦法也很簡單,只要獲取上下文路徑就可以了。由於使用的thymeleaf下面jsp一筆帶過。

下面是各種跳轉的方式。

1.jsp

jsp獲取上下文路徑:下面只是jsp獲取上下文路徑的一種方式

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 <c:set var="basePath"

 value="${pageContext.request.scheme}://${pageContext.request.serverName}: ${pageContext.request.serverPort}${pageContext.request.contextPath}" />

使用示例:<html><a href="${basePath}/jsp/index.jsp"></a></html>

 

2.thymeleaf

2.1.ajax

在thymeleaf中使用ajax提交,url:[[@{/index/ajaxtest}]]這樣就可以。

$.ajax({
        async:true,
        url:"[[@{/findById/}]]"+id,
        type:"POST",
        success:function(data){
           alert("操作成功!");
        }
 })

2.2.form表單提交

<form class="form-group" th:action="@{/login}" method="post">
      <div class="form-group">
           <label>用戶名</label>
           <input class="form-control" name="userName" type="text" placeholder="">
      </div>
      <div class="form-group">
           <label>密碼</label>
           <input class="form-control" name="password" type="password" placeholder="">
      </div>
      <div class="text-right">
           <button class="btn btn-primary" type="submit">登錄</button>
           <button class="btn btn-danger" data-dismiss="modal">取消</button>
      </div>
 </form>

2.3.a標簽

<a th:href="@{'searchInfo/'+${info.id}}">查看</a>

這樣使用thymeleaf會自動獲取上下文參數。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM