BAEL-86: Added localization and themes.
This commit is contained in:
committed by
slavisa-baeldung
parent
3808960bd9
commit
88a4f61917
82
spring-dispatcher-servlet/pom.xml
Normal file
82
spring-dispatcher-servlet/pom.xml
Normal file
@@ -0,0 +1,82 @@
|
||||
<?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>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</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.3.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf-spring4</artifactId>
|
||||
<version>3.0.2.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.21</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<version>2.7</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<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>
|
||||
<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.3.12.v20160915</version>
|
||||
<configuration>
|
||||
<webApp>
|
||||
<contextPath>/</contextPath>
|
||||
</webApp>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.spring.dispatcher.servlet;
|
||||
|
||||
import com.baeldung.spring.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,114 @@
|
||||
package com.baeldung.spring.dispatcher.servlet;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.ui.context.support.ResourceBundleThemeSource;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.ThemeResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.theme.CookieThemeResolver;
|
||||
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
|
||||
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;
|
||||
import java.util.Locale;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.enterprise.patterns.dispatcher.servlet.web")
|
||||
@EnableWebMvc
|
||||
public class WebConfiguration extends WebMvcConfigurerAdapter {
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry
|
||||
.addResourceHandler("/public/**")
|
||||
.addResourceLocations("/public/");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(localeChangeInterceptor());
|
||||
registry.addInterceptor(themeChangeInterceptor());
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("messages");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleResolver localeResolver() {
|
||||
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
|
||||
localeResolver.setDefaultLocale(Locale.ENGLISH);
|
||||
localeResolver.setCookieName("locale");
|
||||
localeResolver.setCookieMaxAge(-1);
|
||||
return localeResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleChangeInterceptor localeChangeInterceptor() {
|
||||
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
|
||||
localeChangeInterceptor.setParamName("lang");
|
||||
localeChangeInterceptor.setIgnoreInvalidLocale(true);
|
||||
return localeChangeInterceptor;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ResourceBundleThemeSource themeSource() {
|
||||
ResourceBundleThemeSource themeSource = new ResourceBundleThemeSource();
|
||||
themeSource.setBasenamePrefix("theme-");
|
||||
themeSource.setFallbackToSystemLocale(false);
|
||||
return themeSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ThemeResolver themeResolver() {
|
||||
CookieThemeResolver themeResolver = new CookieThemeResolver();
|
||||
themeResolver.setDefaultThemeName("robotask");
|
||||
themeResolver.setCookieName("theme");
|
||||
themeResolver.setCookieMaxAge(-1);
|
||||
return themeResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ThemeChangeInterceptor themeChangeInterceptor() {
|
||||
ThemeChangeInterceptor themeChangeInterceptor = new ThemeChangeInterceptor();
|
||||
themeChangeInterceptor.setParamName("theme");
|
||||
return themeChangeInterceptor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.spring.dispatcher.servlet;
|
||||
|
||||
import com.baeldung.spring.dispatcher.servlet.web.filters.RequestLoggingFilter;
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
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[]{"/*"};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Filter[] getServletFilters() {
|
||||
return new Filter[]{new RequestLoggingFilter()};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.spring.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.spring.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,28 @@
|
||||
package com.baeldung.spring.dispatcher.servlet.web;
|
||||
|
||||
import com.baeldung.spring.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 org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/")
|
||||
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,54 @@
|
||||
package com.baeldung.spring.dispatcher.servlet.web;
|
||||
|
||||
import com.baeldung.spring.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.*;
|
||||
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(Comparator.comparing(Task::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";
|
||||
}
|
||||
}
|
||||
14
spring-dispatcher-servlet/src/main/resources/log4j2.xml
Normal file
14
spring-dispatcher-servlet/src/main/resources/log4j2.xml
Normal file
@@ -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,10 @@
|
||||
home.title=Welcome to TaskTools!
|
||||
task.add.description=description
|
||||
task.add.due=due
|
||||
task.add.header=Adding a task to {0}''s list:
|
||||
task.add.submit=Submit
|
||||
task.add.title={0}: task add
|
||||
task.list.add-new=Add new
|
||||
task.list.header={0}''s tasks:
|
||||
task.list.home=Home
|
||||
task.list.title={0}: task list
|
||||
@@ -0,0 +1,10 @@
|
||||
home.title=Willkommen bei TaskTools!
|
||||
task.add.description=Beschreibung
|
||||
task.add.due=f\u00e4llig
|
||||
task.add.header=F\u00fcge eine Aufgabe zu {0}''s Liste hinzu:
|
||||
task.add.submit=Senden
|
||||
task.add.title={0}: Task hinzuf\u00fcgen
|
||||
task.list.add-new=Neuer Task
|
||||
task.list.header={0}''s Tasks:
|
||||
task.list.home=Startseite
|
||||
task.list.title={0}: Task Liste
|
||||
@@ -0,0 +1 @@
|
||||
stylesheet=/public/css/themes/post_it.css
|
||||
@@ -0,0 +1 @@
|
||||
stylesheet=/public/css/themes/robotask.css
|
||||
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Error</title>
|
||||
<link rel="stylesheet" type="text/css" th:href="${#themes.code('stylesheet')}">
|
||||
</head>
|
||||
<body>
|
||||
<h2>Error:</h2>
|
||||
<p th:text="|URL: ${url}|"></p>
|
||||
<p th:text="${exception}"></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title th:text="#{home.title}"></title>
|
||||
<link rel="stylesheet" type="text/css" th:href="${#themes.code('stylesheet')}">
|
||||
</head>
|
||||
<body>
|
||||
<h2>TaskTools</h2>
|
||||
<ul>
|
||||
<li th:each="username : ${users}">
|
||||
<a th:href="@{/tasks/{username}/list(username=${username})}" th:text="${username}"></a>
|
||||
</li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title th:text="#{task.add.title(${username})}"></title>
|
||||
<link rel="stylesheet" type="text/css" th:href="${#themes.code('stylesheet')}">
|
||||
</head>
|
||||
<body>
|
||||
<h2 th:text="#{task.add.header(${username})}"></h2>
|
||||
<form method="post" action="#" th:action="@{/tasks/{username}/add(username=${username})}" th:object="${task}">
|
||||
<input type="text" th:field="*{description}" data-th-placeholder="#{task.add.description}"/>
|
||||
<input type="datetime-local" th:field="*{due}" data-th-placeholder="#{task.add.due}"/>
|
||||
<input type="submit" data-th-value="#{task.add.submit}"/>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title th:text="#{task.list.title(${username})}"></title>
|
||||
<link rel="stylesheet" type="text/css" th:href="${#themes.code('stylesheet')}">
|
||||
</head>
|
||||
<body>
|
||||
<h2 th:text="#{task.list.header(${username})}"></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="@{/}" th:text="#{task.list.home}"></a>
|
||||
<a th:href="@{/tasks/{username}/add(username=${username})}" th:text="#{task.list.add-new}"></a>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,8 @@
|
||||
@import url('https://fonts.googleapis.com/css?family=Indie+Flower');
|
||||
|
||||
* {
|
||||
font-family: 'Indie Flower', sans-serif;
|
||||
font-size: 18px;
|
||||
color: #ffeb3b;
|
||||
background-color: #212121;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
@import url('https://fonts.googleapis.com/css?family=Roboto');
|
||||
|
||||
* {
|
||||
font-family: Roboto, sans-serif;
|
||||
font-size: 1em;
|
||||
color: #212121;
|
||||
background-color: #fafafa;
|
||||
}
|
||||
Reference in New Issue
Block a user