From e7fd58f1023ce9e1c9ab880bf27c5c0b2a3cffca Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Wed, 23 Nov 2016 16:19:26 +0700 Subject: thymeleaf layout plugin used --- juick-spring-www/build.gradle | 1 + .../www/configuration/WebAppConfiguration.java | 37 +++++++++++++ .../juick/www/configuration/WwwInitializer.java | 3 +- .../www/configuration/WwwServletConfiguration.java | 29 +++++----- .../www/controllers/DivideByZeroController.java | 16 ------ .../www/controllers/ShowMessageController.java | 4 +- .../com/juick/www/formatter/DateFormatter.java | 43 +++++++++++++++ .../juick/www/settings/TemplateSettingsHolder.java | 35 ++++++++++++ .../src/main/resources/messages.properties | 11 +++- .../src/main/resources/messages_en.properties | 11 +++- .../webapp/WEB-INF/templates/divideByZero.html | 11 ---- .../src/main/webapp/WEB-INF/templates/index.html | 12 +++-- .../webapp/WEB-INF/templates/layout/footer.html | 3 -- .../WEB-INF/templates/layout/mainLayout.html | 63 ++++++++++++++++++++-- .../webapp/WEB-INF/templates/layout/sponsors.html | 8 +++ .../webapp/WEB-INF/templates/postNotFound.html | 27 +++++----- .../webapp/WEB-INF/templates/userNotFound.html | 28 +++++----- 17 files changed, 259 insertions(+), 83 deletions(-) create mode 100644 juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java delete mode 100644 juick-spring-www/src/main/java/com/juick/www/controllers/DivideByZeroController.java create mode 100644 juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java create mode 100644 juick-spring-www/src/main/java/com/juick/www/settings/TemplateSettingsHolder.java delete mode 100644 juick-spring-www/src/main/webapp/WEB-INF/templates/divideByZero.html delete mode 100644 juick-spring-www/src/main/webapp/WEB-INF/templates/layout/footer.html create mode 100644 juick-spring-www/src/main/webapp/WEB-INF/templates/layout/sponsors.html (limited to 'juick-spring-www') diff --git a/juick-spring-www/build.gradle b/juick-spring-www/build.gradle index f48f9d17..e405506b 100644 --- a/juick-spring-www/build.gradle +++ b/juick-spring-www/build.gradle @@ -30,6 +30,7 @@ dependencies { compile "org.thymeleaf:thymeleaf:${thymeleafVersion}" compile "org.thymeleaf:thymeleaf-spring4:${thymeleafVersion}" compile "org.thymeleaf.extras:thymeleaf-extras-java8time:3.0.0.RELEASE" + compile "nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:2.1.1" compile "org.springframework.security:spring-security-web:${springSecurityVersion}" compile "org.springframework.security:spring-security-config:${springSecurityVersion}" testRuntime "org.thymeleaf:thymeleaf-testing:3.0.1.RELEASE" diff --git a/juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java b/juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java new file mode 100644 index 00000000..5fb7848d --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java @@ -0,0 +1,37 @@ +package com.juick.www.configuration; + +import com.juick.www.settings.TemplateSettingsHolder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.ResourceBundleMessageSource; +import org.springframework.core.env.Environment; + +import javax.annotation.Resource; + +/** + * Created by aalexeev on 11/22/16. + */ +@Configuration +@PropertySource("classpath:juick.conf") +public class WebAppConfiguration { + @Resource + private Environment env; + + @Bean + public ResourceBundleMessageSource messageSource() { + ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); + + messageSource.setBasenames("messages", "errors"); + messageSource.setDefaultEncoding("UTF-8"); + messageSource.setFallbackToSystemLocale(false); + messageSource.setUseCodeAsDefaultMessage(true); + + return messageSource; + } + + @Bean + public TemplateSettingsHolder settingsHolder() { + return new TemplateSettingsHolder(env); + } +} diff --git a/juick-spring-www/src/main/java/com/juick/www/configuration/WwwInitializer.java b/juick-spring-www/src/main/java/com/juick/www/configuration/WwwInitializer.java index 6abb9f79..d5a3bbef 100644 --- a/juick-spring-www/src/main/java/com/juick/www/configuration/WwwInitializer.java +++ b/juick-spring-www/src/main/java/com/juick/www/configuration/WwwInitializer.java @@ -16,7 +16,8 @@ public class WwwInitializer extends AbstractAnnotationConfigDispatcherServletIni @Override protected Class[] getRootConfigClasses() { - return new Class[]{DataConfiguration.class, SearchConfiguration.class, WebSecurityConfig.class}; + return new Class[]{ + WebAppConfiguration.class, DataConfiguration.class, SearchConfiguration.class, WebSecurityConfig.class}; } @Override diff --git a/juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java b/juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java index 869fd651..089e43a8 100644 --- a/juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java +++ b/juick-spring-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java @@ -2,11 +2,13 @@ package com.juick.www.configuration; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.juick.www.formatter.DateFormatter; +import nz.net.ultraq.thymeleaf.LayoutDialect; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; -import org.springframework.context.support.ResourceBundleMessageSource; +import org.springframework.format.FormatterRegistry; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; @@ -57,6 +59,9 @@ public class WwwServletConfiguration extends WebMvcConfigurationSupport { // across different data types, so this flag is "false" by default // for safer backwards compatibility. templateEngine.setEnableSpringELCompiler(true); + // Thymeleaf Layout Dialect + templateEngine.addDialect(new LayoutDialect()); + return templateEngine; } @@ -93,17 +98,7 @@ public class WwwServletConfiguration extends WebMvcConfigurationSupport { .addResourceLocations("/") .resourceChain(true) .addResolver(new PathResourceResolver()); - } - - @Bean - public ResourceBundleMessageSource messageSource() { - ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); - - messageSource.setBasenames("messages", "errors"); - messageSource.setDefaultEncoding("UTF-8"); - messageSource.setFallbackToSystemLocale(false); - - return messageSource; + registry.addResourceHandler("/static/**").addResourceLocations("/static/"); } @Override @@ -114,4 +109,14 @@ public class WwwServletConfiguration extends WebMvcConfigurationSupport { return result; } + + @Override + public void addFormatters(final FormatterRegistry registry) { + registry.addFormatter(dateFormatter()); + } + + @Bean + public DateFormatter dateFormatter() { + return new DateFormatter(); + } } diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/DivideByZeroController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/DivideByZeroController.java deleted file mode 100644 index 07266adc..00000000 --- a/juick-spring-www/src/main/java/com/juick/www/controllers/DivideByZeroController.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.juick.www.controllers; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; - -/** - * Created by aalexeev on 11/21/16. - */ -@Controller -public class DivideByZeroController { - - @RequestMapping("/divideByZero") - public String getPage() { - return "divideByZero"; - } -} diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java index b8c25135..59ab52c2 100644 --- a/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java @@ -55,10 +55,10 @@ public class ShowMessageController { if (isPostNumber && anything.equals(Integer.toString(messageId))) { if (messageId > 0) { com.juick.User author = messagesService.getMessageAuthor(messageId); + if (author != null) return "redirect:/" + author.getName() + "/" + anything; - } else if (messageId == 0) - return "forward:/divideByZero.html"; + } model.addAttribute("messageId", anything); diff --git a/juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java b/juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java new file mode 100644 index 00000000..74596fc5 --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/formatter/DateFormatter.java @@ -0,0 +1,43 @@ +package com.juick.www.formatter; + +import org.springframework.context.MessageSource; +import org.springframework.format.Formatter; + +import javax.annotation.Resource; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +/** + * Created by aalexeev on 11/22/16. + */ +public class DateFormatter implements Formatter { + @Resource + private MessageSource messageSource; + + + public DateFormatter() { + super(); + } + + @Override + public Date parse(final String text, final Locale locale) throws ParseException { + final SimpleDateFormat dateFormat = createDateFormat(locale); + return dateFormat.parse(text); + } + + @Override + public String print(final Date object, final Locale locale) { + final SimpleDateFormat dateFormat = createDateFormat(locale); + return dateFormat.format(object); + } + + private SimpleDateFormat createDateFormat(final Locale locale) { + final String format = this.messageSource.getMessage("date.format", null, locale); + final SimpleDateFormat dateFormat = new SimpleDateFormat(format); + dateFormat.setLenient(false); + return dateFormat; + } + +} diff --git a/juick-spring-www/src/main/java/com/juick/www/settings/TemplateSettingsHolder.java b/juick-spring-www/src/main/java/com/juick/www/settings/TemplateSettingsHolder.java new file mode 100644 index 00000000..c6df73da --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/settings/TemplateSettingsHolder.java @@ -0,0 +1,35 @@ +package com.juick.www.settings; + +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.springframework.core.env.Environment; + +/** + * Created by aalexeev on 11/22/16. + */ +public class TemplateSettingsHolder { + private final boolean showSponsors; + private final boolean showSape; + + + public TemplateSettingsHolder(Environment settingsEnv) { + showSponsors = BooleanUtils.toBoolean(settingsEnv.getProperty("template.showSponsors", "false")); + showSape = BooleanUtils.toBoolean(settingsEnv.getProperty("template.showSape", "true")); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("showSponsors", showSponsors) + .append("showSape", showSape) + .toString(); + } + + public boolean isShowSponsors() { + return showSponsors; + } + + public boolean isShowSape() { + return showSape; + } +} diff --git a/juick-spring-www/src/main/resources/messages.properties b/juick-spring-www/src/main/resources/messages.properties index 9749ddef..29715d5d 100644 --- a/juick-spring-www/src/main/resources/messages.properties +++ b/juick-spring-www/src/main/resources/messages.properties @@ -1 +1,10 @@ -return.toMain=Вернуться на главную \ No newline at end of file +date.format=dd.MM.yyyy + +link.settings = Настройки +link.returnToMain =Вернуться на главную +link.contacts=Контакты +link.help=Помощь +link.adv=Реклама + +label.sponsor=Спонсор +label.sponsors=Спонсоры \ No newline at end of file diff --git a/juick-spring-www/src/main/resources/messages_en.properties b/juick-spring-www/src/main/resources/messages_en.properties index 67fbb7d7..9efa2cbb 100644 --- a/juick-spring-www/src/main/resources/messages_en.properties +++ b/juick-spring-www/src/main/resources/messages_en.properties @@ -1 +1,10 @@ -return.toMain=Return to main \ No newline at end of file +date.format=MM/dd/yyyy + +link.settings=Settings +link.returnToMain=Return to home +link.contacts=Contacts +link.help=Help +link.adv=Advertisement + +label.sponsor=Sponsor +label.sponsors=Sponsors \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/divideByZero.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/divideByZero.html deleted file mode 100644 index 7e03d74a..00000000 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/divideByZero.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - Title - - -00000000000000000000000000000000000000000000000000000000000000000000000 - - - \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/index.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/index.html index a5c5b348..5687985d 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/index.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/index.html @@ -1,11 +1,15 @@ - + - - Title + Главная страница + -

Index page

+
+

Главная страница !

+
\ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/footer.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/footer.html deleted file mode 100644 index 1182e61d..00000000 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/footer.html +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/mainLayout.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/mainLayout.html index 659a3fb0..34891b45 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/mainLayout.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/mainLayout.html @@ -1,12 +1,65 @@ - + - - - - + + + + + + + + + Layout title + + + + +
+

Page content goes here

+
+ + \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/sponsors.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/sponsors.html new file mode 100644 index 00000000..6c68a867 --- /dev/null +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/sponsors.html @@ -0,0 +1,8 @@ + + + +
+ Спонсоры: +
+ + \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/postNotFound.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/postNotFound.html index c8712c9f..4162aa8b 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/postNotFound.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/postNotFound.html @@ -1,22 +1,23 @@ - + - Страница не найдена - - -
-

Страница не найдена

-
+
+
+

Страница не найдена

+
-
-

Похоже, пользователь удалил страницу, возможна она и не была создана.

-
-
- Вернуться на главную -
+
+

Похоже, пользователь удалил страницу, возможна она и не была создана.

+
+
+ Вернуться на главную +
+
\ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/userNotFound.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/userNotFound.html index 300b7730..de1a97a0 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/userNotFound.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/userNotFound.html @@ -1,22 +1,22 @@ - + - Пользователь не найден - - +
+
+

Пользователь не найден

+
-
-

Пользователь не найден

-
- -
-

Пользователь не найден.

-
-
- Вернуться на главную -
+
+

Пользователь не найден.

+
+
+ Вернуться на главную +
+
\ No newline at end of file -- cgit v1.2.3