我是靠谱客的博主 笨笨白猫,最近开发中收集的这篇文章主要介绍java带参方法怎么修改姓名_SpringMVC自动将请求参数和入参对象的属性进行一一绑定;要求请求参数的名字和javaBean入参的对象里面的属性名是一样的||员工的增删改查案例...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

最后

以上就是笨笨白猫为你收集整理的java带参方法怎么修改姓名_SpringMVC自动将请求参数和入参对象的属性进行一一绑定;要求请求参数的名字和javaBean入参的对象里面的属性名是一样的||员工的增删改查案例...的全部内容,希望文章能够帮你解决java带参方法怎么修改姓名_SpringMVC自动将请求参数和入参对象的属性进行一一绑定;要求请求参数的名字和javaBean入参的对象里面的属性名是一样的||员工的增删改查案例...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部