目录
理论
演示
代码
这里要注意几点:
1. SpringMVC中配置HiddenHttpMethodFilter;(SpringBoot自动配置好的);
2. 页面创建一个post表单
3. 创建一个input项,name="_method";值就是指定的请求方式
这里可以使用三目运算,判断,关键代码如下:
type为hidden的状态;
因为在修改的时候,会把people传给页面,但发现people有值的时候,就说明是修改界面,
但people没有值的时候,说明是添加界面!
同理,其他的用户属性也有三元判断!
程序运行截图如下:
添加添加人员后,添加好数据:
点击添加:
点击锅盖的修改(把名字修改为锅盖呵呵):
点击修改后:
程序结构如下:
源码如下:
MyMvcConfig.java
package addandeditdemo.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyMvcConfig {
@Bean
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("index.html").setViewName("index");
}
};
return adapter;
}
}
PeopleController.java
package addandeditdemo.demo.controller;
import addandeditdemo.demo.data.PeopleData;
import addandeditdemo.demo.entities.People;
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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import java.util.Collection;
@Controller
public class PeopleController {
@Autowired
PeopleData peopleData;
@GetMapping({"/index", "/"})
public String list(Model model){
Collection peoples = peopleData.getAll();
model.addAttribute("index", peoples);
return "index";
}
@GetMapping("/add")
public String toAddPage(){
return "add";
}
@PostMapping("/add")
public String addPeople(People people){
System.out.println(people);
peopleData.save(people);
return "redirect:/";
}
//修改页面
@GetMapping("/edit/{id}")
public String toEditPage(@PathVariable("id") Integer id, Model model){
People people = peopleData.get(id);
model.addAttribute("people", people);
return "/add";
}
//修改
@PutMapping("/add")
public String updatePeople(People people){
System.out.println(people);
peopleData.save(people);
return "redirect:/";
}
}
PeopleData.java
package addandeditdemo.demo.data;
import addandeditdemo.demo.entities.People;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Repository
public class PeopleData {
private static Map peoples = null;
static{
peoples = new HashMap();
peoples.put(1001, new People(1001, "妹爷", "[email protected]", 1, new Date()));
peoples.put(1002, new People(1002, "球球", "[email protected]", 0, new Date()));
peoples.put(1003, new People(1003, "猪小明", "[email protected]", 1, new Date()));
peoples.put(1004, new People(1004, "米线", "[email protected]", 0, new Date()));
peoples.put(1005, new People(1005, "腿腿", "[email protected]", 0, new Date()));
peoples.put(1006, new People(1006, "闰土", "[email protected]", 1, new Date()));
}
private static Integer initId = 1007;
public void save(People people){
if(people.getId() == null){
people.setId(initId++);
}
peoples.put(people.getId(), people);
}
public Collection getAll(){
return peoples.values();
}
public People get(Integer id){
return peoples.get(id);
}
}
People.java
package addandeditdemo.demo.entities;
import java.util.Date;
public class People {
private Integer id;
private String name;
private String email;
private Integer gender;
private Date birth;
public People(Integer id, String name, String email, Integer gender, Date birth) {
this.id = id;
this.name = name;
this.email = email;
this.gender = gender;
this.birth = birth;
}
public People(){
}
@Override
public String toString() {
return "People{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", gender=" + gender +
", birth=" + birth +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
add.html
Title
index.html
呵呵
application.properties
spring.thymeleaf.cache=false
spring.mvc.date-format=yyyy-MM-dd
pron.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.19.RELEASE
com.loginWebDemo
demo
0.0.1-SNAPSHOT
loginWeb
Demo project for Spring Boot
1.8
3.0.9.RELEASE
2.2.2
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.webjars
jquery
3.3.1
org.webjars
bootstrap
4.0.0
org.springframework.boot
spring-boot-maven-plugin