4
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
package org.example.controllers;
|
||||
|
||||
import org.example.dao.PersonDAO;
|
||||
import org.example.models.Person;
|
||||
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.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/people")
|
||||
@@ -29,4 +28,15 @@ public class PeopleController {
|
||||
model.addAttribute("person",personDAO.show(id));
|
||||
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) {
|
||||
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.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Person
|
||||
{
|
||||
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