CRUD c:create創(chuàng)建 r:retieve:查詢 u:update:修改 d:delete:刪除 rest /emp/1 get 代表查詢id為1的員工 /emp/1 put 代表修改id為1的員工 /emp/1 delete 代表刪除id為1的員工 /emp post 代表新增員工 /emps get 代表查詢所有的員工 /indexemp.jsp --->emps請求--->轉(zhuǎn)發(fā)到empList.jsp展示所有員工數(shù)據(jù) 添加: empList.jsp中點擊添加員工,--->查詢所有的部門信息要展示在頁面-->添加頁面(addEmp.jsp) -->輸入要添加的數(shù)據(jù)-->點擊保存--->控制器接收請求處理保存數(shù)據(jù)--->保存完成--->繼續(xù)回到首頁 修改: 在頁面點擊修改--(查詢要修改的員工信息,放到域?qū)ο笾校薷捻撁骘@示數(shù)據(jù)/emp/1 get) ->員工的修改頁面(editemp)--->輸入要修改的數(shù)據(jù),點擊修改按鈕,發(fā)送請求/emp/1 put -->修改員工--->員工列表頁面 刪除: 在頁面點擊刪除-->發(fā)送刪除請求(/emp/1 delete)-->刪除-->來到主頁面
1.EmployeeController
package com.tanzhou.controller;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.tanzhou.dao.DepartmentDao;
import com.tanzhou.dao.EmployeeDao;
import com.tanzhou.domain.Department;
import com.tanzhou.domain.Employee;
@Controller
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
@Autowired
private DepartmentDao departmentDao;
/**
* 查詢所有的員工
*/
@RequestMapping("/emps")
public String getEmps(Model model){
Collection<Employee> emps = employeeDao.getEmployees();
model.addAttribute("emps", emps);//存到request域?qū)ο笾? return "empList";
}
// public Employee(Integer id, String name, String email, Integer sex, Department department) {
@RequestMapping("/addpage")
public String addpage(Model model){
Collection<Department> depts = departmentDao.getDepartments();
model.addAttribute("depts", depts);
model.addAttribute("employee", new Employee());
return "addEmp";
}
@RequestMapping(value="/emp",method=RequestMethod.POST)//
public String addEmp(Employee employee){
System.out.println(employee);
employeeDao.save(employee);//模擬保存
return "redirect:/emps";//添加之后直接重定向到查詢員工的請求
}
//<a href="${pageContext.request.contextPath}/emp/${emp.id}"></a>
@RequestMapping(value="/emp/{id}",method=RequestMethod.GET)
public String getEmpById(@PathVariable("id")Integer id,Model model){
//根據(jù)id查詢員工
Employee employee = employeeDao.getEmployee(id);
model.addAttribute("employee", employee);
//查詢所有部門
Collection<Department> dept = departmentDao.getDepartments();
model.addAttribute("depts", dept);
return "editemp";
}
//修改員工
@RequestMapping(value="/emp/{id}",method=RequestMethod.PUT)
public String updateEmp(@ModelAttribute("employee")Employee emp){
System.out.println(emp);
employeeDao.save(emp);
return "redirect:/emps";
}
@ModelAttribute
public void modelAttribute(@RequestParam(value="id",required=false)Integer id,Model model){
//先根據(jù)id查詢員工
if(id!=null){
Employee employee = employeeDao.getEmployee(id);
model.addAttribute("employee", employee);
}
}
//刪除
@RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
public String deleteEmp(@PathVariable("id")Integer id){
//刪除
employeeDao.deleteEmployee(id);
return "redirect:/emps";
}
}
2.EmployeeDao和DeptmentDao
package com.tanzhou.dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.tanzhou.domain.Department;
import com.tanzhou.domain.Employee;
@Repository
public class EmployeeDao {
@Autowired
private DepartmentDao ddao;
private static Map<Integer,Employee> map = null;
// public Employee(Integer id, String name, String email,
// Integer sex, Department department) {
static{
map = new HashMap<>();
map.put(1, new Employee(1,"王A","WA@QQ.com",1,new Department(1,"研發(fā)部")));
map.put(2, new Employee(2,"王B","WB@QQ.com",2,new Department(2,"市場部")));
map.put(3, new Employee(3,"王C","WC@QQ.com",1,new Department(3,"策劃部")));
map.put(4, new Employee(4,"王D","WD@QQ.com",2,new Department(2,"市場部")));
map.put(5, new Employee(5,"王E","WE@QQ.com",1,new Department(1,"研發(fā)部")));
}
private static Integer initid = 5;
/**
* 新增或修改
* 如果傳入對象有id,代表修改
* 如果傳入對象沒有id,代表新增
* @param emp
*/
public void save(Employee emp){
if(emp.getId()==null){
emp.setId(initid++);
}
emp.setDepartment(ddao.getDepartment(emp.getDepartment().getDid()));
map.put(emp.getId(), emp);
}
/**
* 獲取所有的員工
* @return
*/
public Collection<Employee> getEmployees(){
return map.values();
}
/**
* 根據(jù)id查詢員工對象
* @param id
* @return
*/
public Employee getEmployee(Integer id){
return map.get(id);
}
/**
* 根據(jù)id刪除員工
* @param id
*/
public void deleteEmployee(Integer id){
map.remove(id);
}
}
package com.tanzhou.dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.tanzhou.domain.Department;
@Repository
public class DepartmentDao {
private static Map<Integer,Department> map = null;
static{
map = new HashMap<>();
map.put(1, new Department(1,"研發(fā)部"));
map.put(2, new Department(2,"市場部"));
map.put(3, new Department(3,"策劃部"));
}
//獲取所有部門信息
public Collection<Department> getDepartments(){
return map.values();
}
//根據(jù)部門編號查詢對應(yīng)的部門
public Department getDepartment(Integer did){
return map.get(did);
}
}


3.jsp頁面
addEmp.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>添加員工信息</h1>
<!-- 表單標(biāo)簽:可以將模型數(shù)據(jù)中的屬性和html頁面上的表單元素直接進(jìn)行綁定
1.導(dǎo)入表單標(biāo)簽庫
modelAttribute
-->
<form:form action="${pageContext.request.contextPath}/emp" modelAttribute="employee">
<!-- 1.path就是原來htmlinput標(biāo)簽的name項 2.自動回顯模型數(shù)據(jù)中某個對象的屬性值-->
員工姓名:<form:input path="name"/><br/>
員工郵箱:<form:input path="email"/><br/>
男:<input type="radio" name="sex" value="1"><br/>
女:<input type="radio" name="sex" value="2"><br/>
員工所在部門:
<!-- items:代表要遍歷的集合,自動遍歷,遍歷出來的每一個對象都是部門對象
itemLabel:指定遍歷出來的這個對象的哪個屬性要作為options標(biāo)簽體的值.
itemValue:指定遍歷出來的這個對象的哪個屬性要作為options標(biāo)簽體的value.
-->
<form:select path="department.did" items="${depts}" itemLabel="dname" itemValue="did">
</form:select>
<input type="submit" value="添加員工">
</form:form>
<%-- <form action="">
員工姓名:<input type="text" name="name"><br/>
員工郵箱:<input type="text" name="email"><br/>
員工性別:<br/>
男:<input type="radio" name="sex" value="1"><br/>
女:<input type="radio" name="sex" value="2"><br/>
員工所在部門:
<select name="department.did">
<c:forEach items="${depts}" var="dept">
<option value="${dept.did}">${dept.dname}</option>
</c:forEach>
</select>
<input type="submit" value="提交">
</form> --%>
</body>
</html>
editEmp.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改員工</title>
</head>
<body>
<h1>修改員工的信息</h1>
<form:form method="post" modelAttribute="employee" action="${pageContext.request.contextPath}/emp/${employee.id}">
<input type="hidden" name="_method" value="put">
<input type="hidden" name="id" value="${employee.id}">
員工郵箱:<form:input path="email"/><br/>
男:<form:radiobutton path="sex" value="1"/>
女::<form:radiobutton path="sex" value="2"/><br/>
員工所在部門:
<form:select path="department.did" items="${depts}" itemLabel="dname" itemValue="did">
</form:select>
<input type="submit" value="修改員工">
</form:form>
</body>
</html>
empList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/jq/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(function(){
alert("aaa");
})
</script>
<!--
springmvc處理靜態(tài)資源的問題:
web.xml中配置了url-pattern:/ 用來處理所有請求,但是jsp除外
發(fā)送http://localhost:8080/springmvcday04/jq/jquery-3.4.1.min.js
前端控制器會進(jìn)行接收并處理,而對應(yīng)控制器中沒有定義對應(yīng)的方法處理,所以出現(xiàn)404
解決:
讓js請求不交給前端控制器處理
辦法:
第一種:修改web.xml url-pattern:/*.action,如果項目要使用rest風(fēng)格,支持不好
第二種:
在springmvc的配置文件中加上標(biāo)簽 <mvc:default-servlet-handler/>
但是加上標(biāo)簽出現(xiàn)動態(tài)請求找不到,所以還需要加上<mvc:annotation-driven></mvc:annotation-driven>
一起配合使用
-->
</head>
<body>
<h1>歡迎來到員工列表頁面</h1>
<table border="1">
<tr>
<th>員工id</th>
<th>員工姓名</th>
<th>員工郵箱</th>
<th>員工性別</th>
<th>所在部門</th>
<th>修改</th>
<th>刪除</th>
</tr>
<c:forEach items="${emps}" var="emp">
<tr>
<td>${emp.id}</td>
<td>${emp.name}</td>
<td>${emp.email}</td>
<td>${emp.sex==1?"男":"女"}</td>
<td>${emp.department.dname}</td>
<td>
<a href="${pageContext.request.contextPath}/emp/${emp.id}">修改</a>
</td>
<td>
<form action="${pageContext.request.contextPath}/emp/${emp.id}" method="post">
<input type="hidden" name="_method" value="delete">
<input type="submit" value="刪除員工">
</form>
</td>
</tr>
</c:forEach>
</table>
<a href="addpage">添加員工</a>
</body>
</html>



浙公網(wǎng)安備 33010602011771號