
上QQ阅读APP看书,第一时间看更新
3.5 路径处理
Web开发过程中,路径的处理操作是最为麻烦的。如果要进行准确的路径定位,最好使用完整的路径,并明确写上用户的协议、主机名称、端口以及虚拟目录的名称。这些处理的难点在于Thymeleaf彻底消失了,因为其路径访问变得相当容易,只需要在动态页面中使用“@{路径}”即可访问。
提示:回顾原始实现。
在进行Web项目开发的过程中,相信不少开发者都编写过如下的类似代码:

而后再使用<base>元素(<base href="<%=basePath%>"/>)进行引用,可以解决路径操作问题。
1.【mldnboot-thymeleaf项目】在ThymeleafController中创建一个新的方法,用于跳转。

2.【mldnboot-thymeleaf项目】建立src/main/view/templates/message/message_path.html页面。

在message_path.html页面中继续引用之前定义了的资源,而采用“@{路径}”的形式使得资源引用也十分简单,页面运行效果如图3-7所示。

图3-7 Web资源引入