SpringBoot狂神15-(员工管理系统-增/改/删/404/注销)

(七)增加员工实现——增

1. list页面增加添加员工按钮

首先在list.html页面增添一个增加员工按钮,点击该按钮时发起一个请求/add

<h2><a class="btn btn-sm btn-success" th:href="@{/add}">添加员工</a></h2>

然后编写对应的controller,处理点击添加员工的请求

 这里通过get方式提交请求,在EmployeeController中添加一个方法add用来处理list页面点击提交按钮的操作,返回到add.html添加员工页面,我们即将创建

@GetMapping("/add")
public String add(Model model) {
    //查出所有的部门信息,添加到departments中,用于前端接收
    Collection<Department> departments = departmentDao.getDepartments();
    model.addAttribute("departments", departments);
    return "emp/add";//返回到添加员工页面
}

2. 创建添加员工页面add

templates/emp下新建一个add.html

image-20201005002351454

我们复制list.html中的内容,修改其中表格为:

<form>
    <div class="form-group">
        <label>LastName</label>
        <input type="text" name="lastName" class="form-control" placeholder="lastname:zsr">
    </div>
    <div class="form-group">
        <label>Email</label>
        <input type="email" name="email" class="form-control" placeholder="email:xxxxx@qq.com">
    </div>
    <div class="form-group">
        <label>Gender</label><br/>
        <div class="form-check form-check-inline">
            <input class="form-check-input" type="radio" name="gender" value="1">
            <label class="form-check-label">男</label>
        </div>
        <div class="form-check form-check-inline">
            <input class="form-check-input" type="radio" name="gender" value="0">
            <label class="form-check-label">女</label>
        </div>
    </div>
    <div class="form-group">
        <label>department</label>
        <!--注意这里的name是department.id,因为传入的参数为id-->
        <select class="form-control" name="department.id">
            <option th:each="department:${departments}" th:text="${department.getDepartmentName()}" th:value="${department.getId()}"></option>
        </select>
    </div>
    <div class="form-group">
        <label>Birth</label>
        <!--springboot默认的日期格式为yyyy/MM/dd-->  实体类为:Data data
        <input type="text" name="date" class="form-control" placeholder="birth:yyyy/MM/dd">
    </div>
    <button type="submit" class="btn btn-primary">添加</button>
</form>

 我们重启主程序看看
image-20201005002133908
点击添加员工,成功跳转到add.html页面
image-20201005131019727
下拉框中的内容不应该是1、2、3、4、5;应该是所有的部门名,我们遍历得到

<!--其中th:value用来表示部门的id,我们实际传入的值为id-->
<option th:each="department:${departments}" th:text="${department.getDepartmentName()}" th:value="${department.getId()}"></option>

重启测试,成功显示所有部门
image-20201005004847095
到此,添加员工页面编写完成

3. add页面添加员工请求

add.html页面,当我们填写完信息,点击添加按钮,应该完成添加返回到list页面,展示新的员工信息;因此在add.html点击添加按钮的一瞬间,我们同样发起一个请求/add,与上述提交按钮发出的请求路径一样,但这里发出的是post请求

 

然后编写对应的controller,同样在EmployeeController中添加一个方法addEmp用来处理点击添加按钮的操作

@PostMapping("/add")   //这里2个请求都是/add,是rest风格,用请求类型区分
public String addEmp(Employee employee) {
    employeeDao.addEmployee(employee);//添加一个员工
    return "redirect:/emps";//重定向到/emps,刷新列表,返回到list页面
}

 我们重启主程序,进行测试,进入添加页面,填写相关信息,注意日期格式默认为yyyy/MM/dd
image-20201005105038255
然后点击添加按钮,成功实现添加员工
image-20201005105046710
我们也可以添加多个员工
image-20201005105422329

(八)修改员工信息——改

1. list页面编辑按钮增添请求

当我们点击编辑标签时,应该跳转到编辑页面edit.html(我们即将创建)进行编辑

因此首先将list.html页面的编辑标签添加href属性,实现点击请求/edit/id号到编辑页面

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


然后编写对应的controller,在EmployeeController中添加一个方法edit用来处理list页面点击编辑按钮的操作,返回到edit.html编辑员工页面,我们即将创建

//restful风格接收参数
@RequestMapping("/edit/{id}")
public String edit(@PathVariable("id") int id, Model model) {
    //查询指定id的员工,添加到empByID中,用于前端接收
    Employee employeeByID = employeeDao.getEmployeeByID(id);
    model.addAttribute("empByID", employeeByID);
    //查出所有的部门信息,添加到departments中,用于前端接收
    Collection<Department> departments = departmentDao.getDepartments();
    model.addAttribute("departments", departments);
    return "/emp/edit";//返回到编辑员工页面
}

2. 创建编辑员工页面edit

templates/emp下新建一个edit.html

image-20201005110750692

复制add.html中的代码,稍作修改

<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
    <form th:action="@{/edit}" method="post">
        <div class="form-group">
            <label>LastName</label>
            <input th:value="${empByID.getLastName()}" type="text" name="lastName" class="form-control"
                   placeholder="lastname:zsr">
        </div>
        <div class="form-group">
            <label>Email</label>
            <input th:value="${empByID.getEmail()}" type="email" name="email" class="form-control"
                   placeholder="email:xxxxx@qq.com">
        </div>
        <div class="form-group">
            <label>Gender</label><br/>
            <div class="form-check form-check-inline">
                <input th:checked="${empByID.getGender()==1}" class="form-check-input" type="radio"
                       name="gender" value="1">
                <label class="form-check-label">男</label>
            </div>
            <div class="form-check form-check-inline">
                <input th:checked="${empByID.getGender()==0}" class="form-check-input" type="radio"
                       name="gender" value="0">
                <label class="form-check-label">女</label>
            </div>
        </div>
        <div class="form-group">
            <label>department</label>
            <!--注意这里的name是department.id,因为传入的参数为id-->
            <select class="form-control" name="department.id">
                <option th:selected="${department.getId()==empByID.department.getId()}"
                        th:each="department:${departments}" th:text="${department.getDepartmentName()}"
                        th:value="${department.getId()}">
                </option>
            </select>
        </div>
        <div class="form-group">
            <label>Birth</label>
            <!--springboot默认的日期格式为yy/MM/dd-->
            <input th:value="${empByID.getDate()}" type="text" name="date" class="form-control"
                   placeholder="birth:yy/MM/dd">
        </div>
        <button type="submit" class="btn btn-primary">修改</button>
    </form>
</main>

 启动主程序测试,点击编辑1号用户
image-20201005184849563
成功跳转到edit.html,且所选用户信息正确
image-20201005190050045
但是日期的格式不太正确,我们规定一下显示的日期格式(这里日期格式不对会报错)

<!--springboot默认的日期格式为yy/MM/dd-->
<input th:value="${#dates.format(empByID.getDate(),'yyyy/MM/dd')}" type="text" name="date" class="form-control"
       placeholder="birth:yy/MM/dd">

3. edit页面编辑完成提交请求

edit.html点击修改按钮的一瞬间,我们需要返回到list页面,更新员工信息,因此我们需要添加href属性,实现点击按钮时发起一个请求/edit

然后编写对应的controller,处理点击修改按钮的请求

同样在EmployeeController中添加一个方法EditEmp用来处理edit页面点击添加的操作

@PostMapping("/add")
public String EditEmp(Employee employee) {
    employeeDao.addEmployee(employee);//添加一个员工
    return "redirect:/emps";//添加完成重定向到/emps,刷新列表
}

然后指定修改人的id(这里加隐藏域的id是因为employee对象需要id)

<input type="hidden" name="id" th:value="${empByID.getId()}">

重启测试,同样修改1号用户名称为dddd
image-20201005190833690
然后点击修改
image-20201005190901846
成功修改并返回到list.html

(九)删除员工信息——删

image-20201005224835477

当我们点击删除标签时,应该发起一个请求,删除指定的用户,然后重新返回到list页面显示员工数据

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

然后编写对应的controller,处理点击删除按钮的请求,删除指定员工,重定向到/emps请求,更新员工信息

@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") Integer id) {
    employeeDao.deleteEmployeeByID(id);
    return "redirect:/emps";
}

重启测试,点击删除按钮即可删除指定员工

(十)404页面定制

只需要在templates目录下新建一个error包,然后将404.html放入其中,404报错后SpringBoot就会自动找到这个页面

image-20201005230228695

我们可以启动程序测试,随便访问一个不存在的页面

出现的404页面即是我们自己的404.html

(十一)注销操作

在我们提取出来的公共commons页面,顶部导航栏处中的标签添加href属性,实现点击发起请求/user/logout

 然后编写对应的controller,处理点击注销标签的请求,在LoginController中编写对应的方法,清除session,并重定向到首页

@RequestMapping("/user/logout")
public String logout(HttpSession session) {
    session.invalidate();
    return "redirect:/index.html";    //注意这里redirect重定向这样写
}

重启测试,登录成功后,点击log out即可退出到首页
image-20201005232944539

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值