pom.xml文件:
war
org.springframework
spring-webmvc
5.3.1
ch.qos.logback
logback-classic
1.2.3
javax.servlet
javax.servlet-api
3.1.0
provided
org.thymeleaf
thymeleaf-spring5
3.0.12.RELEASE
org.projectlombok
lombok
1.18.10
web.xml文件:
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
/
SpringMVC.xml文件:
EmployeeController类
@Controller
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
@GetMapping("/getAllEmployee")
public String getAllEmployee(Model model){
Collection employees = employeeDao.getAll();
model.addAttribute("employees",employees);
return "employeeList";
}
@DeleteMapping("/deleteEmployee/{id}")
public String deleteEmployee(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/getAllEmployee";
}
@PostMapping("/addEmployee")
public String addEmployee(Employee employee){
employeeDao.save(employee);
return "redirect:/getAllEmployee";
}
@GetMapping("/getEmployeeById/{id}")
public String getEmployeeById(@PathVariable("id") Integer id,Model model){
Employee employee = employeeDao.get(id);
model.addAttribute("employee",employee);
return "employeeUpdate";
}
@PutMapping("/updateEmployee")
public String updateEmployee(Employee employee){
employeeDao.save(employee);
return "redirect:/getAllEmployee";
}
}
EmployeeDao类:
@Repository
public class EmployeeDao {
private static Map employeeMap = null;
static {
employeeMap = new HashMap();
employeeMap.put(1001,new Employee(1001,"老王","[email protected]",1));
employeeMap.put(1002,new Employee(1002,"老李","[email protected]",1));
employeeMap.put(1003,new Employee(1003,"老赵","[email protected]",0));
employeeMap.put(1004,new Employee(1003,"老秦","[email protected]",0));
employeeMap.put(1005,new Employee(1003,"老头","[email protected]",0));
}
private static Integer initId = 1006;
public void save(Employee employee){
if (employee.getId() == null){
employee.setId(initId++);
}
employeeMap.put(employee.getId(),employee);
}
public Collection getAll(){
return employeeMap.values();
}
public Employee get(Integer id){
return employeeMap.get(id);
}
public void delete(Integer id){
employeeMap.remove(id);
}
}
Employee类:
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Employee {
private Integer id;
private String name;
private String email;
private Integer gender;
}
employeeAdd.html
添加
employeeList.html:
Employee Info
id
name
email
gender
options(add)
delete
update