BAEL-86: Working sample.
This commit is contained in:
parent
c9073cbfa5
commit
04f8eb55a8
|
@ -0,0 +1,35 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.enterprise.patterns</groupId>
|
||||
<artifactId>enterprise-patterns-parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>spring-dispatcher-servlet</module>
|
||||
</modules>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,73 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-dispatcher-servlet</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.enterprise.patterns</groupId>
|
||||
<artifactId>enterprise-patterns-parent</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>4.3.2.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf-spring4</artifactId>
|
||||
<version>3.0.1.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<version>2.6.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.6.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<version>9.4.0.M1</version>
|
||||
<configuration>
|
||||
<webApp>
|
||||
<contextPath>/</contextPath>
|
||||
</webApp>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.enterprise.patterns.dispatcher.servlet;
|
||||
|
||||
import com.baeldung.enterprise.patterns.dispatcher.servlet.models.Task;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Configuration
|
||||
public class RootConfiguration {
|
||||
@Bean
|
||||
public Map<String, List<Task>> taskList() {
|
||||
Map<String, List<Task>> taskMap = new HashMap<>();
|
||||
List<Task> taskList = new ArrayList<>();
|
||||
taskList.add(new Task("Clean the dishes!", new Date()));
|
||||
taskMap.put("Cid", taskList);
|
||||
return taskMap;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.enterprise.patterns.dispatcher.servlet;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||
import org.thymeleaf.templatemode.TemplateMode;
|
||||
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.enterprise.patterns.dispatcher.servlet.web")
|
||||
@EnableWebMvc
|
||||
public class WebConfiguration extends WebMvcConfigurerAdapter {
|
||||
@Bean
|
||||
public ServletContextTemplateResolver templateResolver(ServletContext servletContext) {
|
||||
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(servletContext);
|
||||
templateResolver.setPrefix("/WEB-INF/views/");
|
||||
templateResolver.setSuffix(".html");
|
||||
templateResolver.setTemplateMode(TemplateMode.HTML);
|
||||
return templateResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SpringTemplateEngine templateEngine(ServletContextTemplateResolver templateResolver) {
|
||||
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
||||
templateEngine.setTemplateResolver(templateResolver);
|
||||
return templateEngine;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ThymeleafViewResolver viewResolver(SpringTemplateEngine templateEngine) {
|
||||
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
||||
viewResolver.setTemplateEngine(templateEngine);
|
||||
return viewResolver;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.enterprise.patterns.dispatcher.servlet;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
@Override
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return new Class<?>[]{RootConfiguration.class};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class<?>[]{WebConfiguration.class};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[]{"/example/*"};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.enterprise.patterns.dispatcher.servlet.models;
|
||||
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Task {
|
||||
|
||||
private String description;
|
||||
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd'T'hh:mm")
|
||||
private Date due;
|
||||
|
||||
public Task() {
|
||||
}
|
||||
|
||||
public Task(Date due) {
|
||||
this.due = due;
|
||||
}
|
||||
|
||||
public Task(String description, Date due) {
|
||||
this.description = description;
|
||||
this.due = due;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Date getDue() {
|
||||
return due;
|
||||
}
|
||||
|
||||
public void setDue(Date due) {
|
||||
this.due = due;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.enterprise.patterns.dispatcher.servlet.web;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@ControllerAdvice
|
||||
public class GlobalDefaultExceptionHandler {
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) throws Exception {
|
||||
if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
|
||||
throw e;
|
||||
}
|
||||
ModelAndView modelAndView = new ModelAndView();
|
||||
modelAndView.addObject("exception", e);
|
||||
modelAndView.addObject("url", request.getRequestURL());
|
||||
modelAndView.setViewName("error");
|
||||
return modelAndView;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.enterprise.patterns.dispatcher.servlet.web;
|
||||
|
||||
import com.baeldung.enterprise.patterns.dispatcher.servlet.models.Task;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
public class HomeController {
|
||||
@Autowired
|
||||
private Map<String, List<Task>> taskMap;
|
||||
|
||||
@GetMapping("/*")
|
||||
public String home(Model model) {
|
||||
List<String> users = taskMap.keySet().stream()
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
model.addAttribute("users", users);
|
||||
return "home";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.enterprise.patterns.dispatcher.servlet.web;
|
||||
|
||||
import com.baeldung.enterprise.patterns.dispatcher.servlet.models.Task;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/tasks")
|
||||
public class TaskController {
|
||||
@Autowired
|
||||
private Map<String, List<Task>> taskMap;
|
||||
|
||||
@GetMapping("/{username}/list")
|
||||
public String listForm(
|
||||
Model model,
|
||||
@PathVariable("username") String username
|
||||
) {
|
||||
List<Task> tasks = taskMap.get(username).stream()
|
||||
.sorted((t1, t2) -> t1.getDue().compareTo(t2.getDue()))
|
||||
.collect(Collectors.toList());
|
||||
model.addAttribute("username", username);
|
||||
model.addAttribute("tasks", tasks);
|
||||
return "task-list";
|
||||
}
|
||||
|
||||
@GetMapping("/{username}/add")
|
||||
public String addForm(
|
||||
Model model,
|
||||
@PathVariable("username") String username
|
||||
) {
|
||||
model.addAttribute("username", username);
|
||||
model.addAttribute("task", new Task(new Date()));
|
||||
return "task-add";
|
||||
}
|
||||
|
||||
@PostMapping("/{username}/add")
|
||||
public String addSubmit(
|
||||
@PathVariable("username") String username,
|
||||
@ModelAttribute Task task
|
||||
) {
|
||||
List<Task> taskList = taskMap.get(username);
|
||||
if (taskList == null) {
|
||||
taskList = new ArrayList<>();
|
||||
}
|
||||
taskList.add(task);
|
||||
taskMap.put(username, taskList);
|
||||
return "redirect:list";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}:%n[+] %msg%n"/>
|
||||
</Console>
|
||||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="Console"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Error</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Error:</h2>
|
||||
<p th:text="|URL: ${url}|"></p>
|
||||
<p th:text="${exception}"></p>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,14 @@
|
|||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Welcome to TaskTools!</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>TaskTools</h2>
|
||||
<ul>
|
||||
<li th:each="username : ${users}">
|
||||
<a th:href="@{/example/tasks/{username}/list(username=${username})}" th:text="${username}"></a>
|
||||
</li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,14 @@
|
|||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title th:text="|${username}: task add|"></title>
|
||||
</head>
|
||||
<body>
|
||||
<h2 th:text="|Adding a task to ${username}'s list:|"></h2>
|
||||
<form method="post" action="#" th:action="@{/example/tasks/{username}/add(username=${username})}" th:object="${task}">
|
||||
<input type="text" th:field="*{description}" placeholder="description"/>
|
||||
<input type="datetime-local" th:field="*{due}" placeholder="due"/>
|
||||
<input type="submit" value="Submit"/>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,19 @@
|
|||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title th:text="|${username}: task list|"></title>
|
||||
</head>
|
||||
<body>
|
||||
<h2 th:text="|${username}'s tasks:|"></h2>
|
||||
<ul>
|
||||
<li th:each="task : ${tasks}">
|
||||
<p th:text="*{task.due}"></p>
|
||||
<p th:text="*{task.description}"></p>
|
||||
</li>
|
||||
</ul>
|
||||
<p>
|
||||
<a th:href="@{/example}">Home</a>
|
||||
<a th:href="@{/example/tasks/{username}/add(username=${username})}">Add new</a>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue