pom.xml文件:
- <packaging>warpackaging>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.3.1version>
- dependency>
-
- <dependency>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-classicartifactId>
- <version>1.2.3version>
- dependency>
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>3.1.0version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>org.thymeleafgroupId>
- <artifactId>thymeleaf-spring5artifactId>
- <version>3.0.12.RELEASEversion>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.18.10version>
- dependency>
- dependencies>
web.xml文件:
- "1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
- <filter>
- <filter-name>CharacterEncodingFilterfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
- <init-param>
- <param-name>encodingparam-name>
- <param-value>UTF-8param-value>
- init-param>
- <init-param>
- <param-name>forceResponseEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
- <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>
-
- <servlet>
- <servlet-name>DispatcherServletservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:SpringMVC.xmlparam-value>
- init-param>
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>DispatcherServletservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
- web-app>
SpringMVC.xml文件:
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
- <context:component-scan base-package="com.cjc.mvc">context:component-scan>
-
- <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
- <property name="order" value="1"/>
- <property name="characterEncoding" value="UTF-8"/>
- <property name="templateEngine">
- <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
- <property name="templateResolver">
- <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
-
- <property name="prefix" value="/WEB-INF/templates/"/>
-
- <property name="suffix" value=".html"/>
- <property name="templateMode" value="HTML5"/>
- <property name="characterEncoding" value="UTF-8"/>
- bean>
- property>
- bean>
- property>
- bean>
-
- <mvc:view-controller path="/" view-name="index">mvc:view-controller>
- <mvc:view-controller path="/toAdd" view-name="employeeAdd">mvc:view-controller>
-
- <mvc:default-servlet-handler/>
-
- <mvc:annotation-driven/>
- beans>
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,"老王","123@qq.com",1));
- employeeMap.put(1002,new Employee(1002,"老李","456@qq.com",1));
- employeeMap.put(1003,new Employee(1003,"老赵","789@qq.com",0));
- employeeMap.put(1004,new Employee(1003,"老秦","987@qq.com",0));
- employeeMap.put(1005,new Employee(1003,"老头","654@qq.com",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
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>添加title>
- head>
- <body>
- <form th:action="@{/addEmployee}" method="post">
- name:<input type="text" name="name" ><br>
- email:<input type="text" name="email"><br>
- gender:<input type="radio" name="gender" value="1" >male
- <input type="radio" name="gender" value="2">female<br>
- <input type="submit" value="add">
- form>
- body>
- html>
employeeList.html:
- <table id="dataTable" border="1" cellspacing="0" cellpadding="0" style="text-align: center">
- <tr>
- <th colspan="5">Employee Infoth>
- tr>
- <tr>
- <th>idth>
- <th>nameth>
- <th>emailth>
- <th>genderth>
- <th>options(<a th:href="@{/toAdd}">adda>)th>
- tr>
- <tr th:each="employee : ${employees}">
- <td th:text="${employee.id}">td>
- <td th:text="${employee.name}">td>
- <td th:text="${employee.email}">td>
- <td th:text="${employee.gender}">td>
- <td>
- <a @click="deleteEmployee" th:href="@{'/deleteEmployee/'+${employee.id}}">deletea>
- <a th:href="@{'/getEmployeeById/'+${employee.id}}">updatea>
- td>
- tr>
- table>
- <form id="deleteForm" method="post">
- <input type="hidden" name="_method" value="delete">
- form>
- <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 elementById = document.getElementById("deleteForm");
- //将触发点击事件的超链接的href属性赋值给表单action
- elementById.action = event.target.href;
- //提交表单
- elementById.submit();
- //取消超链接默认行为
- event.preventDefault();
- }
- }
- });
- script>
- body>
- html>
employeeUpdate.html
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>修改title>
- head>
- <body>
- <form th:action="@{/updateEmployee}" method="post">
- <input type="hidden" name="_method" value="put">
- <input type="hidden" name="id" th:value="${employee.id}">
- name:<input type="text" name="name" th:value="${employee.name}"><br>
- email:<input type="text" name="email" th:value="${employee.email}"><br>
- gender:<input type="radio" name="gender" value="1" th:field="${employee.gender}">male
- <input type="radio" name="gender" value="2" th:field="${employee.gender}">female<br>
- <input type="submit" value="update">
- form>
- body>
- html>
index.html
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>首页title>
- head>
- <body>
- <h1>首页h1>
- <a th:href="@{/getAllEmployee}">查看员工信息a>
- body>
- html>
vue.js这个文件可以自己下载拷贝过来
已上传到资源下载中:https://download.csdn.net/download/qq_46093575/86870794