文章目录
- 1.jstl表达式,要在Tomcat安装目录的lib文件下添加下面两个jar包
- 2.其次需要在maven中导入下面的jar包
- 3.写实体类,并使用集合封装数据
- 4.实现员工列表展示页面
- 5.实现添加员工页面
- 6.添加员工
- 7.编写员工修改页面
- 8.修改员工信息
- 9.删除员工
1.jstl表达式,要在Tomcat安装目录的lib文件下添加下面两个jar包
2.其次需要在maven中导入下面的jar包
将其添加到lib文件夹下面
3.写实体类,并使用集合封装数据
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;
//引用类型
private Department department;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
private Integer id;
private String departmentName;
}
@Repository
public class EmployeeDao {
private static Map<Integer, Employee> employees = null;
@Autowired
private DepartmentDao departmentDao;
static{
employees = new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));
employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));
employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));
employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));
employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));
}
private static Integer initId = 1006;
public void save(Employee employee){
if(employee.getId() == null){
employee.setId(initId++);
}
employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
employees.put(employee.getId(), employee);
}
public Collection<Employee> getAll(){
return employees.values();
}
public Employee get(Integer id){
return employees.get(id);
}
public void delete(Integer id){
employees.remove(id);
}
}
@Repository
public class DepartmentDao {
private static Map<Integer, Department> departments = null;
static{
departments = new HashMap<Integer, Department>();
departments.put(101, new Department(101, "D-AA"));
departments.put(102, new Department(102, "D-BB"));
departments.put(103, new Department(103, "D-CC"));
departments.put(104, new Department(104, "D-DD"));
departments.put(105, new Department(105, "D-EE"));
}
public Collection<Department> getDepartments(){
return departments.values();
}
public Department getDepartment(Integer id){
return departments.get(id);
}
}
4.实现员工列表展示页面
需求:显示所有的员工信息
第一步:访问index.jsp
–>发送请求路径/emps
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--直接跳转到路径:/emps--%>
<jsp:forward page="/emps"></jsp:forward>
第二步:利用控制器查询所有员工,并将查询结果放到请求域中,转到list页面展示
@Controller
public class EmployeeController {
@Autowired
EmployeeDao employeeDao;
@RequestMapping("/emps")
public String test(Model model){
Collection<Employee> all = employeeDao.getAll();
model.addAttribute("emps",all);
return "list";
}
}
第三步:写员工列表展示页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>员工列表</h1>
<table border="1" cellpadding="5px" cellspacing="0">
<tr>
<th>ID</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>departmentName</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>
<c:forEach items="${emps}" var="emp">
<tr>
<td>${emp.id}</td>
<td>${emp.lastName}</td>
<td>${emp.email}</td>
<td>${emp.gender==0?"女":"男"}</td>
<td>${emp.department.departmentName}</td>
<td>EDIT</td>
<td>DELETE</td>
</tr>
</c:forEach>
</table>
<a href="toaddpage">添加员工</a>
</body>
</html>
5.实现添加员工页面
需求:展示添加员工页面
第一步:在list页面中写一个员工添加超链接,发送请求/toaddpage
<a href="toaddpage">添加员工</a>
第二步:利用控制器查询出所有的部门信息放在域中,转发到add.jsp添加页面
@Controller
public class EmployeeController {
@Autowired
DepartmentDao departmentDao;
@RequestMapping("/toaddpage")
public String toAddPage(Model model) {
//查出所有部门
Collection<Department> departments = departmentDao.getDepartments();
//将数据放在请求域中
model.addAttribute("depts",departments);
//去添加页面
return "add";
}
}
第三步:编写添加页面
<h1>员工添加</h1>
<form action="">
lastName:<input name="lastName"/><br>
email:<input name="email"/><br>
gender:<br/>
男:<input type="radio" name="gender" value="1"/><br>
女:<input type="radio" name="gender" value="0"/><br>
dept:<select name="department.id">
<c:forEach items="${depts}" var="deptItem">
<!--标签体中的是在页面的提示信息,value才是真正提交的值-->
<option value="${deptItem.id}">${deptItem.departmentName}</option>
</c:forEach>
</select><br>
<input type="submit" value="提交"/>
</form>
但是上面的方法太麻烦:
可以使用SpringMvc提供的表单标签实现将模型数据中的属性和HTML表单元素绑定,以实现表单数据便捷编辑和表单值的回显。
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--使用SpringMvc提供的表单标签库--%>
<form:form action="">
<%--
path:原来input标签的name项
1.当做原生的name项
2.自动回显隐含模型中某个对象对应属性的值
--%>
lastname:<form:input path="lastName"/><br>
email:<form:input path="email"/><br>
gender:<br>
男:<form:radiobutton path="gender" value="1"/><br>
女:<form:radiobutton path="gender" value="0"/><br>
<%--
items:指定呀遍历的集合
itemLabel:指定遍历出的这个对象的哪个属性是作为option标签体的值
itemValue:指定刚才遍历出来的这个对象的哪个属性是作为提交的value值
--%>
dept:
<form:select path="department.id"
items="${depts}"
itemLabel="departmentName"
itemValue="id">
</form:select>
<br>
<input type="submit" value="保存"/>
</form:form>
SpringMvc认为表单中的每一项数据都要回显的:
path指定的每一个属性,这个属性是从隐含模型(请求域中取出的某个对象的属性),那么请求域中必须有一个对象拥有这些属性。这个对象就是command
@Controller
public class EmployeeController {
@Autowired
DepartmentDao departmentDao;
@RequestMapping("/toaddpage")
public String toAddPage(Model model) {
//查出所有部门
Collection<Department> departments = departmentDao.getDepartments();
//将数据放在请求域中
model.addAttribute("depts",departments);
model.addAttribute("command",
new Employee(null,"张三","haha@163.com",
0,departmentDao.getDepartment(101)));
//去添加页面
return "add";
}
}
因为请求域中必须有一个command,也挺烦的,可以在表单中使用一个 modelAttribute
属性。
使用command:从请求与中获取一个command对象,把这个对象中的每一个属性对应 的值回显出来。
modelAttribute:将其设置为一个指定的值,取对象的key就用这个指定的值
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form action="" modelAttribute="employee">
lastname:<form:input path="lastName"/><br>
email:<form:input path="email"/><br>
gender:<br>
男:<form:radiobutton path="gender" value="1"/><br>
女:<form:radiobutton path="gender" value="0"/><br>
dept:
<form:select path="department.id"
items="${depts}"
itemLabel="departmentName"
itemValue="id">
</form:select>
<br>
<input type="submit" value="保存"/>
</form:form>
@Controller
public class EmployeeController {
@Autowired
DepartmentDao departmentDao;
@RequestMapping("/toaddpage")
public String toAddPage(Model model) {
//查出所有部门
Collection<Department> departments = departmentDao.getDepartments();
//将数据放在请求域中
model.addAttribute("depts",departments);
model.addAttribute("employee", new Employee());
//去添加页面
return "add";
}
}
6.添加员工
第一步:编写请求路径,提交方式为post,代表添加员工
<%--获取项目根目录--%>
<%
pageContext.setAttribute("ctp",request.getContextPath());
%>
<form:form action="${ctp}/emp" modelAttribute="employee" method="post">
</form:form>
第二步:编写添加员工的控制器类方法,添加员工保存数据并查询员工跳转到员工列表展示页面。
@Controller
public class EmployeeController {
@Autowired
EmployeeDao employeeDao;
@RequestMapping("/emps")
public String getEmps(Model model) {
Collection<Employee> all = employeeDao.getAll();
model.addAttribute("emps", all);
return "list";
}
@RequestMapping(value="/emp",method = RequestMethod.POST)
public String addEmp(Employee employee){
System.out.println("要添加的员工:"+employee);
employeeDao.save(employee);
//返回列表页面:重定向到查询所有请求
return "redirect:/emps";
}
}
7.编写员工修改页面
第一步:添加员工后来到员工列表展示页面,现在需要点击EDIT后能够来到查询员工信息的控制器类方法中
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
pageContext.setAttribute("ctp",request.getContextPath());
%>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>员工列表</h1>
<table border="1" cellpadding="5px" cellspacing="0">
<tr>
<th>ID</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>departmentName</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>
<c:forEach items="${emps}" var="emp">
<tr>
<td>${emp.id}</td>
<td>${emp.lastName}</td>
<td>${emp.email}</td>
<td>${emp.gender==0?"女":"男"}</td>
<td>${emp.department.departmentName}</td>
<td>
<a href="${ctp}/emp/${emp.id}">edit</a>
</td>
<td>DELETE</td>
</tr>
</c:forEach>
</table>
<%--使用项目根目录的绝对路径--%>
<a href="${ctp}/toaddpage">添加员工</a>
</body>
</html>
第二步:编写控制器类查询所有员工信息,放到请求域中,并跳转到修改员工信息页面进行回显。
@Controller
public class EmployeeController {
@Autowired
EmployeeDao employeeDao;
@Autowired
DepartmentDao departmentDao;
//查询员工
@RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
public String getEmp(@PathVariable("id") Integer id,Model model){
//根据id查询员工信息
Employee employee = employeeDao.get(id);
//放在请求域中
model.addAttribute("employee",employee);
//查询部门信息放在隐含模型中
Collection<Department> departments = departmentDao.getDepartments();
model.addAttribute("depts",departments);
return "edit";
}
}
第三步:编写修改员工信息展示页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--员工修改页面--%>
<%--modelAttribute:这个表单的所有内容显示绑定的是请求域中employee的值--%>
<form:form action="" modelAttribute="employee">
email:<form:input path="email"/>
gender:<br>
男:<form:radiobutton path="gender" value="1"/><br>
女:<form:radiobutton path="gender" value="0"/><br>
dept:
<form:select path="department.id"
items="${depts}"
itemValue="id"
itemLabel="departmentName"/><br>
<input type="submit" value="修改"/>
</form:form>
</body>
</html>
8.修改员工信息
第一步:输入完成信息后点击修改,发送put请求来到修改员工的控制器类方法中
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%
pageContext.setAttribute("cpt",request.getContextPath());
%>
<%--员工修改页面--%>
<%--modelAttribute:这个表单的所有内容显示绑定的是请求域中employee的值--%>
<form:form action="${cpt}/emp/${employee.id}"
modelAttribute="employee" method="post">
</form:form>
第二步:编写控制器类修改员工信息方法:
@Controller
public class EmployeeController {
@Autowired
EmployeeDao employeeDao;
@Autowired
DepartmentDao departmentDao;
@RequestMapping("/emps")
public String getEmps(Model model) {
Collection<Employee> all = employeeDao.getAll();
model.addAttribute("emps", all);
return "list";
}
//修改页面
@RequestMapping(value = "/emp/{id}",method = RequestMethod.PUT)
public String updateEmp(@ModelAttribute("employee") Employee employee){
System.out.println("要修改的员工:"+employee);
employeeDao.save(employee);
return "redirect:/emps";
}
@ModelAttribute
public void myModelAttribute(
@RequestParam(value = "id",required = false) Integer id, Model model){
if(id!=null){
Employee employee = employeeDao.get(id);
model.addAttribute("employee",employee);
}
}
}
9.删除员工
第一步编辑DELETE,让其成为一个按钮
<h1>员工列表</h1>
<table border="1" cellpadding="5px" cellspacing="0">
<tr>
<th>ID</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>departmentName</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>
<c:forEach items="${emps}" var="emp">
<tr>
<td>${emp.id}</td>
<td>${emp.lastName}</td>
<td>${emp.email}</td>
<td>${emp.gender==0?"女":"男"}</td>
<td>${emp.department.departmentName}</td>
<td>
<a href="${ctp}/emp/${emp.id}">edit</a>
</td>
<td>
<a href="${ctp}/emp/${emp.id}">delete</a>
<form action="${ctp}/emp/${emp.id}" method="post">
<input type="hidden" name="_method" value="delete"/>
<input type="submit" value="删除"/>
</form>
</td>
</tr>
</c:forEach>
</table>
<%--使用项目根目录的绝对路径--%>
<a href="${ctp}/toaddpage">添加员工</a>
</body>
第二步:编写删除员工的控制器方法
@Controller
public class EmployeeController {
@Autowired
EmployeeDao employeeDao;
@Autowired
DepartmentDao departmentDao;
@RequestMapping("/emps")
public String getEmps(Model model) {
Collection<Employee> all = employeeDao.getAll();
model.addAttribute("emps", all);
return "list";
}
//删除页面
@RequestMapping(value = "/emp/{id}",method = RequestMethod.DELETE)
public String deleteEmp(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/emps";
}
}