主要用于展示最后的代码。项目结构如下:
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceResponseEncoding
true
CharacterEncodingFilter
/*
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMVC.xml
1
DispatcherServlet
/
(1) bean - Employee
package com.atguigu.rest.bean;
public class Employee {
private Integer id;
private String lastName;
private String email;
//1 male, 0 female
private Integer gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Employee(Integer id, String lastName, String email, Integer gender) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
}
public Employee() {
}
}
(2) control - EmployeeControl
package com.atguigu.rest.controller;
import com.atguigu.rest.bean.Employee;
import com.atguigu.rest.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Collection;
@Controller
public class EmployeeController {
@Autowired
//自动装配:默认根据ByType,其次ByName。在IOC容器的范围内找到一个Bean来赋值
//通过注解 + 扫描,IOC容器中就包含了EmployeeDao这个Bean,从而进行自动装配
private EmployeeDao employeeDao;
@RequestMapping(value="/employee", method = RequestMethod.GET)
public String getAllEmployee(Model model){
Collection employeeList = employeeDao.getAll();
//利用model向request域对象共享数据
model.addAttribute("employeeList", employeeList);
return "employee_list";
}
//这里由于有传进来的参数,因此在/employee后面需要有占位符{}来存储
//然后利用@PathVariable注解,将占位符所表示的值与控制器函数内的形参进行绑定
@RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
public String deleteEmployee(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/employee";
//在删除结束后,回到展示表单的页面。先进入上一个控制器方法获取全部的志愿信息,让它再去展示表单
//这里不采用转发,而是重定向。因为删除成功后,和之前的请求就没有关系了。
//如果用转发,则地址栏中的地址还是现在的地址:/employee/{id}
//重定向之后,地址栏中就会显示:/employee。相当于咱们自己在地址栏中输入了个地址去访问,默认为get请求
}
@RequestMapping(value = "/employee", method = RequestMethod.POST)
public String addEmployee(Employee employee){
//存储得到的Employee对象
employeeDao.save(employee);
return "redirect:/employee";
}
@RequestMapping(value = "/employee/{id}", method = RequestMethod.GET)
//由于需要将查询出的数据在请求域中共享,因此参数中有个model
public String getEmployeeById(@PathVariable("id") Integer id, Model model){
Employee employee = employeeDao.get(id);
model.addAttribute("employee",employee);
return "employee_update";
}
@RequestMapping(value = "/employee", method = RequestMethod.PUT)
public String updateEmployee(Employee employee){
employeeDao.save(employee);
return "redirect:/employee";
}
}
(3) dao - EmployeeDao
package com.atguigu.rest.dao;
import com.atguigu.rest.bean.Employee;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
// Dao: 数据访问接口
// @Repository持久层注解
@Repository
public class EmployeeDao {
private static Map employees = null;
static {
employees = new HashMap();
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;
public void save(Employee employee) {
if (employee.getId() == null) {
employee.setId(initId++);
}
employees.put(employee.getId(), employee);
}
public Collection getAll() {
return employees.values();
}
public Employee get(Integer id) {
return employees.get(id);
}
public void delete(Integer id) {
employees.remove(id);
}
}
(1) employee_add.html
add employee
(2) employee_list.html
Employee Info
Employee Info
id
lastname
email
gender
options(add)
delete
update
(3) employee_update.html
update employee
(4) index.html
首页
首页
查看员工信息