當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot 路径处理
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot 路径处理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在進行普通的WEB開發(fā)過程之中,路徑的處理操作是最為麻煩的,因為如果想要進行方便的路徑定位最好使用完整路徑,需要明確的寫上你的協(xié)議、你的主機的名稱、端口、虛擬目錄的名稱。這些處理的難點在thymeleaf里面徹底消失了,因為路徑訪問變得相當容易。1、在src/main/static/js目錄里面創(chuàng)建有一個main.js
window.onload = function(){console.log("****** www.baidu.com ******");
}
2、如果后續(xù)要想進行方便的訪問,可以使用"@{}"進行一個訪問的定位處理.message_show_style.html<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>SpringBoot模板渲染</title><script type="text/javascript" th:src="@{/js/main.js}"></script><link rel="icon" type="image/x-icon" href="/images/favicon.ico" /><meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body><p th:utext="'官方網(wǎng)站:' + ${url} + '、數(shù)學計算:' + (1 + 2)"/>
</body>
</html>
message_info.html<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>SpringBoot模板渲染</title><script type="text/javascript" th:src="@{/js/main.js}"></script><meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body><h1>www.baidu.com</h1>
</body>
</html>@RequestMapping(value = "message/showStyle", method = RequestMethod.GET)public String showStyle(Model model) { // 通過model可以實現(xiàn)內(nèi)容的傳遞model.addAttribute("url", "<span style='color:red'>www.mldn.cn</span>");return "message/message_show_style";}
以后如果要想在頁面中進行資源的定位一定要使用"@{路徑}"完成,而且一定要使用thymeleaf標簽.
3、而且在以后進行指定路徑訪問的時候你也可以使用"@{}"形式訪問控制器@RequestMapping(value = "/show", method = RequestMethod.GET)public String show(String mid,Model model) { // 通過model可以實現(xiàn)內(nèi)容的傳遞model.addAttribute("url", "www.1234.cn"); // request屬性傳遞包裝model.addAttribute("mid", mid); // request屬性傳遞包裝// 此處只返回一個路徑, 該路徑?jīng)]有設置后綴,后綴默認是*.htmlreturn "message/message_show"; }<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>SpringBoot模板渲染</title><script type="text/javascript" th:src="@{/js/main.js}"></script><link rel="icon" type="image/x-icon" href="/images/favicon.ico" /><meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body><p th:utext="'官方網(wǎng)站:' + ${url} + '、數(shù)學計算:' + (1 + 2)"/><a th:href="@{/show}" th:text="訪問"></a></body>
</html>
<a href="aa.html" th:href="@{/show}" th:text="訪問"></a>如果你在使用超鏈接的時候設置了"href" 和 "th:href" 的話,那么前者的html元素的默認屬性將無效.
?
總結(jié)
以上是生活随笔為你收集整理的SpringBoot 路径处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot 信息输出
- 下一篇: SpringBoot 处理内置对象