Skip to content

Instantly share code, notes, and snippets.

@lpand

lpand/WebConfig.java

Last active Jan 28, 2016
Embed
What would you like to do?
package com.italtel.newpm.fe.config;
import java.text.SimpleDateFormat;
import java.util.List;
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.data.web.config.EnableSpringDataWebSupport;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.validation.Validator;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
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.templateresolver.ServletContextTemplateResolver;
import com.italtel.newpm.definitions.model.NewpmConfig;
import com.italtel.newpm.definitions.model.NewpmOssConfig;
import com.italtel.newpm.fe.annotation.Ne;
import com.italtel.newpm.fe.annotation.Oss;
import com.italtel.newpm.fe.config.web.AuthorizationWebConfig;
import com.italtel.newpm.fe.db.ConfigRepository;
import com.italtel.newpm.fe.db.LogicalAggregation;
import com.italtel.newpm.fe.db.OssConfigRepository;
import com.italtel.newpm.fe.web.ConfigViewHelper;
import com.italtel.newpm.fe.web.controllers.ConfigControllerImpl;
import com.italtel.newpm.fe.web.controllers.NeConfigController;
import com.italtel.newpm.fe.web.validation.NeValidator;
import com.italtel.newpm.fe.web.validation.OssConfigValidator;
import com.italtel.newpm.fe.web.view.ViewHelper;
import nz.net.ultraq.thymeleaf.LayoutDialect;
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
@ComponentScan(
basePackageClasses = { AuthorizationWebConfig.class, NeConfigController.class })
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
// "Thymeleaf template resolver serving HTML 5"
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCacheable(false);
return templateResolver;
}
@Bean
// "Thymeleaf template engine with Spring integration"
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.addDialect(new LayoutDialect());
return templateEngine;
}
@Bean
// "Thymeleaf view resolver"
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename("messages/messages");
return source;
}
@Bean @Ne Validator neValidator(ConfigRepository repo) {
return new NeValidator(repo);
}
@Bean @Oss Validator ossConfigValidator(OssConfigRepository repo) {
return new OssConfigValidator(repo);
}
@Bean public ConfigControllerImpl<NewpmConfig>
configControllerImpl(MessageSource messages, @Ne LogicalAggregation<NewpmConfig> config) {
return new ConfigControllerImpl<NewpmConfig>(
config,
ConfigViewHelper<NewpmConfig>::new,
messages,
ViewHelper.ne()
);
}
@Bean public ConfigControllerImpl<NewpmOssConfig>
ossConfigControllerImpl(MessageSource messages, @Oss LogicalAggregation<NewpmOssConfig> oss) {
return new ConfigControllerImpl<>(
oss,
ConfigViewHelper<NewpmOssConfig>::new,
messages,
ViewHelper.oss()
);
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd-MM-yyyy"));
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
// converters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()));
}
// Tell Spring to let requests for static content be handled by the
// container's default servlet.
@Override
public void
configureDefaultServletHandling(DefaultServletHandlerConfigurer config) {
config.enable();
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer config) {
config.favorPathExtension(true)
.ignoreAcceptHeader(true)
.useJaf(false)
.defaultContentType(MediaType.TEXT_HTML)
.mediaType("html", MediaType.TEXT_HTML)
.mediaType("json", MediaType.APPLICATION_JSON);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.