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
This commit is contained in:
Eunice A. Obugyei 2017-08-25 18:33:03 +00:00 committed by Zeger Hendrikse
parent cec17bdbfe
commit d492c6f4d8
3 changed files with 60 additions and 18 deletions

View File

@ -35,9 +35,15 @@
<version>4.3.10.RELEASE</version> <version>4.3.10.RELEASE</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>org.thymeleaf</groupId>
<artifactId>jstl</artifactId> <artifactId>thymeleaf</artifactId>
<version>1.2</version> <version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.7.RELEASE</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -1,13 +1,16 @@
package com.baeldung.kotlin.mvc 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.Bean
import org.springframework.context.annotation.Configuration 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.EnableWebMvc
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry import org.springframework.web.servlet.config.annotation.ViewControllerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
import org.springframework.web.servlet.view.InternalResourceViewResolver import org.thymeleaf.spring4.SpringTemplateEngine
import org.springframework.web.servlet.view.JstlView 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 @EnableWebMvc
@Configuration @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?) { override fun addViewControllers(registry: ViewControllerRegistry?) {
super.addViewControllers(registry) super.addViewControllers(registry)
@ -24,14 +33,28 @@ open class ApplicationWebConfig: WebMvcConfigurerAdapter() {
} }
@Bean @Bean
open fun viewResolver(): ViewResolver { open fun templateResolver(): SpringResourceTemplateResolver {
val bean = InternalResourceViewResolver() 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
bean.setPrefix("/WEB-INF/view/") open fun templateEngine(): SpringTemplateEngine {
bean.setSuffix(".jsp") 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
} }
} }

View File

@ -12,13 +12,26 @@
<context:component-scan base-package="com.baeldung.kotlin.mvc" /> <context:component-scan base-package="com.baeldung.kotlin.mvc" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:view-controller path="/welcome.html"/> <mvc:view-controller path="/welcome.html"/>
<mvc:annotation-driven /> <mvc:annotation-driven />
<bean id="templateResolver"
class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML" />
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
</bean>
</beans> </beans>