Spring boot添加员工页面跳转

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nangeali/article/details/82526805

页面跳转

单击添加按钮,跳转到添加页面
可以选择员工的公寓,需要把公寓信息传递过去

添加按钮

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

Controller

//来到员工添加页面
@GetMapping("/emp")
public String toAddPage(Model model) {
    //来到添加页面,查出所有的部门,在页面显示
    Collection<Department> departments = departmentDao.getDepartments();
    model.addAttribute("depts", departments);
    return "emp/add";
}

添加页面

可以选择用户的公寓

<!--提交的是部门的id-->
<select class="form-control" name="department.id">
    <option th:selected="${emp!=null}?${dept.id == emp.department.id}" th:value="${dept.id}" th:each="dept:${depts}" th:text="${dept.departmentName}">1</option>
</select>

页面

<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
    <!--需要区分是员工修改还是添加;-->
    <form th:action="@{/emp}" method="post">
        <!--发送put请求修改员工数据-->
        <!--
        1、SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自动配置好的)
        2、页面创建一个post表单
        3、创建一个input项,name="_method";值就是我们指定的请求方式
        -->
        <input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
        <input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">
        <div class="form-group">
            <label>LastName</label>
            <input name="lastName" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${emp.lastName}">
        </div>
        <div class="form-group">
            <label>Email</label>
            <input name="email" type="email" class="form-control" placeholder="[email protected]" th:value="${emp!=null}?${emp.email}">
        </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" th:checked="${emp!=null}?${emp.gender==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" th:checked="${emp!=null}?${emp.gender==0}">
                <label class="form-check-label"></label>
            </div>
        </div>
        <div class="form-group">
            <label>department</label>
            <!--提交的是部门的id-->
            <select class="form-control" name="department.id">
                <option th:selected="${emp!=null}?${dept.id == emp.department.id}" th:value="${dept.id}" th:each="dept:${depts}" th:text="${dept.departmentName}">1</option>
            </select>
        </div>
        <div class="form-group">
            <label>Birth</label>
            <input name="birth" type="text" class="form-control" placeholder="zhangsan" th:value="${emp!=null}?${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}">
        </div>
        <button type="submit" class="btn btn-primary" th:text="${emp!=null}?'修改':'添加'">添加</button>
    </form>
</main>

猜你喜欢

转载自blog.csdn.net/nangeali/article/details/82526805