Advertisement

使用Spring Boot-ThemeLeaf替代JSP

阅读量:

Jsp在集成的servlet容器中运行时存在一些局限性(例如,内嵌的Tomcat和Jetty不支持以jar形式运行jsp,而Undertow则完全不支持jsp功能)。

Spring Boot框架为开发者提供了多种模板引擎选项,包括FreeMarker、Groovy、Thymeleaf、Velocity以及Mustache。其中,Spring Boot特别推荐使用Thymeleaf作为模板引擎,原因在于Thymeleaf能够与Spring MVC实现无缝对接,并提供完整的支持。

1.引入依赖

org.springframework.boot

spring-boot-starter-thymeleaf

2.编写控制器

其编写方式与jsp类似

@RequestMapping("/toLogin")

public String toLogin(){

return "user/login";

}

然而,对于jsp而言,需要通过配置前缀和后缀来定位对应的jsp文件。而Thymeleaf默认会从classpath:/templates/目录下查找.html格式的文件。

在源码中也

全部评论 (0)

还没有任何评论哟~