Skip to content

Commit 27b8221

Browse files
committed
Add Lesson 16 code
1 parent 7885bf0 commit 27b8221

5 files changed

Lines changed: 212 additions & 0 deletions

File tree

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>spring-mvc-app-java-config</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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.alishev.springcourse;
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 HelloController {
11+
@GetMapping("/hello-world")
12+
public String sayHello() {
13+
return "hello_world";
14+
}
15+
}
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org" lang="en">
3+
4+
<head>
5+
<meta charset="ISO-8859-1">
6+
<title>My app</title>
7+
</head>
8+
9+
<body>
10+
11+
<p>Hello world!</p>
12+
13+
</body>
14+
15+
</html>

0 commit comments

Comments
 (0)