diff options
author | Alexander Alexeev | 2016-11-23 16:19:26 +0700 |
---|---|---|
committer | Vitaly Takmazov | 2016-11-23 13:03:09 +0300 |
commit | e7fd58f1023ce9e1c9ab880bf27c5c0b2a3cffca (patch) | |
tree | 5ce36019dffc5f6eb4f71756a9f91068d5efbd54 /juick-spring-www/src/main/java/com | |
parent | 37a171892ba11c578ea947103ee7488801785eac (diff) |
thymeleaf layout plugin used
Diffstat (limited to 'juick-spring-www/src/main/java/com')
7 files changed, 136 insertions, 31 deletions
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<Date> { + @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; + } +} |