• Java --- springMVC实现RESTFul案例


    一、使用springMVC实现RESTFul小案例

    1.1、项目目录图:

     1.2、代码实现:

    pom.xml文件:

    1. <packaging>warpackaging>
    2. <dependencies>
    3. <dependency>
    4. <groupId>org.springframeworkgroupId>
    5. <artifactId>spring-webmvcartifactId>
    6. <version>5.3.1version>
    7. dependency>
    8. <dependency>
    9. <groupId>ch.qos.logbackgroupId>
    10. <artifactId>logback-classicartifactId>
    11. <version>1.2.3version>
    12. dependency>
    13. <dependency>
    14. <groupId>javax.servletgroupId>
    15. <artifactId>javax.servlet-apiartifactId>
    16. <version>3.1.0version>
    17. <scope>providedscope>
    18. dependency>
    19. <dependency>
    20. <groupId>org.thymeleafgroupId>
    21. <artifactId>thymeleaf-spring5artifactId>
    22. <version>3.0.12.RELEASEversion>
    23. dependency>
    24. <dependency>
    25. <groupId>org.projectlombokgroupId>
    26. <artifactId>lombokartifactId>
    27. <version>1.18.10version>
    28. dependency>
    29. dependencies>

    web.xml文件:

    1. "1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <filter>
    7. <filter-name>CharacterEncodingFilterfilter-name>
    8. <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    9. <init-param>
    10. <param-name>encodingparam-name>
    11. <param-value>UTF-8param-value>
    12. init-param>
    13. <init-param>
    14. <param-name>forceResponseEncodingparam-name>
    15. <param-value>trueparam-value>
    16. init-param>
    17. filter>
    18. <filter-mapping>
    19. <filter-name>CharacterEncodingFilterfilter-name>
    20. <url-pattern>/*url-pattern>
    21. filter-mapping>
    22. <filter>
    23. <filter-name>HiddenHttpMethodFilterfilter-name>
    24. <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
    25. filter>
    26. <filter-mapping>
    27. <filter-name>HiddenHttpMethodFilterfilter-name>
    28. <url-pattern>/*url-pattern>
    29. filter-mapping>
    30. <servlet>
    31. <servlet-name>DispatcherServletservlet-name>
    32. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    33. <init-param>
    34. <param-name>contextConfigLocationparam-name>
    35. <param-value>classpath:SpringMVC.xmlparam-value>
    36. init-param>
    37. <load-on-startup>1load-on-startup>
    38. servlet>
    39. <servlet-mapping>
    40. <servlet-name>DispatcherServletservlet-name>
    41. <url-pattern>/url-pattern>
    42. servlet-mapping>
    43. web-app>

    SpringMVC.xml文件:

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc"
    6. 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">
    7. <context:component-scan base-package="com.cjc.mvc">context:component-scan>
    8. <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    9. <property name="order" value="1"/>
    10. <property name="characterEncoding" value="UTF-8"/>
    11. <property name="templateEngine">
    12. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
    13. <property name="templateResolver">
    14. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    15. <property name="prefix" value="/WEB-INF/templates/"/>
    16. <property name="suffix" value=".html"/>
    17. <property name="templateMode" value="HTML5"/>
    18. <property name="characterEncoding" value="UTF-8"/>
    19. bean>
    20. property>
    21. bean>
    22. property>
    23. bean>
    24. <mvc:view-controller path="/" view-name="index">mvc:view-controller>
    25. <mvc:view-controller path="/toAdd" view-name="employeeAdd">mvc:view-controller>
    26. <mvc:default-servlet-handler/>
    27. <mvc:annotation-driven/>
    28. beans>

    EmployeeController类

    1. @Controller
    2. public class EmployeeController {
    3. @Autowired
    4. private EmployeeDao employeeDao;
    5. @GetMapping("/getAllEmployee")
    6. public String getAllEmployee(Model model){
    7. Collection employees = employeeDao.getAll();
    8. model.addAttribute("employees",employees);
    9. return "employeeList";
    10. }
    11. @DeleteMapping("/deleteEmployee/{id}")
    12. public String deleteEmployee(@PathVariable("id") Integer id){
    13. employeeDao.delete(id);
    14. return "redirect:/getAllEmployee";
    15. }
    16. @PostMapping("/addEmployee")
    17. public String addEmployee(Employee employee){
    18. employeeDao.save(employee);
    19. return "redirect:/getAllEmployee";
    20. }
    21. @GetMapping("/getEmployeeById/{id}")
    22. public String getEmployeeById(@PathVariable("id") Integer id,Model model){
    23. Employee employee = employeeDao.get(id);
    24. model.addAttribute("employee",employee);
    25. return "employeeUpdate";
    26. }
    27. @PutMapping("/updateEmployee")
    28. public String updateEmployee(Employee employee){
    29. employeeDao.save(employee);
    30. return "redirect:/getAllEmployee";
    31. }
    32. }

    EmployeeDao类:

    1. @Repository
    2. public class EmployeeDao {
    3. private static Map employeeMap = null;
    4. static {
    5. employeeMap = new HashMap();
    6. employeeMap.put(1001,new Employee(1001,"老王","123@qq.com",1));
    7. employeeMap.put(1002,new Employee(1002,"老李","456@qq.com",1));
    8. employeeMap.put(1003,new Employee(1003,"老赵","789@qq.com",0));
    9. employeeMap.put(1004,new Employee(1003,"老秦","987@qq.com",0));
    10. employeeMap.put(1005,new Employee(1003,"老头","654@qq.com",0));
    11. }
    12. private static Integer initId = 1006;
    13. public void save(Employee employee){
    14. if (employee.getId() == null){
    15. employee.setId(initId++);
    16. }
    17. employeeMap.put(employee.getId(),employee);
    18. }
    19. public Collection getAll(){
    20. return employeeMap.values();
    21. }
    22. public Employee get(Integer id){
    23. return employeeMap.get(id);
    24. }
    25. public void delete(Integer id){
    26. employeeMap.remove(id);
    27. }
    28. }

    Employee类:

    1. @AllArgsConstructor
    2. @NoArgsConstructor
    3. @Data
    4. public class Employee {
    5. private Integer id;
    6. private String name;
    7. private String email;
    8. private Integer gender;
    9. }

    employeeAdd.html

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>添加title>
    6. head>
    7. <body>
    8. <form th:action="@{/addEmployee}" method="post">
    9. name:<input type="text" name="name" ><br>
    10. email:<input type="text" name="email"><br>
    11. gender:<input type="radio" name="gender" value="1" >male
    12. <input type="radio" name="gender" value="2">female<br>
    13. <input type="submit" value="add">
    14. form>
    15. body>
    16. html>

    employeeList.html:

    1. <table id="dataTable" border="1" cellspacing="0" cellpadding="0" style="text-align: center">
    2. <tr>
    3. <th colspan="5">Employee Infoth>
    4. tr>
    5. <tr>
    6. <th>idth>
    7. <th>nameth>
    8. <th>emailth>
    9. <th>genderth>
    10. <th>options(<a th:href="@{/toAdd}">adda>)th>
    11. tr>
    12. <tr th:each="employee : ${employees}">
    13. <td th:text="${employee.id}">td>
    14. <td th:text="${employee.name}">td>
    15. <td th:text="${employee.email}">td>
    16. <td th:text="${employee.gender}">td>
    17. <td>
    18. <a @click="deleteEmployee" th:href="@{'/deleteEmployee/'+${employee.id}}">deletea>
    19. <a th:href="@{'/getEmployeeById/'+${employee.id}}">updatea>
    20. td>
    21. tr>
    22. table>
    23. <form id="deleteForm" method="post">
    24. <input type="hidden" name="_method" value="delete">
    25. form>
    26. <script type="text/javascript" th:src="@{/static/js/vue.js}">script>
    27. <script type="text/javascript">
    28. var vue = new Vue({
    29. el:"#dataTable",
    30. methods:{
    31. deleteEmployee:function (event){
    32. //根据id获取表单元素
    33. var elementById = document.getElementById("deleteForm");
    34. //将触发点击事件的超链接的href属性赋值给表单action
    35. elementById.action = event.target.href;
    36. //提交表单
    37. elementById.submit();
    38. //取消超链接默认行为
    39. event.preventDefault();
    40. }
    41. }
    42. });
    43. script>
    44. body>
    45. html>

    employeeUpdate.html

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>修改title>
    6. head>
    7. <body>
    8. <form th:action="@{/updateEmployee}" method="post">
    9. <input type="hidden" name="_method" value="put">
    10. <input type="hidden" name="id" th:value="${employee.id}">
    11. name:<input type="text" name="name" th:value="${employee.name}"><br>
    12. email:<input type="text" name="email" th:value="${employee.email}"><br>
    13. gender:<input type="radio" name="gender" value="1" th:field="${employee.gender}">male
    14. <input type="radio" name="gender" value="2" th:field="${employee.gender}">female<br>
    15. <input type="submit" value="update">
    16. form>
    17. body>
    18. html>

     index.html

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>首页h1>
    9. <a th:href="@{/getAllEmployee}">查看员工信息a>
    10. body>
    11. html>

    vue.js这个文件可以自己下载拷贝过来

    已上传到资源下载中:https://download.csdn.net/download/qq_46093575/86870794

  • 相关阅读:
    练习题——Linux文件权限与目录管理
    七月集训(6)滑动窗口+动态规划
    自建网盘平台搭建(源码+教程)
    代码评审——Java占位符%n的处理
    C++虚继承原理与类布局分析
    编译原理5 自底向上语法分析
    《Linux驱动:块设备驱动--构建基本流程》
    Godot 官方2D C#重构(1):雪花碰撞
    chrome实用插件分享
    RedisSearch深度解析:探索全文搜索的新境界
  • 原文地址:https://blog.csdn.net/qq_46093575/article/details/127631397