From d492c6f4d819705fcb6df71b2a57b7fd04524c2c Mon Sep 17 00:00:00 2001 From: "Eunice A. Obugyei" Date: Fri, 25 Aug 2017 18:33:03 +0000 Subject: [PATCH] BAEL-1014 [Spring MVC with Kotlin] (#2495) * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Removed unnecessary comment * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] Added Exception test cases * Applied baeldung formatter in Eclipse * Merged from https://github.com/eugenp/tutorials Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Revert "Merged from https://github.com/eugenp/tutorials" This reverts commit 74447a163b9e3f244a2578315fbdb525d20cd16b. * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Spring Security for a Java EE Application[http://jira.baeldung.com/browse/BAEL-884] * Updated spring-security version to 4.2.3.RELEASE * Added spring-mvc-kotlin module for http://jira.baeldung.com/browse/BAEL-1014 * Removed dependency for kotlin-reflect * Switched use of JSP to Thymeleaf * Switched use of JSP to Thymeleaf --- spring-mvc-kotlin/pom.xml | 12 ++++-- .../kotlin/mvc/ApplicationWebConfig.kt | 43 ++++++++++++++----- .../main/webapp/WEB-INF/spring-web-config.xml | 23 +++++++--- 3 files changed, 60 insertions(+), 18 deletions(-) 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