1. <!DOCTYPE html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. </head>
    7. <body>
    8. <h1>首页</h1>
    9. <!--写死路径后,修改了也要修改,不方便-->
    10. <a href="/SpringMVC/target">访问target.html页面</a><br>
    11. <!--使用thymeleaf,th:href 自动添加上下文路径-->
    12. <a th:href="@{/target}">访问target.html页面</a>
    13. </body>
    14. </html>

    注意:
    image.png

    /SpringMVC/target 表示:[http://localhost:8080/](http://localhost:8080/SpringMVC/) + **SpringMVC** + **/target**
    加上视图解析器就是:[http://localhost:8080/](http://localhost:8080/SpringMVC/) + **SpringMVC** +** `WEB-INF/templates/` + `/target` + `.html**`

    而thymeleaf 的 **th**:href 是得到完整的上下文路径:
    ![image.png](https://cdn.nlark.com/yuque/0/2021/png/13019604/1629218549201-7a2e2e60-5494-40af-8f8f-5fad12171f5c.png#align=left&display=inline&height=163&margin=%5Bobject%20Object%5D&name=image.png&originHeight=163&originWidth=488&size=12603&status=done&style=none&width=488)