Skip to content

Commit 04f8eb5

Browse files
incredible-ioslavisa-baeldung
authored andcommitted
BAEL-86: Working sample.
1 parent c9073cb commit 04f8eb5

14 files changed

Lines changed: 408 additions & 0 deletions

File tree

‎enterprise-patterns/pom.xml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
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>com.baeldung.enterprise.patterns</groupId>
8+
<artifactId>enterprise-patterns-parent</artifactId>
9+
<packaging>pom</packaging>
10+
<modules>
11+
<module>spring-dispatcher-servlet</module>
12+
</modules>
13+
14+
<parent>
15+
<groupId>com.baeldung</groupId>
16+
<artifactId>parent-modules</artifactId>
17+
<version>1.0.0-SNAPSHOT</version>
18+
</parent>
19+
20+
<build>
21+
<pluginManagement>
22+
<plugins>
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-compiler-plugin</artifactId>
26+
<version>3.5.1</version>
27+
<configuration>
28+
<source>1.8</source>
29+
<target>1.8</target>
30+
</configuration>
31+
</plugin>
32+
</plugins>
33+
</pluginManagement>
34+
</build>
35+
</project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
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+
<artifactId>spring-dispatcher-servlet</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
<packaging>war</packaging>
10+
11+
<parent>
12+
<groupId>com.baeldung.enterprise.patterns</groupId>
13+
<artifactId>enterprise-patterns-parent</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
<relativePath>..</relativePath>
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>javax.servlet</groupId>
21+
<artifactId>javax.servlet-api</artifactId>
22+
<version>3.1.0</version>
23+
<scope>provided</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework</groupId>
27+
<artifactId>spring-webmvc</artifactId>
28+
<version>4.3.2.RELEASE</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.thymeleaf</groupId>
32+
<artifactId>thymeleaf-spring4</artifactId>
33+
<version>3.0.1.RELEASE</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.apache.logging.log4j</groupId>
37+
<artifactId>log4j-slf4j-impl</artifactId>
38+
<version>2.6.2</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.apache.logging.log4j</groupId>
42+
<artifactId>log4j-core</artifactId>
43+
<version>2.6.2</version>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-compiler-plugin</artifactId>
52+
</plugin>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-war-plugin</artifactId>
56+
<version>3.0.0</version>
57+
<configuration>
58+
<failOnMissingWebXml>false</failOnMissingWebXml>
59+
</configuration>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.eclipse.jetty</groupId>
63+
<artifactId>jetty-maven-plugin</artifactId>
64+
<version>9.4.0.M1</version>
65+
<configuration>
66+
<webApp>
67+
<contextPath>/</contextPath>
68+
</webApp>
69+
</configuration>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.enterprise.patterns.dispatcher.servlet;
2+
3+
import com.baeldung.enterprise.patterns.dispatcher.servlet.models.Task;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
import java.util.*;
8+
9+
@Configuration
10+
public class RootConfiguration {
11+
@Bean
12+
public Map<String, List<Task>> taskList() {
13+
Map<String, List<Task>> taskMap = new HashMap<>();
14+
List<Task> taskList = new ArrayList<>();
15+
taskList.add(new Task("Clean the dishes!", new Date()));
16+
taskMap.put("Cid", taskList);
17+
return taskMap;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.enterprise.patterns.dispatcher.servlet;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
8+
import org.thymeleaf.spring4.SpringTemplateEngine;
9+
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
10+
import org.thymeleaf.templatemode.TemplateMode;
11+
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
12+
13+
import javax.servlet.ServletContext;
14+
15+
@Configuration
16+
@ComponentScan("com.baeldung.enterprise.patterns.dispatcher.servlet.web")
17+
@EnableWebMvc
18+
public class WebConfiguration extends WebMvcConfigurerAdapter {
19+
@Bean
20+
public ServletContextTemplateResolver templateResolver(ServletContext servletContext) {
21+
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(servletContext);
22+
templateResolver.setPrefix("/WEB-INF/views/");
23+
templateResolver.setSuffix(".html");
24+
templateResolver.setTemplateMode(TemplateMode.HTML);
25+
return templateResolver;
26+
}
27+
28+
@Bean
29+
public SpringTemplateEngine templateEngine(ServletContextTemplateResolver templateResolver) {
30+
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
31+
templateEngine.setTemplateResolver(templateResolver);
32+
return templateEngine;
33+
}
34+
35+
@Bean
36+
public ThymeleafViewResolver viewResolver(SpringTemplateEngine templateEngine) {
37+
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
38+
viewResolver.setTemplateEngine(templateEngine);
39+
return viewResolver;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.enterprise.patterns.dispatcher.servlet;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
@Override
7+
protected Class<?>[] getRootConfigClasses() {
8+
return new Class<?>[]{RootConfiguration.class};
9+
}
10+
11+
@Override
12+
protected Class<?>[] getServletConfigClasses() {
13+
return new Class<?>[]{WebConfiguration.class};
14+
}
15+
16+
@Override
17+
protected String[] getServletMappings() {
18+
return new String[]{"/example/*"};
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.enterprise.patterns.dispatcher.servlet.models;
2+
3+
import org.springframework.format.annotation.DateTimeFormat;
4+
5+
import java.util.Date;
6+
7+
public class Task {
8+
9+
private String description;
10+
11+
@DateTimeFormat(pattern = "yyyy-MM-dd'T'hh:mm")
12+
private Date due;
13+
14+
public Task() {
15+
}
16+
17+
public Task(Date due) {
18+
this.due = due;
19+
}
20+
21+
public Task(String description, Date due) {
22+
this.description = description;
23+
this.due = due;
24+
}
25+
26+
public String getDescription() {
27+
return description;
28+
}
29+
30+
public void setDescription(String description) {
31+
this.description = description;
32+
}
33+
34+
public Date getDue() {
35+
return due;
36+
}
37+
38+
public void setDue(Date due) {
39+
this.due = due;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.enterprise.patterns.dispatcher.servlet.web;
2+
3+
import org.springframework.core.annotation.AnnotationUtils;
4+
import org.springframework.web.bind.annotation.ControllerAdvice;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.ResponseStatus;
7+
import org.springframework.web.servlet.ModelAndView;
8+
9+
import javax.servlet.http.HttpServletRequest;
10+
11+
@ControllerAdvice
12+
public class GlobalDefaultExceptionHandler {
13+
@ExceptionHandler(Exception.class)
14+
public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) throws Exception {
15+
if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
16+
throw e;
17+
}
18+
ModelAndView modelAndView = new ModelAndView();
19+
modelAndView.addObject("exception", e);
20+
modelAndView.addObject("url", request.getRequestURL());
21+
modelAndView.setViewName("error");
22+
return modelAndView;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.enterprise.patterns.dispatcher.servlet.web;
2+
3+
import com.baeldung.enterprise.patterns.dispatcher.servlet.models.Task;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.stream.Collectors;
12+
13+
@Controller
14+
public class HomeController {
15+
@Autowired
16+
private Map<String, List<Task>> taskMap;
17+
18+
@GetMapping("/*")
19+
public String home(Model model) {
20+
List<String> users = taskMap.keySet().stream()
21+
.sorted()
22+
.collect(Collectors.toList());
23+
model.addAttribute("users", users);
24+
return "home";
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.enterprise.patterns.dispatcher.servlet.web;
2+
3+
import com.baeldung.enterprise.patterns.dispatcher.servlet.models.Task;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import java.util.ArrayList;
10+
import java.util.Date;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.stream.Collectors;
14+
15+
@Controller
16+
@RequestMapping("/tasks")
17+
public class TaskController {
18+
@Autowired
19+
private Map<String, List<Task>> taskMap;
20+
21+
@GetMapping("/{username}/list")
22+
public String listForm(
23+
Model model,
24+
@PathVariable("username") String username
25+
) {
26+
List<Task> tasks = taskMap.get(username).stream()
27+
.sorted((t1, t2) -> t1.getDue().compareTo(t2.getDue()))
28+
.collect(Collectors.toList());
29+
model.addAttribute("username", username);
30+
model.addAttribute("tasks", tasks);
31+
return "task-list";
32+
}
33+
34+
@GetMapping("/{username}/add")
35+
public String addForm(
36+
Model model,
37+
@PathVariable("username") String username
38+
) {
39+
model.addAttribute("username", username);
40+
model.addAttribute("task", new Task(new Date()));
41+
return "task-add";
42+
}
43+
44+
@PostMapping("/{username}/add")
45+
public String addSubmit(
46+
@PathVariable("username") String username,
47+
@ModelAttribute Task task
48+
) {
49+
List<Task> taskList = taskMap.get(username);
50+
if (taskList == null) {
51+
taskList = new ArrayList<>();
52+
}
53+
taskList.add(task);
54+
taskMap.put(username, taskList);
55+
return "redirect:list";
56+
}
57+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN">
3+
<Appenders>
4+
<Console name="Console" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}:%n[+] %msg%n"/>
6+
</Console>
7+
</Appenders>
8+
9+
<Loggers>
10+
<Root level="info">
11+
<AppenderRef ref="Console"/>
12+
</Root>
13+
</Loggers>
14+
</Configuration>

0 commit comments

Comments
 (0)