BAEL-86: Added localization and themes.

This commit is contained in:
Christian Rädel 2016-10-18 17:04:02 +02:00 committed by slavisa-baeldung
parent 3808960bd9
commit 88a4f61917
23 changed files with 244 additions and 113 deletions

View File

@ -1,41 +0,0 @@
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;
}
}

View File

@ -1,14 +0,0 @@
<!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>

View File

@ -1,14 +0,0 @@
<!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>

View File

@ -1,19 +0,0 @@
<!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>

View File

@ -5,12 +5,11 @@
<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>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
@ -25,22 +24,27 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.1.RELEASE</version>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.6.2</version>
<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.6.2</version>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
@ -49,6 +53,11 @@
<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>
@ -61,7 +70,7 @@
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.0.M1</version>
<version>9.3.12.v20160915</version>
<configuration>
<webApp>
<contextPath>/</contextPath>

View File

@ -1,6 +1,6 @@
package com.baeldung.enterprise.patterns.dispatcher.servlet;
package com.baeldung.spring.dispatcher.servlet;
import com.baeldung.enterprise.patterns.dispatcher.servlet.models.Task;
import com.baeldung.spring.dispatcher.servlet.models.Task;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

View File

@ -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;
}
}

View File

@ -1,7 +1,10 @@
package com.baeldung.enterprise.patterns.dispatcher.servlet;
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() {
@ -15,6 +18,11 @@ public class WebInitializer extends AbstractAnnotationConfigDispatcherServletIni
@Override
protected String[] getServletMappings() {
return new String[]{"/example/*"};
return new String[]{"/*"};
}
@Override
protected Filter[] getServletFilters() {
return new Filter[]{new RequestLoggingFilter()};
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.enterprise.patterns.dispatcher.servlet.models;
package com.baeldung.spring.dispatcher.servlet.models;
import org.springframework.format.annotation.DateTimeFormat;

View File

@ -1,4 +1,4 @@
package com.baeldung.enterprise.patterns.dispatcher.servlet.web;
package com.baeldung.spring.dispatcher.servlet.web;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;

View File

@ -1,16 +1,18 @@
package com.baeldung.enterprise.patterns.dispatcher.servlet.web;
package com.baeldung.spring.dispatcher.servlet.web;
import com.baeldung.enterprise.patterns.dispatcher.servlet.models.Task;
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;

View File

@ -1,15 +1,12 @@
package com.baeldung.enterprise.patterns.dispatcher.servlet.web;
package com.baeldung.spring.dispatcher.servlet.web;
import com.baeldung.enterprise.patterns.dispatcher.servlet.models.Task;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
@Controller
@ -24,7 +21,7 @@ public class TaskController {
@PathVariable("username") String username
) {
List<Task> tasks = taskMap.get(username).stream()
.sorted((t1, t2) -> t1.getDue().compareTo(t2.getDue()))
.sorted(Comparator.comparing(Task::getDue))
.collect(Collectors.toList());
model.addAttribute("username", username);
model.addAttribute("tasks", tasks);

View File

@ -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

View File

@ -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

View File

@ -0,0 +1 @@
stylesheet=/public/css/themes/post_it.css

View File

@ -0,0 +1 @@
stylesheet=/public/css/themes/robotask.css

View File

@ -2,6 +2,7 @@
<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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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;
}

View File

@ -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;
}