問題說明
寫博客頁面在發表博客後我想跳轉到博客詳情頁。這裏面我用到了:window.location.href="localhost:8080/post/detail/123";
這種方式的弊端是寫死了項目路徑。爲了獲得項目路徑,我找了找貌似thymeleaf沒有提供較好的方式,網上提供的那些解答都文不對題。
最後發現使用<base>標籤可以在html頁面<head>標籤中一次配置:
<base th:href="${#request.getContextPath()}+'/'">
這個頁面的url都會自動加上項目基本路徑。
支持 <a>、<img>、<link>、<form> 標籤中的 URL,當然還有我上面的跳轉URL。
實際案例: