首页 > 解决方案 > Spring Boot 和 Thymeleaf:通过导航栏链接切换页面

问题描述

我正在利用以下方式开展汽车经销商项目:

爪哇 8

弹簧靴

百里香叶

AWS mySQL 数据库

我目前的问题是我无法使用导航栏在 html 页面之间移动。我有 5 个 html 页面(index.html、customer.html、employee.html、vehicle.html 和 transaction.html)

index.html 中的代码:

    <a class="active" th:href="@{/index.html}"><i class="(Put css class here)"></i> Home</a>
    <a th:href="@{/vehicle.html}"><i class="(Put css class here)"></i> Vehicles </a>
    <a th:href="@{/customer.html}"><i class="(Put css class here)"></i> Customers </a>
    <a th:href="@{/employee.html}"><i class="(Put css class here)"></i> Employees </a>
    <a th:href="@{/transaction.html}"><i class="(Put css class here)"></i> Transactions </a>

来自 IndexController 的代码:

@Controller
public class IndexController {

@RequestMapping("/index")
public String index(){
    return "index";
}

}

该页面在首次打开时在 localhost:8088 正常加载,但是当我单击“主页”或任何其他按钮时,我得到一个 404。

如果有人能简单地指出我的大致方向,我将不胜感激。

标签: javamysqlamazon-web-servicesspring-bootthymeleaf

解决方案


相反,尝试在控制器中为这些不同的链接发出 GetMapping 请求。

例子:

    @GetMapping("/customer")
  public String getCustomer(){
    return "customer.html";
 }

在您的 html 类(导航栏)中:

    <a th:href="@{/customer}"><i class="(Put css class here)"></i> Customers </a>

*确保删除 .html,但这取决于您在配置中的配置方式,但我相信这是默认配置。


推荐阅读