Moved from XML config to Java config
This commit is contained in:
parent
b6ba166e72
commit
02740211b0
@ -0,0 +1,36 @@
|
||||
package org.baeldung.thymeleaf.config;
|
||||
|
||||
import javax.servlet.ServletRegistration.Dynamic;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
/**
|
||||
* Java configuration file that is used for web application initialization
|
||||
*/
|
||||
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
public WebApp() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class<?>[] { WebMVCConfig.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customizeRegistration(final Dynamic registration) {
|
||||
super.customizeRegistration(registration);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package org.baeldung.thymeleaf.config;
|
||||
|
||||
import org.baeldung.thymeleaf.formatter.NameFormatter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Description;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.format.FormatterRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan({ "org.baeldung.thymeleaf" })
|
||||
/**
|
||||
* Java configuration file that is used for Spring MVC and Thymeleaf
|
||||
* configurations
|
||||
*/
|
||||
public class WebMVCConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Bean
|
||||
@Description("Thymeleaf Template Resolver")
|
||||
public ServletContextTemplateResolver templateResolver() {
|
||||
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
|
||||
templateResolver.setPrefix("/WEB-INF/views/");
|
||||
templateResolver.setSuffix(".html");
|
||||
templateResolver.setTemplateMode("HTML5");
|
||||
|
||||
return templateResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Description("Thymeleaf Template Engine")
|
||||
public SpringTemplateEngine templateEngine() {
|
||||
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
||||
templateEngine.setTemplateResolver(templateResolver());
|
||||
|
||||
return templateEngine;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Description("Thymeleaf View Resolver")
|
||||
public ThymeleafViewResolver viewResolver() {
|
||||
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
||||
viewResolver.setTemplateEngine(templateEngine());
|
||||
viewResolver.setOrder(1);
|
||||
return viewResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Description("Spring Message Resolver")
|
||||
public ResourceBundleMessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("messages");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Description("Custom Conversion Service")
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
registry.addFormatter(new NameFormatter());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user