4
This commit is contained in:
1 parent
6bdf17d0f3
commit
9f9e4188c0
4 files changed
+37
-3
No files matched your search
@@ -1,12 +1,11 @@
|
|||||||
package org.example.controllers;
|
package org.example.controllers;
|
||||||
|
|
||||||
import org.example.dao.PersonDAO;
|
import org.example.dao.PersonDAO;
|
||||||
|
import org.example.models.Person;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/people")
|
@RequestMapping("/people")
|
||||||
@@ -29,4 +28,15 @@ public class PeopleController {
|
|||||||
model.addAttribute("person",personDAO.show(id));
|
model.addAttribute("person",personDAO.show(id));
|
||||||
return "people/show";
|
return "people/show";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/new")
|
||||||
|
public String newPerson(@ModelAttribute("person") Person person) {
|
||||||
|
return "people/new";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping()
|
||||||
|
public String create(@ModelAttribute("person") Person person){
|
||||||
|
personDAO.save(person);
|
||||||
|
return "redirect:people";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -22,4 +22,9 @@ public class PersonDAO {
|
|||||||
public Person show(int id) {
|
public Person show(int id) {
|
||||||
return people.stream().filter(person -> person.getId() == id).findAny().orElse(null);
|
return people.stream().filter(person -> person.getId() == id).findAny().orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void save(Person person) {
|
||||||
|
person.setId(people.size()+1);
|
||||||
|
people.add(person);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,12 @@ package org.example.models;
|
|||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
@Getter@Setter
|
@Getter@Setter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
public class Person
|
public class Person
|
||||||
{
|
{
|
||||||
private String name;
|
private String name;
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org" lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="ISO-8859-1">
|
||||||
|
<title>Save</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<form th:method="POST" th:action="@{/people}" th:object="${person}">
|
||||||
|
<label for="name">Enter name: </label>
|
||||||
|
<input type="text" th:field="*{name}" id="name"/>
|
||||||
|
<br/>
|
||||||
|
<input type="submit" value="Create!"/>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in new issue
Block a user