进阶SpringBoot之员工管理系统(6)修改、删除、404 与注销

EmployeeController.java:

@GetMapping("/emp/{id}")  Restful 风格

import com.demo.web.dao.DepartmentDao;
import com.demo.web.dao.EmployeeDao;
import com.demo.web.pojo.Department;
import com.demo.web.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 {
    @Autowired //自动装配
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;

    @RequestMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emps",employees);
        return "emp/list";
    }

    @GetMapping("/emp")
    public String toAddpage(Model model){
        //查出所有部门信息
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/add";
    }

    @PostMapping("/emp")
    public String addEmp(Employee employee){
        employeeDao.save(employee); //调用底层业务方法保存员工信息
        return "redirect:/emps";
    }

    //去员工的修改页面
    @GetMapping("/emp/{id}")
    public String toUpdateEmp(@PathVariable("id") Integer id, Model model){
        //查出原来的数据
        Employee employee = employeeDao.getEmployeeById(id);
        model.addAttribute("emp",employee);
        //查出所有部门信息
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/update";
    }

    @RequestMapping("/updateEmp")
    public String updateEmp(Employee employee){
        employeeDao.save(employee);
        return "redirect:/emps";
    }
}

update.html:

<div th:insert="~{commons/commons::topbar}"></div>

<div class="container-fluid">
	<div class="row">

		<div th:insert="~{commons/commons::sidebar(active='list.html')}"></div>

		<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
			<form th:action="@{/emp}" method="post">
				<div class="form-group"><label>LastName</label>
					<input th:value="${emp.getLastName()}" class="form-control" placeholder="zhang" type="text" name="lastName">
				</div>
				<div class="form-group"><label>Email</label>
					<input th:value="${emp.getEmail()}" class="form-control" placeholder="24736743@qq.com" type="email" name="email">
				</div>
				<div class="form-group"><label>Gender</label><br/>
					<div class="form-check form-check-inline">
						<input th:checked="${emp.getGender()==1}" class="form-check-input" name="gender" type="radio" value="1">
						<label class="form-check-label">男</label>
					</div>
					<div class="form-check form-check-inline">
						<input th:checked="${emp.getGender()==0}" class="form-check-input" name="gender" type="radio" value="0">
						<label class="form-check-label">女</label>
					</div>
				</div>
				<div class="form-group"><label>department</label>
					<select class="form-control" name="department.id">
						<option th:selected="${dept.getId()==emp.getDepartment().getId()}" th:each="dept:${departments}" th:text="${dept.getDepartmentName()}" th:value="${dept.getId()}"></option>
					</select>
				</div>
				<div class="form-group">
					<label>Birthday</label>
					<input th:value="${#dates.format(emp.getBirthday(), 'yyyy-MM-dd HH:mm')}" class="form-control" placeholder="2024-08-16" type="text" name="birthday">
				</div>
				<button class="btn btn-primary" type="submit">修改</button>
			</form>
		</main>
	</div>
</div>

首页编辑链接:

<a class="btn btn-sm btn-primary" th:href="@{/emp/}+${emp.getId()}">编辑</a>

点击编辑后,员工信息根据 id 显示

 删除链接:

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

 调用 delete 方法:

    //删除员工
    @GetMapping("/delemp/{id}")
    public String deleteEmp(@PathVariable("id") int id){
        employeeDao.delete(id);
        return "redirect:/emps"; //重定向到首页
    }

删除成功!

404.html 放在 error 目录下

 

注销:

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

LoginController.java:

session.invalidate();   使 session 无效

package com.demo.web.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController {

    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Model model,
                        HttpSession session){

        //具体业务
        if(!StringUtils.isEmpty(username) && "demo".equals(password)){
            session.setAttribute("loginUser",username);
            return "redirect:/main.html"; //redirect重定向到登录成功页面
        }else {
            model.addAttribute("msg","用户名或密码错误");
            return "index";
        }
    }
    
    //注销
    @RequestMapping("/user/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index.html";
    }
}

点击注销后重定向到登录页

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值