当前位置: 首页 > news >正文

【WEEK11】 【DAY6】Employee Management System Part 7【English Version】

2024.5.11 Saturday
Continued from 【WEEK11】 【DAY5】Employee Management System Part 6【English Version】

Contents

  • 10.8. Delete and 404 Handling
    • 10.8.1. Modify list.html
    • 10.8.2. Modify EmployeeController.java
    • 10.8.3. Restart
    • 10.8.4. 404 Page Handling
      • 10.8.4.1. Move the 404.html file into it
      • 10.8.4.2. Restart and Run
    • 10.8.5. Logout
      • 10.8.5.1. Modify commons.html
      • 10.8.5.2. Modify LoginController.java
      • 10.8.5.3. Restart
  • 10.9. Summary
    • 10.9.1. How to Build a Website
    • 10.9.2. Templates

10.8. Delete and 404 Handling

10.8.1. Modify list.html

Insert image description here

<a class="btn btn-sm btn-danger" th:href="@{/delemp/{id}(id=${emp.getId})}">Delete</a><!--@{/delemp/}+${emp.getId()} can also be written this way-->

10.8.2. Modify EmployeeController.java

package com.P14.controller;import com.P14.dao.DepartmentDao;
import com.P14.dao.EmployeeDao;
import com.P14.pojo.Department;
import com.P14.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;import java.util.Collection;@Controller
public class EmployeeController {// Query all employees@AutowiredEmployeeDao employeeDao;@Autowired  // AutowireDepartmentDao departmentDao;@RequestMapping("/emps")    // Whenever dashboard.html requests th:href="@{emps}" (line 89), it will be redirected to @RequestMapping("/emps")public String list(Model model){    // Then it will query all employees, and modify: how to display them on the front-end pageCollection<Employee> employees = employeeDao.getAll();model.addAttribute("emps",employees);return "emp/list";}@GetMapping("/emp") // Get request for redirectionpublic String toAddpage(Model model){// Retrieve information of all departmentsCollection<Department> departments = departmentDao.getDepartment();model.addAttribute("departments",departments);return "emp/add";}@PostMapping("/emp")public String addEmp(Employee employee){// Adding operation forwardSystem.out.println("save=>"+employee);employeeDao.save(employee); // Call the underlying business method to save employee informationreturn "redirect:/emps";    // After clicking "Add" on the "Add Employee" page, redirect to the "Employee Management" page}// Go to the employee update page -> should be able to retrieve the original data@GetMapping("/emp/{id}")public String toUpdateEmp(@PathVariable("id") Integer id,Model model){Employee employee = employeeDao.getEmployeeById(id);model.addAttribute("emp",employee);// Retrieve information of all departmentsCollection<Department> departments = departmentDao.getDepartment();model.addAttribute("departments",departments);return "emp/update";}@PostMapping("/updateEmp")public String updateEmp(Employee employee){employeeDao.save(employee);return "redirect:/emps";}// Delete employee@GetMapping("/delemp/{id}")public String deleteEmp(@PathVariable("id") int id){employeeDao.delete(id);return "redirect:/emps";}}

10.8.3. Restart

Insert image description here
Insert image description here

10.8.4. 404 Page Handling

Create error folder

10.8.4.1. Move the 404.html file into it

Insert image description here
To prevent loss of page style, add two slashes //
Insert image description here

10.8.4.2. Restart and Run

Enter any URL that does not correspond to a page
Insert image description here

If the 404.html is not placed in the error folder, the error page will be displayed with the default browser style:
Insert image description here

10.8.5. Logout

10.8.5.1. Modify commons.html

Insert image description here

<a class="nav-link" th:href="@{/user/logout}">Sign out</a>

10.8.5.2. Modify LoginController.java

package com.P14.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;import javax.servlet.http.HttpSession;@Controller
public class LoginController {@RequestMapping("/user/login")public String login(@RequestParam("username") String username,@RequestParam("password") String password,Model model,HttpSession session){// Business logicif (!StringUtils.isEmpty(username) && "123456".equals(password)){session.setAttribute("loginUser",username);// Login successful, redirect to /main (already set to redirect to dashboard.html in MyMvcConfig)return "redirect:/main.html";}else {// Failed login messagemodel.addAttribute("msg","Invalid username or password");return "index";}}// Logout@RequestMapping("/user/logout")public String logout(HttpSession session){session.invalidate();return "redirect:/index.html";}
}

10.8.5.3. Restart

Insert image description here

  • After login, not logging out -> even if returned to the login page -> can directly access desired URL by entering it again
    Insert image description here
    Return to the login page:
    Insert image description here
    At this point, desired URLs can be accessed directly by entering them:
    For example: http://localhost:8080/emps
    Insert image description here

For example: http://localhost:8080/main.html
Insert image description here

  • After login, then logout -> back to the login page -> must login again to access desired URL
    Insert image description here
    Return to the login page:
    Insert image description here
    Attempting to access main.html by modifying the URL fails, indicating successful user logout.
    Insert image description here

10.9. Summary

10.9.1. How to Build a Website

10.9.1.1. Frontend: Design the appearance using layui
10.9.1.2. Database design (Database design challenges)
10.9.1.3. Make the frontend self-executing and independent
10.9.1.4. How to connect data interfaces: JSON, objects, all in one!
10.9.1.5. Frontend-backend integration testing

10.9.2. Templates

10.9.3. Have a set of familiar backend templates: Essential for work! Recommended to use: x-admin
10.9.4. Frontend pages: At least be able to combine a website page using frontend frameworks

  • index
  • about
  • blog
  • post
  • user

10.9.5. Make this website able to run independently!

http://www.lryc.cn/news/347591.html

相关文章:

  • 【52】Camunda8-Zeebe核心引擎-Clustering与流程生命周期
  • 从零开始的软件测试学习之旅(八)jmeter线程组参数化及函数学习
  • 图文并茂:解析Spring Boot Controller返回图片的三种方式
  • 问题处理记录 | 表输出报错 Packet for query is too large (5,214,153 > 4,194,304).
  • 数据结构_栈和队列(Stack Queue)
  • 基于docker 的elasticsearch冷热分离及生命周期管理
  • pikachu靶场(xss通关教程)
  • 实验0.0 Visual Studio 2022安装指南
  • 数据结构之----线性表
  • thinkphp5.1 模型auto
  • 企业微信创建应用(一)
  • Cosmo Bunny Girl
  • 初始化linux数据盘(3TB)分区-格式化-挂载目录
  • NFS网络文件系统的应用
  • AttributeError: module ‘PIL.Image‘ has no attribute ‘ANTIALIAS‘
  • 进程的共享主存通信实验
  • 深度缓冲技术在AI去衣中的神奇作用
  • 能效?性能?一个关于Windows下使用openssl speed进行速度测试的诡异问题
  • block性能考虑和线程安全
  • 没有公网ip,如何实现外网访问内网?
  • Python中如何将小数转化为百分数进行输出
  • 加入全球少儿编程运动:Scratch让每个孩子都能成为创造者(Scratch最新版客户端和初/中/高级学习资料整理分享)
  • 引擎:主程渲染
  • Java 高级面试问题及答案
  • 邮件的安全认证(dkim/spf/dmarc)
  • 单调栈问题
  • Hexo博客重新部署与Git配置
  • KUKA机器人专业名词解释
  • 阿里云 物联网平台 MQTT连接、数据传输
  • 栈和队列OJ练习题及解答