Skip to content

Commit 74b182e

Browse files
committed
Add Lesson 20 code
1 parent ce789ec commit 74b182e

17 files changed

Lines changed: 567 additions & 0 deletions

File tree

Lesson20.IntroToModel/pom.xml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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>intro-to-model</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+
</build>
106+
</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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ru.alishev.springcourse.controllers;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
9+
/**
10+
* @author Neil Alishev
11+
*/
12+
@Controller
13+
@RequestMapping("/first")
14+
public class FirstController {
15+
16+
@GetMapping("/hello")
17+
public String helloPage(@RequestParam(value = "name", required = false) String name,
18+
@RequestParam(value = "surname", required = false) String surname,
19+
Model model) {
20+
21+
// System.out.println("Hello, " + name + " " + surname);
22+
model.addAttribute("message", "Hello, " + name + " " + surname);
23+
24+
return "first/hello";
25+
}
26+
27+
@GetMapping("/goodbye")
28+
public String goodByePage() {
29+
return "first/goodbye";
30+
}
31+
32+
@GetMapping("/calculator")
33+
public String calculator(@RequestParam("a") int a, @RequestParam("b") int b,
34+
@RequestParam("action") String action, Model model) {
35+
36+
double result;
37+
38+
switch (action) {
39+
case "multiplication":
40+
result = a * b;
41+
break;
42+
case "division":
43+
result = a / (double) b;
44+
break;
45+
case "subtraction":
46+
result = a - b;
47+
break;
48+
case "addition":
49+
result = a + b;
50+
break;
51+
default:
52+
result = 0;
53+
break;
54+
}
55+
56+
model.addAttribute("result", result);
57+
58+
return "first/calculator";
59+
}
60+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ru.alishev.springcourse.controllers;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
/**
7+
* @author Neil Alishev
8+
*/
9+
@Controller
10+
public class SecondController {
11+
12+
@GetMapping("/exit")
13+
public String exit() {
14+
return "second/exit";
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Calculator</title>
6+
</head>
7+
<body>
8+
The result is: <p th:text="${result}"/>
9+
</body>
10+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Goodbye</title>
6+
</head>
7+
<body>
8+
Goodbye!
9+
10+
<a href="/first/hello">Say hello</a> or <a href="/exit">Exit</a>
11+
</body>
12+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Hello</title>
6+
</head>
7+
<body>
8+
Hello!
9+
10+
<a href="/first/goodbye">Say goodbye</a> or <a href="/exit">Exit</a>
11+
or <a href="/first/hello?name=Tom&surname=Jone">Request with parameters</a>
12+
13+
<p th:text="${message}"/>
14+
15+
</body>
16+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Exit</title>
6+
</head>
7+
<body>
8+
No more links
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)