diff --git a/2022-05/spring-18-view/.gitignore b/2022-05/spring-18-view/.gitignore new file mode 100644 index 00000000..4ea52072 --- /dev/null +++ b/2022-05/spring-18-view/.gitignore @@ -0,0 +1,24 @@ +target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ diff --git a/2022-05/spring-18-view/pom.xml b/2022-05/spring-18-view/pom.xml new file mode 100644 index 00000000..f2ad9bdf --- /dev/null +++ b/2022-05/spring-18-view/pom.xml @@ -0,0 +1,21 @@ + + + 4.0.0 + + ru.otus + spring-mvc-view + 1.0 + + pom + + + spring-mvc-view-exercise + spring-mvc-view-demo + spring-mvc-view-solution1 + spring-mvc-view-solution2 + spring-mvc-view-solution3 + spring-mvc-view-solution4 + + diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/.gitignore b/2022-05/spring-18-view/spring-mvc-view-demo/.gitignore new file mode 100644 index 00000000..4ea52072 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/.gitignore @@ -0,0 +1,24 @@ +target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/pom.xml b/2022-05/spring-18-view/spring-mvc-view-demo/pom.xml new file mode 100644 index 00000000..8c4b9db0 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + + ru.otus + spring-mvc-view-demo + 1.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.6.3 + + + + + 11 + 11 + 7.0.2.Final + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + com.h2database + h2 + + + + org.springframework.data + spring-data-jpa + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-validation + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/Main.java b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/Main.java new file mode 100644 index 00000000..d33c8908 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/Main.java @@ -0,0 +1,27 @@ +package ru.otus.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import ru.otus.spring.domain.Person; +import ru.otus.spring.repostory.PersonRepository; + +import javax.annotation.PostConstruct; + +@SpringBootApplication +public class Main { + + public static void main(String[] args) { + SpringApplication.run(Main.class); + } + + @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") + @Autowired + private PersonRepository repository; + + @PostConstruct + public void init() { + repository.save(new Person("Pushkin")); + repository.save(new Person("Lermontov")); + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/config/LocalizationConfig.java b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/config/LocalizationConfig.java new file mode 100644 index 00000000..55871937 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/config/LocalizationConfig.java @@ -0,0 +1,35 @@ +package ru.otus.spring.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.i18n.CookieLocaleResolver; +import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; + +import java.util.Locale; + +@Configuration +public class LocalizationConfig implements WebMvcConfigurer { + + @Bean(name = "localeResolver") + public LocaleResolver localeResolver() { + var resolver = new CookieLocaleResolver(); + resolver.setDefaultLocale(new Locale("en")); + resolver.setCookieName("locale"); + return resolver; + } + + @Bean + public LocaleChangeInterceptor localeChangeInterceptor() { + var localeChangeInterceptor = new LocaleChangeInterceptor(); + localeChangeInterceptor.setParamName("lang"); + return localeChangeInterceptor; + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(localeChangeInterceptor()); + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/controller/NotFoundException.java b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/controller/NotFoundException.java new file mode 100644 index 00000000..41b48826 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/controller/NotFoundException.java @@ -0,0 +1,7 @@ +package ru.otus.spring.controller; + +class NotFoundException extends RuntimeException{ + + NotFoundException() { + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/controller/PersonController.java b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/controller/PersonController.java new file mode 100644 index 00000000..44d500af --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/controller/PersonController.java @@ -0,0 +1,50 @@ +package ru.otus.spring.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; +import ru.otus.spring.domain.Person; +import ru.otus.spring.dto.PersonDto; +import ru.otus.spring.repostory.PersonRepository; + +import javax.validation.Valid; +import java.util.List; + +@Controller +public class PersonController { + + private final PersonRepository repository; + + @Autowired + public PersonController(PersonRepository repository) { + this.repository = repository; + } + + @GetMapping("/") + public String listPage(Model model) { + List persons = repository.findAll(); + model.addAttribute("persons", persons); + return "list"; + } + + @GetMapping("/edit") + public String editPage(@RequestParam("id") int id, Model model) { + Person person = repository.findById(id).orElseThrow(NotFoundException::new); + model.addAttribute("person", person); + return "edit"; + } + + @Validated + @PostMapping("/edit") + public String savePerson(@Valid @ModelAttribute("person") PersonDto person, + BindingResult bindingResult, Model model) { + if (bindingResult.hasErrors()) { + return "edit"; + } + repository.save(person.toDomainObject()); + return "redirect:/"; + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/domain/Person.java b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/domain/Person.java new file mode 100644 index 00000000..a20f0032 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/domain/Person.java @@ -0,0 +1,42 @@ +package ru.otus.spring.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Person { + + @Id + @GeneratedValue + private int id; + private String name; + + public Person() { + } + + public Person(String name) { + this.name = name; + } + + public Person(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/dto/PersonDto.java b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/dto/PersonDto.java new file mode 100644 index 00000000..743d91cc --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/dto/PersonDto.java @@ -0,0 +1,51 @@ +package ru.otus.spring.dto; + +import ru.otus.spring.domain.Person; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; + +public class PersonDto { + + private int id; + + @NotBlank(message = "{name-field-should-not-be-blank}") + @Size(min = 2, max = 10, message = "{name-field-should-has-expected-size}") + private String name; + + public PersonDto() { + } + + public PersonDto(String name) { + this.name = name; + } + + public PersonDto(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Person toDomainObject(){ + return new Person(id, name); + } + + public static PersonDto fromDomainObject(Person person) { + return new PersonDto(person.getId(), person.getName()); + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/repostory/PersonRepository.java b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/repostory/PersonRepository.java new file mode 100644 index 00000000..4b20e5b7 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/java/ru/otus/spring/repostory/PersonRepository.java @@ -0,0 +1,11 @@ +package ru.otus.spring.repostory; + +import org.springframework.data.repository.CrudRepository; +import ru.otus.spring.domain.Person; + +import java.util.List; + +public interface PersonRepository extends CrudRepository { + + List findAll(); +} diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/application.yml b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/application.yml new file mode 100644 index 00000000..407f9445 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/application.yml @@ -0,0 +1,3 @@ +spring: + messages: + encoding: UTF-8 \ No newline at end of file diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/messages.properties b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/messages.properties new file mode 100644 index 00000000..216bf5a6 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/messages.properties @@ -0,0 +1,10 @@ +lang-switcher-header=Select language +en-lang-switch-button-caption=Language - EN +ru-lang-switch-button-caption=Language - RU +persons-table-header=Persons: +edit-button-caption=Edit +person-form-header=Person Info: +save-button-caption=Save +cancel-button-caption=Cancel +name-field-should-not-be-blank=Name field should not be blank +name-field-should-has-expected-size=Name field should be between 2 and 10 characters \ No newline at end of file diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/messages_en.properties b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/messages_en.properties new file mode 100644 index 00000000..216bf5a6 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/messages_en.properties @@ -0,0 +1,10 @@ +lang-switcher-header=Select language +en-lang-switch-button-caption=Language - EN +ru-lang-switch-button-caption=Language - RU +persons-table-header=Persons: +edit-button-caption=Edit +person-form-header=Person Info: +save-button-caption=Save +cancel-button-caption=Cancel +name-field-should-not-be-blank=Name field should not be blank +name-field-should-has-expected-size=Name field should be between 2 and 10 characters \ No newline at end of file diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/messages_ru.properties b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/messages_ru.properties new file mode 100644 index 00000000..00e66529 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/messages_ru.properties @@ -0,0 +1,10 @@ +lang-switcher-header=Выбор языка +en-lang-switch-button-caption=Язык - EN +ru-lang-switch-button-caption=Язык - RU +persons-table-header=Пёрсоны: +edit-button-caption=Изменить +person-form-header=Информация о пёрсоне: +save-button-caption=Сохранить +cancel-button-caption=Отмена +name-field-should-not-be-blank=Имя не должно быть пустым +name-field-should-has-expected-size=Длина имени должна быть от 2 до 10 символов \ No newline at end of file diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/static/listmark.png b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/static/listmark.png new file mode 100644 index 00000000..f8eb391b Binary files /dev/null and b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/static/listmark.png differ diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/templates/edit.html b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/templates/edit.html new file mode 100644 index 00000000..3eb2f065 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/templates/edit.html @@ -0,0 +1,69 @@ + + + + + Edit person + + + + + + + +
+

Person Info:

+ +
+ + +
+ +
+ + +
Wrong person name error
+
+ +
+ + +
+
+ + + diff --git a/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/templates/list.html b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/templates/list.html new file mode 100644 index 00000000..f345f6b7 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-demo/src/main/resources/templates/list.html @@ -0,0 +1,74 @@ + + + + + List of all persons + + + + + + +

Select language

+
+
Language - EN
+
Language - RU
+
+ +

Persons:

+ + + + + + + + + + + + + + + + +
IDNameAction
1John Doe + Edit +
+ + diff --git a/2022-05/spring-18-view/spring-mvc-view-exercise/.gitignore b/2022-05/spring-18-view/spring-mvc-view-exercise/.gitignore new file mode 100644 index 00000000..4ea52072 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-exercise/.gitignore @@ -0,0 +1,24 @@ +target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ diff --git a/2022-05/spring-18-view/spring-mvc-view-exercise/pom.xml b/2022-05/spring-18-view/spring-mvc-view-exercise/pom.xml new file mode 100644 index 00000000..2b922735 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-exercise/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + ru.otus + spring-mvc-view-exercise + 1.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.6.3 + + + + + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + com.h2database + h2 + + + + org.springframework.data + spring-data-jpa + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/Main.java b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/Main.java new file mode 100644 index 00000000..395af78d --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/Main.java @@ -0,0 +1,28 @@ +package ru.otus.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import ru.otus.spring.domain.Person; +import ru.otus.spring.repostory.PersonRepository; + +import javax.annotation.PostConstruct; + +@SpringBootApplication +public class Main { + + public static void main(String[] args) { + SpringApplication.run(Main.class); + } + + //Чтобы не усложнять пример, делать так нельзя :) + @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") + @Autowired + private PersonRepository repository; + + @PostConstruct + public void init() { + repository.save(new Person("Pushkin")); + repository.save(new Person("Lermontov")); + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/controller/NotFoundException.java b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/controller/NotFoundException.java new file mode 100644 index 00000000..41b48826 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/controller/NotFoundException.java @@ -0,0 +1,7 @@ +package ru.otus.spring.controller; + +class NotFoundException extends RuntimeException{ + + NotFoundException() { + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/controller/PersonController.java b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/controller/PersonController.java new file mode 100644 index 00000000..248dc4ed --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/controller/PersonController.java @@ -0,0 +1,33 @@ +package ru.otus.spring.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import ru.otus.spring.domain.Person; +import ru.otus.spring.repostory.PersonRepository; + +import java.util.List; + +@Controller +public class PersonController { + + private final PersonRepository repository; + + @Autowired + public PersonController(PersonRepository repository) { + this.repository = repository; + } + + @GetMapping("/") + public String listPage(Model model) { + List persons = repository.findAll(); + model.addAttribute("persons", persons); + return "list"; + } + + @GetMapping("/edit") + public String editPage(@RequestParam("id") int id, Model model) { + return null; + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/domain/Person.java b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/domain/Person.java new file mode 100644 index 00000000..374c55ac --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/domain/Person.java @@ -0,0 +1,37 @@ +package ru.otus.spring.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Person { + + @Id + @GeneratedValue + private int id; + private String name; + + public Person() { + } + + public Person(String name) { + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/repostory/PersonRepository.java b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/repostory/PersonRepository.java new file mode 100644 index 00000000..4b20e5b7 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/java/ru/otus/spring/repostory/PersonRepository.java @@ -0,0 +1,11 @@ +package ru.otus.spring.repostory; + +import org.springframework.data.repository.CrudRepository; +import ru.otus.spring.domain.Person; + +import java.util.List; + +public interface PersonRepository extends CrudRepository { + + List findAll(); +} diff --git a/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/resources/templates/edit.html b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/resources/templates/edit.html new file mode 100644 index 00000000..87d8c8f1 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/resources/templates/edit.html @@ -0,0 +1,49 @@ + + + + + Edit person + + + + + + +
+

Person Info:

+ +
+ + +
+ +
+ + +
+ +
+ + +
+
+ + + diff --git a/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/resources/templates/list.html b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/resources/templates/list.html new file mode 100644 index 00000000..a5b52a8d --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-exercise/src/main/resources/templates/list.html @@ -0,0 +1,49 @@ + + + + + List of all persons + + + +

Persons:

+ + + + + + + + + + + + + + + + +
IDNameAction
1John Doe + Edit +
+ + diff --git a/2022-05/spring-18-view/spring-mvc-view-solution1/.gitignore b/2022-05/spring-18-view/spring-mvc-view-solution1/.gitignore new file mode 100644 index 00000000..4ea52072 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution1/.gitignore @@ -0,0 +1,24 @@ +target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ diff --git a/2022-05/spring-18-view/spring-mvc-view-solution1/pom.xml b/2022-05/spring-18-view/spring-mvc-view-solution1/pom.xml new file mode 100644 index 00000000..94cb0b54 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution1/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + ru.otus + spring-mvc-view-solution1 + 1.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.6.3 + + + + + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + com.h2database + h2 + + + + org.springframework.data + spring-data-jpa + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/Main.java b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/Main.java new file mode 100644 index 00000000..c41d5ecf --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/Main.java @@ -0,0 +1,29 @@ +package ru.otus.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import ru.otus.spring.domain.Person; +import ru.otus.spring.repostory.PersonRepository; + +import javax.annotation.PostConstruct; + +//http://localhost:8080/edit?id=1 +@SpringBootApplication +public class Main { + + public static void main(String[] args) { + SpringApplication.run(Main.class); + } + + //Чтобы не усложнять пример, делать так нельзя :) + @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") + @Autowired + private PersonRepository repository; + + @PostConstruct + public void init() { + repository.save(new Person("Pushkin")); + repository.save(new Person("Lermontov")); + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/controller/NotFoundException.java b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/controller/NotFoundException.java new file mode 100644 index 00000000..41b48826 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/controller/NotFoundException.java @@ -0,0 +1,7 @@ +package ru.otus.spring.controller; + +class NotFoundException extends RuntimeException{ + + NotFoundException() { + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/controller/PersonController.java b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/controller/PersonController.java new file mode 100644 index 00000000..51c7edf8 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/controller/PersonController.java @@ -0,0 +1,35 @@ +package ru.otus.spring.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import ru.otus.spring.domain.Person; +import ru.otus.spring.repostory.PersonRepository; + +import java.util.List; + +@Controller +public class PersonController { + + private final PersonRepository repository; + + @Autowired + public PersonController(PersonRepository repository) { + this.repository = repository; + } + + @GetMapping("/") + public String listPage(Model model) { + List persons = repository.findAll(); + model.addAttribute("persons", persons); + return "list"; + } + + @GetMapping("/edit") + public String editPage(@RequestParam("id") int id, Model model) { + Person person = repository.findById(id).orElseThrow(NotFoundException::new); + model.addAttribute("person", person); + return "edit"; + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/domain/Person.java b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/domain/Person.java new file mode 100644 index 00000000..374c55ac --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/domain/Person.java @@ -0,0 +1,37 @@ +package ru.otus.spring.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Person { + + @Id + @GeneratedValue + private int id; + private String name; + + public Person() { + } + + public Person(String name) { + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/repostory/PersonRepository.java b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/repostory/PersonRepository.java new file mode 100644 index 00000000..4b20e5b7 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/java/ru/otus/spring/repostory/PersonRepository.java @@ -0,0 +1,11 @@ +package ru.otus.spring.repostory; + +import org.springframework.data.repository.CrudRepository; +import ru.otus.spring.domain.Person; + +import java.util.List; + +public interface PersonRepository extends CrudRepository { + + List findAll(); +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/resources/templates/edit.html b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/resources/templates/edit.html new file mode 100644 index 00000000..7b3f5d6a --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/resources/templates/edit.html @@ -0,0 +1,48 @@ + + + + + Edit person + + + + + +
+

Person Info:

+ +
+ + +
+ +
+ + +
+ +
+ + +
+
+ + + diff --git a/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/resources/templates/list.html b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/resources/templates/list.html new file mode 100644 index 00000000..0d59a759 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution1/src/main/resources/templates/list.html @@ -0,0 +1,50 @@ + + + + + List of all persons + + + + +

Persons:

+ + + + + + + + + + + + + + + + +
IDNameAction
1John Doe + Edit +
+ + diff --git a/2022-05/spring-18-view/spring-mvc-view-solution2/.gitignore b/2022-05/spring-18-view/spring-mvc-view-solution2/.gitignore new file mode 100644 index 00000000..4ea52072 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution2/.gitignore @@ -0,0 +1,24 @@ +target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ diff --git a/2022-05/spring-18-view/spring-mvc-view-solution2/pom.xml b/2022-05/spring-18-view/spring-mvc-view-solution2/pom.xml new file mode 100644 index 00000000..f88c66cd --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution2/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + ru.otus + spring-mvc-view-solution2 + 1.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.6.3 + + + + + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + com.h2database + h2 + + + + org.springframework.data + spring-data-jpa + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/Main.java b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/Main.java new file mode 100644 index 00000000..c41d5ecf --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/Main.java @@ -0,0 +1,29 @@ +package ru.otus.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import ru.otus.spring.domain.Person; +import ru.otus.spring.repostory.PersonRepository; + +import javax.annotation.PostConstruct; + +//http://localhost:8080/edit?id=1 +@SpringBootApplication +public class Main { + + public static void main(String[] args) { + SpringApplication.run(Main.class); + } + + //Чтобы не усложнять пример, делать так нельзя :) + @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") + @Autowired + private PersonRepository repository; + + @PostConstruct + public void init() { + repository.save(new Person("Pushkin")); + repository.save(new Person("Lermontov")); + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/controller/NotFoundException.java b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/controller/NotFoundException.java new file mode 100644 index 00000000..41b48826 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/controller/NotFoundException.java @@ -0,0 +1,7 @@ +package ru.otus.spring.controller; + +class NotFoundException extends RuntimeException{ + + NotFoundException() { + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/controller/PersonController.java b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/controller/PersonController.java new file mode 100644 index 00000000..51c7edf8 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/controller/PersonController.java @@ -0,0 +1,35 @@ +package ru.otus.spring.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import ru.otus.spring.domain.Person; +import ru.otus.spring.repostory.PersonRepository; + +import java.util.List; + +@Controller +public class PersonController { + + private final PersonRepository repository; + + @Autowired + public PersonController(PersonRepository repository) { + this.repository = repository; + } + + @GetMapping("/") + public String listPage(Model model) { + List persons = repository.findAll(); + model.addAttribute("persons", persons); + return "list"; + } + + @GetMapping("/edit") + public String editPage(@RequestParam("id") int id, Model model) { + Person person = repository.findById(id).orElseThrow(NotFoundException::new); + model.addAttribute("person", person); + return "edit"; + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/domain/Person.java b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/domain/Person.java new file mode 100644 index 00000000..374c55ac --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/domain/Person.java @@ -0,0 +1,37 @@ +package ru.otus.spring.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Person { + + @Id + @GeneratedValue + private int id; + private String name; + + public Person() { + } + + public Person(String name) { + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/repostory/PersonRepository.java b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/repostory/PersonRepository.java new file mode 100644 index 00000000..4b20e5b7 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/java/ru/otus/spring/repostory/PersonRepository.java @@ -0,0 +1,11 @@ +package ru.otus.spring.repostory; + +import org.springframework.data.repository.CrudRepository; +import ru.otus.spring.domain.Person; + +import java.util.List; + +public interface PersonRepository extends CrudRepository { + + List findAll(); +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/resources/templates/edit.html b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/resources/templates/edit.html new file mode 100644 index 00000000..ef643cff --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/resources/templates/edit.html @@ -0,0 +1,48 @@ + + + + + Edit person + + + + + +
+

Person Info:

+ +
+ + +
+ +
+ + +
+ +
+ + +
+
+ + + diff --git a/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/resources/templates/list.html b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/resources/templates/list.html new file mode 100644 index 00000000..a5b52a8d --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution2/src/main/resources/templates/list.html @@ -0,0 +1,49 @@ + + + + + List of all persons + + + +

Persons:

+ + + + + + + + + + + + + + + + +
IDNameAction
1John Doe + Edit +
+ + diff --git a/2022-05/spring-18-view/spring-mvc-view-solution3/.gitignore b/2022-05/spring-18-view/spring-mvc-view-solution3/.gitignore new file mode 100644 index 00000000..4ea52072 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution3/.gitignore @@ -0,0 +1,24 @@ +target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ diff --git a/2022-05/spring-18-view/spring-mvc-view-solution3/pom.xml b/2022-05/spring-18-view/spring-mvc-view-solution3/pom.xml new file mode 100644 index 00000000..d3ad8d2a --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution3/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + ru.otus + spring-mvc-view-solution3 + 1.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.6.3 + + + + + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + com.h2database + h2 + + + + org.springframework.data + spring-data-jpa + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/Main.java b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/Main.java new file mode 100644 index 00000000..aa67bfa1 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/Main.java @@ -0,0 +1,30 @@ +package ru.otus.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import ru.otus.spring.domain.Person; +import ru.otus.spring.repostory.PersonRepository; + +import javax.annotation.PostConstruct; + +//http://localhost:8080 +//http://localhost:8080/edit?id=1 +@SpringBootApplication +public class Main { + + public static void main(String[] args) { + SpringApplication.run(Main.class); + } + + //Чтобы не усложнять пример, делать так нельзя :) + @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") + @Autowired + private PersonRepository repository; + + @PostConstruct + public void init() { + repository.save(new Person("Pushkin")); + repository.save(new Person("Lermontov")); + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/controller/NotFoundException.java b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/controller/NotFoundException.java new file mode 100644 index 00000000..41b48826 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/controller/NotFoundException.java @@ -0,0 +1,7 @@ +package ru.otus.spring.controller; + +class NotFoundException extends RuntimeException{ + + NotFoundException() { + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/controller/PersonController.java b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/controller/PersonController.java new file mode 100644 index 00000000..51c7edf8 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/controller/PersonController.java @@ -0,0 +1,35 @@ +package ru.otus.spring.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import ru.otus.spring.domain.Person; +import ru.otus.spring.repostory.PersonRepository; + +import java.util.List; + +@Controller +public class PersonController { + + private final PersonRepository repository; + + @Autowired + public PersonController(PersonRepository repository) { + this.repository = repository; + } + + @GetMapping("/") + public String listPage(Model model) { + List persons = repository.findAll(); + model.addAttribute("persons", persons); + return "list"; + } + + @GetMapping("/edit") + public String editPage(@RequestParam("id") int id, Model model) { + Person person = repository.findById(id).orElseThrow(NotFoundException::new); + model.addAttribute("person", person); + return "edit"; + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/domain/Person.java b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/domain/Person.java new file mode 100644 index 00000000..374c55ac --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/domain/Person.java @@ -0,0 +1,37 @@ +package ru.otus.spring.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Person { + + @Id + @GeneratedValue + private int id; + private String name; + + public Person() { + } + + public Person(String name) { + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/repostory/PersonRepository.java b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/repostory/PersonRepository.java new file mode 100644 index 00000000..4b20e5b7 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/java/ru/otus/spring/repostory/PersonRepository.java @@ -0,0 +1,11 @@ +package ru.otus.spring.repostory; + +import org.springframework.data.repository.CrudRepository; +import ru.otus.spring.domain.Person; + +import java.util.List; + +public interface PersonRepository extends CrudRepository { + + List findAll(); +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/resources/static/listmark.png b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/resources/static/listmark.png new file mode 100644 index 00000000..f8eb391b Binary files /dev/null and b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/resources/static/listmark.png differ diff --git a/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/resources/templates/edit.html b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/resources/templates/edit.html new file mode 100644 index 00000000..ef643cff --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/resources/templates/edit.html @@ -0,0 +1,48 @@ + + + + + Edit person + + + + + +
+

Person Info:

+ +
+ + +
+ +
+ + +
+ +
+ + +
+
+ + + diff --git a/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/resources/templates/list.html b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/resources/templates/list.html new file mode 100644 index 00000000..19fec79d --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution3/src/main/resources/templates/list.html @@ -0,0 +1,49 @@ + + + + + List of all persons + + + +

Persons:

+ + + + + + + + + + + + + + + + +
IDNameAction
1John Doe + Edit +
+ + diff --git a/2022-05/spring-18-view/spring-mvc-view-solution4/.gitignore b/2022-05/spring-18-view/spring-mvc-view-solution4/.gitignore new file mode 100644 index 00000000..4ea52072 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution4/.gitignore @@ -0,0 +1,24 @@ +target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ diff --git a/2022-05/spring-18-view/spring-mvc-view-solution4/pom.xml b/2022-05/spring-18-view/spring-mvc-view-solution4/pom.xml new file mode 100644 index 00000000..d89ba256 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution4/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + ru.otus + spring-mvc-view-solution4 + 1.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.6.3 + + + + + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + com.h2database + h2 + + + + org.springframework.data + spring-data-jpa + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/Main.java b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/Main.java new file mode 100644 index 00000000..aa67bfa1 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/Main.java @@ -0,0 +1,30 @@ +package ru.otus.spring; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import ru.otus.spring.domain.Person; +import ru.otus.spring.repostory.PersonRepository; + +import javax.annotation.PostConstruct; + +//http://localhost:8080 +//http://localhost:8080/edit?id=1 +@SpringBootApplication +public class Main { + + public static void main(String[] args) { + SpringApplication.run(Main.class); + } + + //Чтобы не усложнять пример, делать так нельзя :) + @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") + @Autowired + private PersonRepository repository; + + @PostConstruct + public void init() { + repository.save(new Person("Pushkin")); + repository.save(new Person("Lermontov")); + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/controller/NotFoundException.java b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/controller/NotFoundException.java new file mode 100644 index 00000000..41b48826 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/controller/NotFoundException.java @@ -0,0 +1,7 @@ +package ru.otus.spring.controller; + +class NotFoundException extends RuntimeException{ + + NotFoundException() { + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/controller/PersonController.java b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/controller/PersonController.java new file mode 100644 index 00000000..f1a367f9 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/controller/PersonController.java @@ -0,0 +1,41 @@ +package ru.otus.spring.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import ru.otus.spring.domain.Person; +import ru.otus.spring.repostory.PersonRepository; + +import java.util.List; + +@Controller +public class PersonController { + + private final PersonRepository repository; + + @Autowired + public PersonController(PersonRepository repository) { + this.repository = repository; + } + + @GetMapping("/") + public String listPage(Model model) { + List persons = repository.findAll(); + model.addAttribute("persons", persons); + return "list"; + } + + @GetMapping("/edit") + public String editPage(@RequestParam("id") int id, Model model) { + Person person = repository.findById(id).orElseThrow(NotFoundException::new); + model.addAttribute("person", person); + return "edit"; + } + + @PostMapping("/edit") + public String savePerson(Person person) { + repository.save(person); + return "redirect:/"; + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/domain/Person.java b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/domain/Person.java new file mode 100644 index 00000000..374c55ac --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/domain/Person.java @@ -0,0 +1,37 @@ +package ru.otus.spring.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Person { + + @Id + @GeneratedValue + private int id; + private String name; + + public Person() { + } + + public Person(String name) { + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/repostory/PersonRepository.java b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/repostory/PersonRepository.java new file mode 100644 index 00000000..4b20e5b7 --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/java/ru/otus/spring/repostory/PersonRepository.java @@ -0,0 +1,11 @@ +package ru.otus.spring.repostory; + +import org.springframework.data.repository.CrudRepository; +import ru.otus.spring.domain.Person; + +import java.util.List; + +public interface PersonRepository extends CrudRepository { + + List findAll(); +} diff --git a/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/resources/templates/edit.html b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/resources/templates/edit.html new file mode 100644 index 00000000..4482d17a --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/resources/templates/edit.html @@ -0,0 +1,48 @@ + + + + + Edit person + + + + + +
+

Person Info:

+ +
+ + +
+ +
+ + +
+ +
+ + +
+
+ + + diff --git a/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/resources/templates/list.html b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/resources/templates/list.html new file mode 100644 index 00000000..9af2d61c --- /dev/null +++ b/2022-05/spring-18-view/spring-mvc-view-solution4/src/main/resources/templates/list.html @@ -0,0 +1,49 @@ + + + + + List of all persons + + + +

Persons:

+ + + + + + + + + + + + + + + + +
IDNameAction
1John Doe + Edit +
+ +