在进行普通的 WEB 开发过程之中,路径的处理操作是最为麻烦的,因为如果要想进行方便的路径定位最好使用完整路径,需要明确的写上你的协议、你的主机名称、端口、虚拟目录的名称。这些处理的难点在 thymeleaf 彻底消失了,因为其路径访问变得相当容易。
在 src/main/view/static/js 目录里面创建有一个 main.js 文件;
window.onload = function() {
console.log("****** www.mldn.cn ******") ;
}
2.如果后续要想进行方便的访问,可以使用"@{}"进行一个访问的定位处理。
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/mldn.ico"/>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<a href="aa.html" th:href="@{/show}">访问</a>
<hr/>
<p th:text="'官方网站:' + ${url}"/>
<p th:utext="'官方网站:' + ${url}"/>
<hr/>
<h2 th:text="#{welcome.url}"/>
<h2 th:text="#{welcome.msg('xiaoli')}"/>
<hr/>
<p th:utext="'官方网站:' + ${url} + '、数学计算:' + (1 + 2)"/>
</body>
</html>
<script type="text/javascript" th:src="@{/js/main.js}"></script>
以后如果要想在页面中进行资源的定位一定要使用"@{路径}"完成,而且一定要使用thymeleaf标签。
3.而且在以后进行制定路径访问的时候你也可以使用"@{路径}"形式访问Action控制器
<a th:href="@{/show}">访问</a>
<a href="aa.html" th:href="@{/show}">访问</a>
如果你在使用超链接的时候设置了"href"和"th:href"的话,那么前者的html元素属性将无效。