Kotlin idioms (#2534)

This commit is contained in:
Grzegorz Piwowarek 2017-08-31 15:03:02 +02:00 committed by GitHub
parent 49acba206c
commit d1a1322bcc
2 changed files with 12 additions and 21 deletions

View File

@ -12,13 +12,9 @@ import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver
import org.thymeleaf.spring4.view.ThymeleafViewResolver import org.thymeleaf.spring4.view.ThymeleafViewResolver
import org.thymeleaf.templatemode.TemplateMode import org.thymeleaf.templatemode.TemplateMode
@EnableWebMvc @EnableWebMvc
@Configuration @Configuration
open class ApplicationWebConfig: WebMvcConfigurerAdapter(), ApplicationContextAware { open class ApplicationWebConfig : WebMvcConfigurerAdapter(), ApplicationContextAware {
private var applicationContext: ApplicationContext? = null private var applicationContext: ApplicationContext? = null
@ -34,27 +30,23 @@ open class ApplicationWebConfig: WebMvcConfigurerAdapter(), ApplicationContextAw
@Bean @Bean
open fun templateResolver(): SpringResourceTemplateResolver { open fun templateResolver(): SpringResourceTemplateResolver {
val templateResolver = SpringResourceTemplateResolver() return SpringResourceTemplateResolver()
templateResolver.prefix = "/WEB-INF/view/" .apply { prefix = "/WEB-INF/view/" }
templateResolver.suffix = ".html" .apply { suffix = ".html"}
templateResolver.templateMode = TemplateMode.HTML .apply { templateMode = TemplateMode.HTML }
templateResolver.setApplicationContext(this.applicationContext); .apply { setApplicationContext(applicationContext) }
return templateResolver
} }
@Bean @Bean
open fun templateEngine(): SpringTemplateEngine { open fun templateEngine(): SpringTemplateEngine {
val templateEngine = SpringTemplateEngine() return SpringTemplateEngine()
templateEngine.setTemplateResolver(templateResolver()) .apply { setTemplateResolver(templateResolver()) }
return templateEngine
} }
@Bean @Bean
open fun viewResolver(): ThymeleafViewResolver { open fun viewResolver(): ThymeleafViewResolver {
val viewResolver = ThymeleafViewResolver() return ThymeleafViewResolver()
viewResolver.templateEngine = templateEngine() .apply { templateEngine = templateEngine() }
viewResolver.order = 1 .apply { order = 1 }
return viewResolver
} }
} }

View File

@ -1,9 +1,8 @@
package com.baeldung.kotlin.mvc package com.baeldung.kotlin.mvc
import com.baeldung.kotlin.mvc.ApplicationWebConfig
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer
class ApplicationWebInitializer: AbstractAnnotationConfigDispatcherServletInitializer() { class ApplicationWebInitializer : AbstractAnnotationConfigDispatcherServletInitializer() {
override fun getRootConfigClasses(): Array<Class<*>>? { override fun getRootConfigClasses(): Array<Class<*>>? {
return null return null