<form id="deleteForm" method="post">
<input type="hidden" name="_method" value="delete">
form>
<filter>
<filter-name>HiddenHttpMethodFilterfilter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
public class Employee {
private Integer id;
private String lastName;
private String email;
//1 male, 0 female
private Integer gender;
//添加无参构造器、有参构造器、get和set方法
}
@Repository
public class EmployeeDao {
private static Map<Integer, Employee> employees = null;
static {
employees = new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "E-AA", "[email protected]", 1));
employees.put(1002, new Employee(1002, "E-BB", "[email protected]", 1));
employees.put(1003, new Employee(1003, "E-CC", "[email protected]", 0));
employees.put(1004, new Employee(1004, "E-DD", "[email protected]", 0));
employees.put(1005, new Employee(1005, "E-EE", "[email protected]", 1));
}
private static Integer initId = 1006;
/**
* 保存员工
* @param employee
*/
public void save(Employee employee) {
if (employee.getId() == null) {
employee.setId(initId++);
}
employees.put(employee.getId(), employee);
}
/**
* 获取所有员工
* @return
*/
public Collection<Employee> getAll() {
return employees.values();
}
/**
* 根据id获取员工
* @param id
* @return
*/
public Employee get(Integer id) {
return employees.get(id);
}
/**
* 根据id删除员工
* @param id
*/
public void delete(Integer id) {
employees.remove(id);
}
}
功能 | URL 地址 | 请求方式 |
---|---|---|
访问首页√ | / | GET |
查询全部数据√ | /employee | GET |
删除√ | /employee/2 | DELETE |
跳转到添加数据页面√ | /toAdd | GET |
执行保存√ | /employee | POST |
跳转到更新数据页面√ | /employee/2 | GET |
执行更新√ | /employee | PUT |
<mvc:view-controller path="/" view-name="index">mvc:view-controller>
<mvc:annotation-driven/>
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页title>
head>
<body>
<h1>首页h1>
<a th:href="@{/employee}">访问员工信息a>
body>
html>
@Controller
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
}
@RequestMapping(value = "/employee", method = RequestMethod.GET)
public String getEmployeeList(Model model) {
//调用EmployeeDao中的方法,返回员工列表
Collection<Employee> employeeList = employeeDao.getAll();
model.addAttribute("employeeList", employeeList);
return "employee_list";
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Emloyee Info</title>
</head>
<body>
<table border="1" cellpadding="0"cellspacing="0"style="text-align: center;"id="dataTable">
<tr>
<th colspan="5">Employee Info</th>
</tr>
<tr>
<th>id</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>options</th>
</tr>
<tr th:each="employee:${employeeList}">
<th th:text="${employee.id}"></th>
<th th:text="${employee.lastName}"></th>
<th th:text="${employee.email}"></th>
<th th:text="${employee.gender}"></th>
<th>
<a href="">delete</a>
<a href="">update</a>
</th>
</tr>
</table>
</body>
</script>
</html>
<form id="deleteForm" method="post">
<input type="hidden" name="_method" value="delete">
form>
<a @click="deleteEmployee" th:href="@{'/employee/'+${employee.id}}">deletea>
<script type="text/javascript" th:src="@{/static/js/vue.js}">script>
<script type="text/javascript" >
var vue=new Vue({
el:"#dataTable",
methods:{
deleteEmployee:function (event) {
//根据id获取表单元素
var deleteForm = document.getElementById("deleteForm");
//将触发点击事件的超链接的href属性赋值给表单的action
deleteForm.action=event.target.href;
//提交表单
deleteForm.submit();
//取消超链接的默认行为
event.preventDefault();
}
}
});
script>
@RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
public String deleteEmployee(@PathVariable("id") Integer id) {
employeeDao.delete(id);
//employee_list这个界面是通过控制器去访问,操作完要跳转到这个界面通过重定向进行页面的跳转
return "redirect:/employee";
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
<title>addtitle>
head>
<body>
<form th:action="@{/employee}" method="post">
lastName: <input type="text" name="lastName"><br>
email: <input type="text" name="email"><br>
gender: <input type="radio" name="gender" value="0">female
<input type="radio" name="gender" value="1">male <br>
<input type="submit" value="add">
form>
body>
html>
<mvc:view-controller path="/toAdd" view-name="employee_add">mvc:view-controller>
@RequestMapping(value = "/employee", method = RequestMethod.POST)
public String addEmployee(Employee employee) {
employeeDao.save(employee);
return "redirect:/employee";
}
<th>options(<a th:href="@{/toAdd}">adda>)th>
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>updatetitle>
head>
<body>
<form th:action="@{/employee}" method="post">
<input type="hidden" name="_method" value="put">
<input type="hidden" name="id" th:value="${employee.id}">
lastName: <input type="text" name="lastName" th:value="${employee.lastName}"><br>
email: <input type="text" name="email" th:value="${employee.email}"><br>
gender: <input type="radio" name="gender" value="0" th:field="${employee.gender}">female
<input type="radio" name="gender" value="1" th:field="${employee.gender}">male <br>
<input type="submit" value="update">
form>
body>
html>
@RequestMapping(value = "/employee/{id}", method = RequestMethod.GET)
public String getEmployeeById(@PathVariable("id") Integer id, Model model) {
Employee employee = employeeDao.get(id);
model.addAttribute("employee", employee);
return "employee_update";
}
<a th:href="@{'/employee/'+${employee.id}}">updatea>
@RequestMapping(value ="/employee",method = RequestMethod.PUT)
public String updateEmployee(Employee employee) {
employeeDao.save(employee);
return "redirect:/employee";
}
删除功能添加vue.js之后
服务器中没有static文件夹,需要重新打包:点击Maven—>点击项目名称—>点击Lifecycle—>双击package
后面还是报错,需要在springMVC.xml中添加
<mvc:default-servlet-handler/>