Skip to content

Commit 9e8b718

Browse files
committed
еÑtherst commit in this course
1 parent 3ad9194 commit 9e8b718

File tree

22 files changed

+326
-45
lines changed

22 files changed

+326
-45
lines changed

Lesson17.SpringControllersIntro/src/main/java/ru/alishev/springcourse/controllers/FirstController.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package ru.alishev.springcourse.controllers;
22

33
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
45
import org.springframework.web.bind.annotation.GetMapping;
56
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
68

79
/**
810
* @author Neil Alishev
@@ -11,11 +13,34 @@
1113
@RequestMapping("/first")
1214
public class FirstController {
1315

16+
1417
@GetMapping("/hello")
15-
public String helloPage() {
18+
public String helloPage(@RequestParam(value="a", required = false) int a,
19+
@RequestParam(value="b", required = false) int b,
20+
@RequestParam(value="action", required = false) String action,
21+
Model model) {
22+
model.addAttribute("someMessage","result "+ a + action + b +" = " + calculate(a,b,action));
23+
24+
//int sum = calculate(a,b,action);
25+
26+
//System.out.println("Hello, " + name + " " + surname);
1627
return "first/hello";
1728
}
1829

30+
public int calculate(int a, int b, String action) {
31+
switch (action) {
32+
case "multiplication":
33+
return a * b;
34+
case "addition":
35+
return a+b;
36+
case "substruction":
37+
return a%b;
38+
case "division":
39+
return a-b;
40+
default: return 0;
41+
}
42+
}
43+
1944
@GetMapping("/goodbye")
2045
public String goodByePage() {
2146
return "first/goodbye";

Lesson17.SpringControllersIntro/src/main/webapp/WEB-INF/views/first/goodbye.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
Goodbye!
99

1010
<a href="/first/hello">Say hello</a> or <a href="/exit">Exit</a>
11+
or <a href ="/first/hello?name=Pidor&surname=Chertov">Request with anower parameters!!OMG</a>
1112
</body>
1213
</html>
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html lang="en">
2+
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
33
<head>
44
<meta charset="UTF-8">
55
<title>Hello</title>
@@ -8,5 +8,10 @@
88
Hello!
99

1010
<a href="/first/goodbye">Say goodbye</a> or <a href="/exit">Exit</a>
11+
or <a href ="/first/hello?a=4&b=2&action=addition">Request with parameters</a>
12+
13+
<p th:text="${someMessage}" />
14+
15+
1116
</body>
1217
</html>

Lesson20_Starter.IntroToModel/pom.xml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@
1818
<maven.compiler.source>1.7</maven.compiler.source>
1919
<maven.compiler.target>1.7</maven.compiler.target>
2020

21-
<spring.version>5.2.1.RELEASE</spring.version>
21+
<spring.version>6.0.4</spring.version>
2222
</properties>
2323

2424
<dependencies>
25+
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
2526
<dependency>
26-
<groupId>junit</groupId>
27-
<artifactId>junit</artifactId>
28-
<version>4.11</version>
27+
<groupId>org.junit.jupiter</groupId>
28+
<artifactId>junit-jupiter-api</artifactId>
29+
<version>5.9.2</version>
2930
<scope>test</scope>
3031
</dependency>
3132

33+
3234
<dependency>
3335
<groupId>org.springframework</groupId>
3436
<artifactId>spring-core</artifactId>
@@ -59,12 +61,16 @@
5961
<version>3.0.11.RELEASE</version>
6062
</dependency>
6163

64+
<!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
6265
<dependency>
63-
<groupId>javax.servlet</groupId>
64-
<artifactId>javax.servlet-api</artifactId>
65-
<version>4.0.1</version>
66+
<groupId>jakarta.servlet</groupId>
67+
<artifactId>jakarta.servlet-api</artifactId>
68+
<version>6.0.0</version>
6669
<scope>provided</scope>
6770
</dependency>
71+
72+
73+
6874
</dependencies>
6975

7076
<build>

Lesson21.CRUD_App1/pom.xml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

33
<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">
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66

77
<groupId>ru.alishev.springcourse</groupId>
@@ -102,15 +102,15 @@
102102
</plugin>
103103
</plugins>
104104
</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>
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>
115115
</build>
116-
</project>
116+
</project>

Lesson21.CRUD_App1/src/main/java/ru/alishev/springcourse/config/MySpringMvcDispatcherSerlvetIntitializer.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package ru.alishev.springcourse.config;
22

3+
import org.springframework.web.filter.HiddenHttpMethodFilter;
34
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
45

6+
import javax.servlet.ServletContext;
7+
import javax.servlet.ServletException;
8+
59
/**
610
* @author Neil Alishev
711
*/
@@ -20,4 +24,15 @@ protected Class<?>[] getServletConfigClasses() {
2024
protected String[] getServletMappings() {
2125
return new String[]{"/"};
2226
}
27+
28+
@Override
29+
public void onStartup(ServletContext aServletContext) throws ServletException {
30+
super.onStartup(aServletContext);
31+
registerHiddenFieldFilter(aServletContext);
32+
}
33+
34+
private void registerHiddenFieldFilter(ServletContext aContext) {
35+
aContext.addFilter("hiddenHttpMethodFilter",
36+
new HiddenHttpMethodFilter()).addMappingForUrlPatterns(null ,true, "/*");
37+
}
2338
}

Lesson21.CRUD_App1/src/main/java/ru/alishev/springcourse/controllers/PeopleController.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.stereotype.Controller;
55
import org.springframework.ui.Model;
6-
import org.springframework.web.bind.annotation.GetMapping;
7-
import org.springframework.web.bind.annotation.PathVariable;
8-
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.*;
97
import ru.alishev.springcourse.dao.PersonDAO;
8+
import ru.alishev.springcourse.models.Person;
109

1110
/**
1211
* @author Neil Alishev
@@ -33,4 +32,32 @@ public String show(@PathVariable("id") int id, Model model) {
3332
model.addAttribute("person", personDAO.show(id));
3433
return "people/show";
3534
}
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+
@GetMapping("/{id}/edit")
47+
public String edit (Model model,@PathVariable("id") int id) {
48+
model.addAttribute("person",personDAO.show(id));
49+
return "people/edit";
50+
}
51+
52+
@PatchMapping("/{id}")
53+
public String update (@ModelAttribute("person") Person person, @PathVariable("id") int id) {
54+
personDAO.update(id,person);
55+
return "redirect:/people";
56+
}
57+
58+
@DeleteMapping("/{id}")
59+
public String delete (@PathVariable("id") int id) {
60+
personDAO.delete(id);
61+
return "redirect:/people";
62+
}
3663
}

Lesson21.CRUD_App1/src/main/java/ru/alishev/springcourse/dao/PersonDAO.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,19 @@ public List<Person> index() {
3030
public Person show(int id) {
3131
return people.stream().filter(person -> person.getId() == id).findAny().orElse(null);
3232
}
33+
34+
public void save(Person person) {
35+
person.setId(++PEOPLE_COUNT);
36+
people.add(person);
37+
}
38+
39+
public void update (int id, Person updatePerson) {
40+
Person personToBeUpdated = show(id);
41+
42+
personToBeUpdated.setName(updatePerson.getName());
43+
}
44+
45+
public void delete (int id) {
46+
people.removeIf(p -> p.getId()==id);
47+
}
3348
}

Lesson21.CRUD_App1/src/main/java/ru/alishev/springcourse/models/Person.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ public class Person {
77
private int id;
88
private String name;
99

10+
public Person () {
11+
12+
}
13+
1014
public Person(int id, String name) {
1115
this.id = id;
1216
this.name = name;
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>Update person</title>
6+
</head>
7+
<body>
8+
9+
<form th:method="Patch" th:action="@{/people/{id}(id=${person.getId()})}" 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="Update!"/>
14+
</form>
15+
16+
</body>
17+
</html>

0 commit comments

Comments
 (0)