Skip to content

Commit 8baaa4d

Browse files
committed
Add Lesson 22 code
1 parent ce7d5c4 commit 8baaa4d

17 files changed

Lines changed: 668 additions & 0 deletions

File tree

Lesson22.CRUD_App2/pom.xml

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ru.alishev.springcourse</groupId>
8+
<artifactId>spring-crud-app1</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>war</packaging>
11+
12+
<name>spring-mvc-app1 Maven Webapp</name>
13+
<!-- FIXME change it to the project's website -->
14+
<url>http://www.example.com</url>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<maven.compiler.source>1.7</maven.compiler.source>
19+
<maven.compiler.target>1.7</maven.compiler.target>
20+
21+
<spring.version>5.2.1.RELEASE</spring.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>junit</groupId>
27+
<artifactId>junit</artifactId>
28+
<version>4.11</version>
29+
<scope>test</scope>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework</groupId>
34+
<artifactId>spring-core</artifactId>
35+
<version>${spring.version}</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework</groupId>
40+
<artifactId>spring-context</artifactId>
41+
<version>${spring.version}</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework</groupId>
46+
<artifactId>spring-web</artifactId>
47+
<version>${spring.version}</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.springframework</groupId>
52+
<artifactId>spring-webmvc</artifactId>
53+
<version>${spring.version}</version>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.thymeleaf</groupId>
58+
<artifactId>thymeleaf-spring5</artifactId>
59+
<version>3.0.11.RELEASE</version>
60+
</dependency>
61+
62+
<dependency>
63+
<groupId>javax.servlet</groupId>
64+
<artifactId>javax.servlet-api</artifactId>
65+
<version>4.0.1</version>
66+
<scope>provided</scope>
67+
</dependency>
68+
</dependencies>
69+
70+
<build>
71+
<finalName>spring-mvc-app1</finalName>
72+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
73+
<plugins>
74+
<plugin>
75+
<artifactId>maven-clean-plugin</artifactId>
76+
<version>3.1.0</version>
77+
</plugin>
78+
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
79+
<plugin>
80+
<artifactId>maven-resources-plugin</artifactId>
81+
<version>3.0.2</version>
82+
</plugin>
83+
<plugin>
84+
<artifactId>maven-compiler-plugin</artifactId>
85+
<version>3.8.0</version>
86+
</plugin>
87+
<plugin>
88+
<artifactId>maven-surefire-plugin</artifactId>
89+
<version>2.22.1</version>
90+
</plugin>
91+
<plugin>
92+
<artifactId>maven-war-plugin</artifactId>
93+
<version>3.2.2</version>
94+
</plugin>
95+
<plugin>
96+
<artifactId>maven-install-plugin</artifactId>
97+
<version>2.5.2</version>
98+
</plugin>
99+
<plugin>
100+
<artifactId>maven-deploy-plugin</artifactId>
101+
<version>2.8.2</version>
102+
</plugin>
103+
</plugins>
104+
</pluginManagement>
105+
<plugins>
106+
<plugin>
107+
<groupId>org.apache.maven.plugins</groupId>
108+
<artifactId>maven-compiler-plugin</artifactId>
109+
<configuration>
110+
<source>8</source>
111+
<target>8</target>
112+
</configuration>
113+
</plugin>
114+
</plugins>
115+
</build>
116+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ru.alishev.springcourse.config;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
/**
6+
* @author Neil Alishev
7+
*/
8+
public class MySpringMvcDispatcherSerlvetIntitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
9+
@Override
10+
protected Class<?>[] getRootConfigClasses() {
11+
return null;
12+
}
13+
14+
@Override
15+
protected Class<?>[] getServletConfigClasses() {
16+
return new Class[]{SpringConfig.class};
17+
}
18+
19+
@Override
20+
protected String[] getServletMappings() {
21+
return new String[]{"/"};
22+
}
23+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ru.alishev.springcourse.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.ComponentScan;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
9+
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
10+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
11+
import org.thymeleaf.spring5.SpringTemplateEngine;
12+
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
13+
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
14+
15+
/**
16+
* @author Neil Alishev
17+
*/
18+
@Configuration
19+
@ComponentScan("ru.alishev.springcourse")
20+
@EnableWebMvc
21+
public class SpringConfig implements WebMvcConfigurer {
22+
23+
private final ApplicationContext applicationContext;
24+
25+
@Autowired
26+
public SpringConfig(ApplicationContext applicationContext) {
27+
this.applicationContext = applicationContext;
28+
}
29+
30+
@Bean
31+
public SpringResourceTemplateResolver templateResolver() {
32+
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
33+
templateResolver.setApplicationContext(applicationContext);
34+
templateResolver.setPrefix("/WEB-INF/views/");
35+
templateResolver.setSuffix(".html");
36+
return templateResolver;
37+
}
38+
39+
@Bean
40+
public SpringTemplateEngine templateEngine() {
41+
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
42+
templateEngine.setTemplateResolver(templateResolver());
43+
templateEngine.setEnableSpringELCompiler(true);
44+
return templateEngine;
45+
}
46+
47+
@Override
48+
public void configureViewResolvers(ViewResolverRegistry registry) {
49+
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
50+
resolver.setTemplateEngine(templateEngine());
51+
registry.viewResolver(resolver);
52+
}
53+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ru.alishev.springcourse.controllers;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.ui.Model;
6+
import org.springframework.web.bind.annotation.*;
7+
import ru.alishev.springcourse.dao.PersonDAO;
8+
import ru.alishev.springcourse.models.Person;
9+
10+
/**
11+
* @author Neil Alishev
12+
*/
13+
@Controller
14+
@RequestMapping("/people")
15+
public class PeopleController {
16+
17+
private final PersonDAO personDAO;
18+
19+
@Autowired
20+
public PeopleController(PersonDAO personDAO) {
21+
this.personDAO = personDAO;
22+
}
23+
24+
@GetMapping()
25+
public String index(Model model) {
26+
model.addAttribute("people", personDAO.index());
27+
return "people/index";
28+
}
29+
30+
@GetMapping("/{id}")
31+
public String show(@PathVariable("id") int id, Model model) {
32+
model.addAttribute("person", personDAO.show(id));
33+
return "people/show";
34+
}
35+
36+
@GetMapping("/new")
37+
public String newPerson(@ModelAttribute("person") Person person) {
38+
return "people/new";
39+
}
40+
41+
@PostMapping()
42+
public String create(@ModelAttribute("person") Person person) {
43+
personDAO.save(person);
44+
return "redirect:/people";
45+
}
46+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ru.alishev.springcourse.dao;
2+
3+
import org.springframework.stereotype.Component;
4+
import ru.alishev.springcourse.models.Person;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/**
10+
* @author Neil Alishev
11+
*/
12+
@Component
13+
public class PersonDAO {
14+
private static int PEOPLE_COUNT;
15+
private List<Person> people;
16+
17+
{
18+
people = new ArrayList<>();
19+
20+
people.add(new Person(++PEOPLE_COUNT, "Tom"));
21+
people.add(new Person(++PEOPLE_COUNT, "Bob"));
22+
people.add(new Person(++PEOPLE_COUNT, "Mike"));
23+
people.add(new Person(++PEOPLE_COUNT, "Katy"));
24+
}
25+
26+
public List<Person> index() {
27+
return people;
28+
}
29+
30+
public Person show(int id) {
31+
return people.stream().filter(person -> person.getId() == id).findAny().orElse(null);
32+
}
33+
34+
public void save(Person person) {
35+
person.setId(++PEOPLE_COUNT);
36+
people.add(person);
37+
}
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ru.alishev.springcourse.models;
2+
3+
/**
4+
* @author Neil Alishev
5+
*/
6+
public class Person {
7+
private int id;
8+
private String name;
9+
10+
public Person() {
11+
12+
}
13+
14+
public Person(int id, String name) {
15+
this.id = id;
16+
this.name = name;
17+
}
18+
19+
public int getId() {
20+
return id;
21+
}
22+
23+
public void setId(int id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Index</title>
6+
</head>
7+
<body>
8+
9+
<div th:each="person : ${people}">
10+
<a th:href="@{/people/{id}(id=${person.getId()})}" th:text="${person.getName()}">user</a>
11+
</div>
12+
13+
</body>
14+
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>New person</title>
6+
</head>
7+
<body>
8+
9+
<form th:method="POST" th:action="@{/people}" th:object="${person}">
10+
<label for="name">Enter name: </label>
11+
<input type="text" th:field="*{name}" id="name"/>
12+
<br/>
13+
<input type="submit" value="Create!"/>
14+
</form>
15+
16+
</body>
17+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Show</title>
6+
</head>
7+
<body>
8+
<p th:text="${person.getName()}">VALUE</p>
9+
<p th:text="${person.getId()}">VALUE</p>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)