欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

springMVC:RestFul案例3

时间:2023-06-16

主要用于展示最后的代码。项目结构如下:

1、web.xml

<?xml version="1.0" encoding="UTF-8"?> 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 /

2、springMVC.xml

<?xml version="1.0" encoding="UTF-8"?>

3、com.atguigu.rest下的java类

(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;@Controllerpublic 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持久层注解@Repositorypublic class EmployeeDao { private static Map employees = null; static { employees = new HashMap(); employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1)); employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1)); employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0)); employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0)); employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 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); }}

4、html

(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

首页首页查看员工信息

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。