1. 程式人生 > 其它 >springboot-專案實戰:刪除,登出,404

springboot-專案實戰:刪除,登出,404

承接:springboot-專案實戰:修改員工

1 刪除員工

修改刪除員工的按鈕

list.html

<a class="btn btn-sm btn-danger" th:href="@{/employeeDelete/}+${employee.getId()}">刪除</a>

在員工控制器中編寫刪除員工的方法

EmployeeController.java

//刪除員工
@RequestMapping("/employeeDelete/{id}")
public String employeeDelete(@PathVariable("id")Integer id){
    employeeDao.employeeDelete(id);
    return "redirect:/getAllEmployee";
}

2 錯誤頁面

在templates目錄下新建一個資料夾error,並把之前的 靜態檔案404.html移動進去

修改一下404.html,刪除原有的導航欄和側邊欄程式碼,改為插入公共元件的方式引入,下面之展示了修改部分的程式碼

404.html

<div th:replace="~{commons/commons::topbar}"></div>
   <div class="container-fluid">
      <div class="row">
         <div th:replace="~{commons/commons::sidebar(active='list.html')}"></div>
         <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
            <h1>404</h1>
         </main>
      </div>
   </div>

在springboot中如果想自定義錯誤頁面,就採用這種方式,注意必須在templates目錄下建立資料夾,名稱也必須是 error, 裡面的頁面名稱也必須是錯誤的編號 比如404.html,500.html,只有這樣才能被springboot識別

3 登出賬戶

修改登出賬戶的按鈕

commons.html

<ul class="navbar-nav px-3">
    <li class="nav-item text-nowrap">
        <a class="nav-link" th:href="@{/user/logout}">Sign out</a>
    </li>
</ul>

在登入控制器中編寫登出賬戶的方法

LoginController.java

@RequestMapping("/user/logout")
public String logout(HttpSession session){
    session.invalidate();
    return "redirect:/index.html";
}

4 啟動程式測試

登入成功後,切換到員工列表,然後點選刪除,刪掉id為1001的員工

確認刪除成功後,在位址列輸入一個不存在的地址進行訪問

頁面跳轉到了我們自定義的404頁面,說明我們自定義錯誤頁面成功,然後點選登出按鈕

點選登出後,頁面跳轉到登入頁面,這時我們在位址列輸入,訪問mian.html

會被攔截器攔截,彈出到登入頁面

說明賬戶已經成功退出,至此三個功能全部實現

5 專案原始碼

springboot員工管理系統.rar