为什么我在Thymeleaf中的锚标记未重定向到本地文件

问题描述

我正在尝试使用aThymeleaf中的Spring boot标记重定向到另一个本地网页,但这是 不工作。我正在从index.html重定向到同一文件夹中的addEdit.html

这是我的代码。

index.html

<div class="container">
          <p th:text="${message}"></p>
          <a th:href="@{/addEdit.html}" class="btn btn-outline-info">Add Employee</a> //not working
           <table class="table table-bordered table-dark">
               <thead class="">
                   <tr> 
                        <th>#</th>
                        <th>Name</th>
                        <th>Departmen</th>
                        <th>Position</th>
                         <th></th>
                         <th></th>
                       
                   </tr>
               </thead>
               
               <tbody>
                    <tr th:each="employee : ${employees}" >
                         <th th:text="${employee.id}"></th>
                         <td th:text="${employee.name}"></td>
                         <td th:text="${employee.department}"></td>
                         <td th:text="${employee.position}"></td>
                         <td>
                             <form action="delete">
                                <input type="submit" value="Delete" class="btn btn-outline-warning"/>
                             </form>
                         </td>
                          <td>
                             <form action="edit">
                                <input type="submit" value="Edit" class="btn btn-outline-info"/>
                             </form>
                         </td>
                    </tr>
                   
               </tbody>
               
           </table>
          </div>

我的EmployeeController

 @Autowired
 private employeeRepo repo;
 
 @RequestMapping("/")
 public String home(Model model) {
     List<Employee> list = new ArrayList<>();
     list = repo.findAll();
     model.addAttribute("employees",list);
     return "index";
 }
 
 @PostMapping("/addEmployee")
 public void addEmployee(Employee employee,Model model) {
     repo.save(employee);
     model.addAttribute("message","Add Successfully");
     home(model);
 }

我的addEdit.html

<div class="container bg-light">
      <form action="addEmployee">
            <input class="form-control form-control-sm" type="text" placeholder="Name" name="name"><br>
            <input class="form-control form-control-sm" type="text" placeholder="Department" name="department"><br>
            <input class="form-control form-control-sm" type="text" placeholder="Position" name="postion"><br/>
            <input type="submit" value="Add Employee" class="btn btn-outline-success btn-lg btn-block">
      </form>
 </div>

解决方法

您不应在链接中包含.html。该链接应指向您的控制器公开的URL。例如,目前没有控制器方法公开/addEdit网址。

因此更新您的控制器:

@GetMapping
public String addEmployee(Model model) {
  // add model attributes here as needed first

  return "addEdit" // This refers to the view,so 'addEdit.html'
}

现在将链接更新为:

<a th:href="@{/addEdit}" class="btn btn-outline-info">Add Employee</a>

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...