java带参方法怎么修改姓名_SpringMVC自动将请求参数和入参对象的属性进行一一绑定;要求请求参数的名字和javaBean入参的对象里面的属性名是一样的||员工的增删改查案例...
发布日期:2021-10-27 11:20:24 浏览次数:11 分类:技术文章

本文共 6715 字,大约阅读时间需要 22 分钟。

SpringMVC自动将请求参数和入参对象的属性进行一一绑定;要求请求参数的名字和javaBean入参的对象里面的属性名是一样的

1、SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自动配置好的)

2、页面创建一个post表单

3、创建一个input项,name="_method";值就是我们指定的请求方式

e0bbe46e28128387ed0828bdddd84626.png

a48224c2063885efc8f07cb795f4998b.png

bd3df39ff2334f13a30c74e7787fcc87.png

Department.java

package com.atguigu.springboot.entities;

public class Department {

private Integer id;

private String departmentName;

public Department() {

}

public Department(int i, String string) {

this.id = i;

this.departmentName = string;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getDepartmentName() {

return departmentName;

}

public void setDepartmentName(String departmentName) {

this.departmentName = departmentName;

}

@Override

public String toString() {

return "Department [id=" + id + ", departmentName=" + departmentName + "]";

}

}

Employee.java

package com.atguigu.springboot.entities;

import java.util.Date;

public class Employee {

private Integer id;

private String lastName;

private String email;

//1 male, 0 female

private Integer gender;

private Department department;

private Date birth;

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 Department getDepartment() {

return department;

}

public void setDepartment(Department department) {

this.department = department;

}

public Date getBirth() {

return birth;

}

public void setBirth(Date birth) {

this.birth = birth;

}

public Employee(Integer id, String lastName, String email, Integer gender,

Department department) {

super();

this.id = id;

this.lastName = lastName;

this.email = email;

this.gender = gender;

this.department = department;

this.birth = new Date();

}

public Employee() {

}

@Override

public String toString() {

return "Employee{" +

"id=" + id +

", lastName='" + lastName + '\'' +

", email='" + email + '\'' +

", gender=" + gender +

", department=" + department +

", birth=" + birth +

'}';

}

}

DepartmentDao.java

package com.atguigu.springboot.dao;

import java.util.Collection;

import java.util.HashMap;

import java.util.Map;

import com.atguigu.springboot.entities.Department;

import org.springframework.stereotype.Repository;

@Repository

public class DepartmentDao {

private static Mapdepartments = null;

static{

departments = new HashMap();

departments.put(101, new Department(101, "D-AA"));

departments.put(102, new Department(102, "D-BB"));

departments.put(103, new Department(103, "D-CC"));

departments.put(104, new Department(104, "D-DD"));

departments.put(105, new Department(105, "D-EE"));

}

public CollectiongetDepartments(){

return departments.values();

}

public Department getDepartment(Integer id){

return departments.get(id);

}

}

EmployeeDao.java

package com.atguigu.springboot.dao;

import java.util.Collection;

import java.util.HashMap;

import java.util.Map;

import com.atguigu.springboot.entities.Department;

import com.atguigu.springboot.entities.Employee;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;

@Repository

public class EmployeeDao {

private static Mapemployees = null;

@Autowired

private DepartmentDao departmentDao;

static{

employees = new HashMap();

employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));

employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));

employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));

employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));

employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));

}

private static Integer initId = 1006;

public void save(Employee employee){

if(employee.getId() == null){

employee.setId(initId++);

}

employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));

employees.put(employee.getId(), employee);

}

//查询所有员工

public CollectiongetAll(){

return employees.values();

}

public Employee get(Integer id){

return employees.get(id);

}

public void delete(Integer id){

employees.remove(id);

}

}

EmployeeController.java

package com.atguigu.springboot.controller;

import com.atguigu.springboot.dao.DepartmentDao;

import com.atguigu.springboot.dao.EmployeeDao;

import com.atguigu.springboot.entities.Department;

import com.atguigu.springboot.entities.Employee;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@Controller

public class EmployeeController {

@Autowired

EmployeeDao employeeDao;

@Autowired

DepartmentDao departmentDao;

//查询所有员工返回列表页面

@GetMapping("/emps")

public String list(Model model){

Collectionemployees = employeeDao.getAll();

//放在请求域中

model.addAttribute("emps",employees);

// thymeleaf默认就会拼串

// classpath:/templates/xxxx.html

return "emp/list";

}

//来到员工添加页面

@GetMapping("/emp")

public String toAddPage(Model model){

//来到添加页面,查出所有的部门,在页面显示

Collectiondepartments = departmentDao.getDepartments();

model.addAttribute("depts",departments);

return "emp/add";

}

//员工添加

//SpringMVC自动将请求参数和入参对象的属性进行一一绑定;要求请求参数的名字和javaBean入参的对象里面的属性名是一样的

@PostMapping("/emp")

public String addEmp(Employee employee){

//来到员工列表页面

System.out.println("保存的员工信息:"+employee);

//保存员工

employeeDao.save(employee);

// redirect: 表示重定向到一个地址 /代表当前项目路径

// forward: 表示转发到一个地址

return "redirect:/emps";

}

//来到修改页面,查出当前员工,在页面回显

@GetMapping("/emp/{id}")

public String toEditPage(@PathVariable("id") Integer id,Model model){

Employee employee = employeeDao.get(id);

model.addAttribute("emp",employee);

//页面要显示所有的部门列表

Collectiondepartments = departmentDao.getDepartments();

model.addAttribute("depts",departments);

//回到修改页面(add是一个修改添加二合一的页面);

return "emp/add";

}

//员工修改;需要提交员工id;

@PutMapping("/emp")

public String updateEmployee(Employee employee){

System.out.println("修改的员工数据:"+employee);

employeeDao.save(employee);

return "redirect:/emps";

}

//员工删除

@DeleteMapping("/emp/{id}")

public String deleteEmployee(@PathVariable("id") Integer id){

employeeDao.delete(id);

return "redirect:/emps";

}

}

list.html

Dashboard Template for Bootstrap

#

lastName

email

gender

department

birth

操作

[[${emp.lastName}]]

修改

删除

add.html

Dashboard Template for Bootstrap

LastName

Email

Gender

department

1

Birth

添加

http://localhost:8083/crud/emps

9e509a0ae39b52f54bb0df5ac9c8ac28.png

http://localhost:8083/crud/emp

6b9b42a24a2fe0ffb756d1171e883fa2.png

http://localhost:8083/crud/emp/1001

68295c7ac6e54c904e1ce935b30c9153.png

转载地址:https://blog.csdn.net/weixin_34518190/article/details/114879539 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:mysql简介分析_(一)MySQL的架构介绍
下一篇:java棋盘覆盖分治法_算法java实现--分治法--棋盘覆盖问题

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年03月25日 04时18分30秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

oracle direct for hdfs xi下载,ORACLE连接HDFS有个专项的解决方案 2019-04-21
java 403怎么抛出_java – 如何在Spring MVC中返回403禁止? 2019-04-21
java jsch工具类_Java工具集-JSch连接远程服务器工具类 2019-04-21
cmd背景变红1003无标题_怎样修改cmd中文字的大小、颜色和背景颜色呢 原来是这样的... 2019-04-21
php rand() 重复,php – mt_rand()给我总是相同的数字 2019-04-21
php taglib.php,thinkphp5 taglib自定义标签教程 2019-04-21
java常用包类 array,Java中的StringBuffer和数组Arrays以及常用类型的包装类 2019-04-21
ctf常见php,CTF中常见的PHP伪协议 2019-04-21
php语言冒泡法,PHP 冒泡排序法 2019-04-21
php如何数组去重复,PHP如何去除数组重复元素? 2019-04-21
java转换ab的值,查看新闻/公告--[整理]Java将AB1234形式的16进制字符串转换为10进制数值,考虑字节序的影响.... 2019-04-21
ui php h5,画出自己的UI组件的详情 2019-04-21
linux服务文件编写,linux编写systemd下服务脚本 2019-04-21
hdfs linux 目录是否存在,Linux中判断hdfs文件是否存在 2019-04-21
linux学习需要什么基础,学linux需要什么基础? 2019-04-21
linux vim编辑kconfig 无法wq,Linux-4.9.2内核在mini2440上的移植(三)——编译环境测试... 2019-04-21
高斯勒让德在c语言中的程序,c语言:用递归方法编写程序,求n阶勒让德多项式的值... 2019-04-21
c语言单片机电子时钟,新人求个51单片机的电子时钟汇编语言(C语言的还没学到)... 2019-04-21
c++语言文件流,C++文件流 2019-04-21
android 动态毛玻璃,Android毛玻璃背景效果简单实现代码 2019-04-21