diff --git a/spring-mvc-kotlin/pom.xml b/spring-mvc-kotlin/pom.xml index 5375ecae7c..264cf49817 100644 --- a/spring-mvc-kotlin/pom.xml +++ b/spring-mvc-kotlin/pom.xml @@ -35,9 +35,15 @@ 4.3.10.RELEASE - javax.servlet - jstl - 1.2 + org.thymeleaf + thymeleaf + 3.0.7.RELEASE + + + + org.thymeleaf + thymeleaf-spring4 + 3.0.7.RELEASE diff --git a/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/mvc/ApplicationWebConfig.kt b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/mvc/ApplicationWebConfig.kt index 4907e46efb..ec1c4e9511 100644 --- a/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/mvc/ApplicationWebConfig.kt +++ b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/mvc/ApplicationWebConfig.kt @@ -1,13 +1,16 @@ package com.baeldung.kotlin.mvc +import org.springframework.context.ApplicationContext +import org.springframework.context.ApplicationContextAware import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration -import org.springframework.web.servlet.ViewResolver import org.springframework.web.servlet.config.annotation.EnableWebMvc import org.springframework.web.servlet.config.annotation.ViewControllerRegistry import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter -import org.springframework.web.servlet.view.InternalResourceViewResolver -import org.springframework.web.servlet.view.JstlView +import org.thymeleaf.spring4.SpringTemplateEngine +import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver +import org.thymeleaf.spring4.view.ThymeleafViewResolver +import org.thymeleaf.templatemode.TemplateMode @@ -15,7 +18,13 @@ import org.springframework.web.servlet.view.JstlView @EnableWebMvc @Configuration -open class ApplicationWebConfig: WebMvcConfigurerAdapter() { +open class ApplicationWebConfig: WebMvcConfigurerAdapter(), ApplicationContextAware { + + private var applicationContext: ApplicationContext? = null + + override fun setApplicationContext(applicationContext: ApplicationContext?) { + this.applicationContext = applicationContext + } override fun addViewControllers(registry: ViewControllerRegistry?) { super.addViewControllers(registry) @@ -24,14 +33,28 @@ open class ApplicationWebConfig: WebMvcConfigurerAdapter() { } @Bean - open fun viewResolver(): ViewResolver { - val bean = InternalResourceViewResolver() + open fun templateResolver(): SpringResourceTemplateResolver { + val templateResolver = SpringResourceTemplateResolver() + templateResolver.prefix = "/WEB-INF/view/" + templateResolver.suffix = ".html" + templateResolver.templateMode = TemplateMode.HTML + templateResolver.setApplicationContext(this.applicationContext); + return templateResolver + } - bean.setViewClass(JstlView::class.java) - bean.setPrefix("/WEB-INF/view/") - bean.setSuffix(".jsp") + @Bean + open fun templateEngine(): SpringTemplateEngine { + val templateEngine = SpringTemplateEngine() + templateEngine.setTemplateResolver(templateResolver()) + return templateEngine + } - return bean + @Bean + open fun viewResolver(): ThymeleafViewResolver { + val viewResolver = ThymeleafViewResolver() + viewResolver.templateEngine = templateEngine() + viewResolver.order = 1 + return viewResolver } } \ No newline at end of file diff --git a/spring-mvc-kotlin/src/main/webapp/WEB-INF/spring-web-config.xml b/spring-mvc-kotlin/src/main/webapp/WEB-INF/spring-web-config.xml index ffe83a66fa..c7f110ea94 100644 --- a/spring-mvc-kotlin/src/main/webapp/WEB-INF/spring-web-config.xml +++ b/spring-mvc-kotlin/src/main/webapp/WEB-INF/spring-web-config.xml @@ -12,13 +12,26 @@ - - - - - + + + + + + + + + + + + + + + + \ No newline at end of file