SpringMVC之RESTful案例附源码

目录

1.准备工作

2.功能清单

​编辑 

 3.访问首页

3.1:配置view-controller

3.2创建页面

4.查询所有员工数据

4.1控制器方法

4.2创建employee_list.html

5.删除

5.1创建处理delete请求方式的表单

5.2删除超链接绑定点击事件

5.3控制器方法

6.跳转到添加数据页面

6.1配置view_controller

6.2创建employee_add.html

7.执行保存

7.1控制器方法

8.跳转到更新数据页面

8.1修改超链接

8.2控制器方法

8.3创建employee_update.html

9.执行更新

9.1控制器方法

##########################################

附录,源码 

项目结构

bean.Employee.java

controller.TestController.java

controller.EmployeeController.java

Dao.EmployeeDao.java

resources.springMVC.xml

webapp.static.js.vue.js

templates.index.html

templates.employee_add.html

templates.employee_list.html

templates.employee_update.html

templates.resful_dome.html

pom.xml


1.准备工作

实体类

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() {
    }
}

 dao模拟数据

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import MaHangLian.bean.Employee;
import org.springframework.stereotype.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);
    }
}

2.功能清单

SpringMVC之RESTful案例附源码_第1张图片 

 3.访问首页

3.1:配置view-controller

3.2创建页面





Title


首页

访问员工信息

4.查询所有员工数据

4.1控制器方法

@RequestMapping(value = "/employee", method = RequestMethod.GET)
public String getEmployeeList(Model model){
Collection employeeList = employeeDao.getAll();
model.addAttribute("employeeList", employeeList);
return "employee_list";
}

4.2创建employee_list.html





Employee Info



Employee Info
id lastName email gender options(add)
delete update

5.删除

5.1创建处理delete请求方式的表单


5.2删除超链接绑定点击事件

引入vue.js

删除超链接

delete

通过vue处理点击事件

5.3控制器方法

@RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
public String deleteEmployee(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/employee";
}

6.跳转到添加数据页面

6.1配置view_controller

6.2创建employee_add.html





Add Employee


lastName:
email:
gender:male female

7.执行保存

7.1控制器方法

@RequestMapping(value = "/employee", method = RequestMethod.POST)
public String addEmployee(Employee employee){
employeeDao.save(employee);
return "redirect:/employee";
}

8.跳转到更新数据页面

8.1修改超链接

update

8.2控制器方法

@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";
}

8.3创建employee_update.html





Update Employee


lastName:
email:
gender:male female

9.执行更新

9.1控制器方法

@RequestMapping(value = "/employee", method = RequestMethod.PUT)
public String updateEmployee(Employee employee){
employeeDao.save(employee);
return "redirect:/employee";
}

##########################################

附录,源码 

项目结构

SpringMVC之RESTful案例附源码_第2张图片

 

bean.Employee.java

package MaHangLian.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() {
    }
}

controller.TestController.java

package MaHangLian.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {


    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("/test_view")
    public String testView() {
        return "test_view";
    }

    @RequestMapping("/test_restful")
    public String UserController(){
        return "test_restful";
    }

    @RequestMapping("/restful_dome")
    public String restfuldome(){
        return "restful_dome";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "employee_add";
    }







}

controller.EmployeeController.java

package MaHangLian.controller;

import MaHangLian.Dao.EmployeeDao;
import MaHangLian.bean.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
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
    private EmployeeDao employeeDao;


    @RequestMapping(value = "/employee", method = RequestMethod.GET)
//    @GetMapping("/employee")
    public String getallemployee(Model model) {
        Collection employeeList = employeeDao.getAll();
        model.addAttribute("employeeList", employeeList);
        return "employee_list";
    }

    @RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
    public String deleteEmployee(@PathVariable("id") Integer id) {
        employeeDao.delete(id);
        return "redirect:/employee";
    }

    @RequestMapping(value = "/emplotee", method = RequestMethod.POST)
    public String addemployee(Employee employee) {
        employeeDao.save(employee);
        return "redirect:/employee";
    }


    @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";
    }
    @RequestMapping(value = "/employee", method = RequestMethod.PUT)
    public String updateEmployee(Employee employee){
        employeeDao.save(employee);
        return "redirect:/employee";
    }


}

Dao.EmployeeDao.java

package MaHangLian.Dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import MaHangLian.bean.Employee;
import org.springframework.stereotype.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);
    }
}

resources.springMVC.xml





    


    
    
        
        
        
            
                
                    
                        
                        
                        
                        
                        
                        
                    
                
            
        
    


    
    







    



webapp.static.js.vue.js

要有一个vue.js文件。我没找到

templates.index.html




  
  index


首页


使用ServletAPI向request域对象共享数据
使用ModelAndView向request域对象共享数据
使用Model向request域对象共享数据
4、使用map向request域对象共享数据
5、使用ModelMap向request域对象共享数据
7、通过ServletAPI向向session域共享数据
8、向application域共享数据

##########################################################

视图测试

##########################################################

RESTful测试

##########################################################

RESTful案例

templates.employee_add.html




    
    addemployee


lastName:
email:
gender:male female

templates.employee_list.html




    
    employee_list


员工信息

员工信息
id lastName email gender options(add)
delete update

templates.employee_update.html




    
    Update Employee


lastName:
email:
gender:male female

templates.resful_dome.html




    
    restful_dome


restful_dome案例



员工首页


查看员工信息

pom.xml



    4.0.0

    org.example

    SpringMVCdo1
    1.0-SNAPSHOT

    
        1.8
        8
    

    
    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
        
    

你可能感兴趣的:(SpringMVC,restful,java,后端)