springboot模板引擎整合详解

Spring Boot提供了多种模板引擎的默认配置支持,但嵌入式容器JSP有限制,2010年后Velocity停止更新,所以这JSP与Velocity两个不建议使用,然实际在企业中,还是有很多在使用。那么这里主要是springboot整合FreeMarker模块。

如果觉得文章不错,对你有帮助,请作者喝杯咖啡,谢谢!如果对您有帮助 ,请多多支持.多少都是您的心意与支持,一分也是爱,再次感谢!!!打开支付宝首页搜“556723462”领红包,领到大红包的小伙伴赶紧使用哦!感谢大家的支持!您的支持,我会继续分享更多的文章,欢迎关注!

Spring Boot提供了默认配置的模板引擎主要有以下几种:
· Thymeleaf
· FreeMarker
· Velocity
· Groovy
· Mustache
· JSP

 

1,创建一个maven项目结构如下图:

2,在pom.xml中引入相应的配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springboot.web</groupId>
<artifactId>springboot-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!--springboot配置父目录依赖的版本,必须要添加 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<!--springboot的web支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入freeMarker的依赖包. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- 引入thymeleaf的依赖包. -->
<dependency>
<groupId>org.springframework.boot </groupId>
<artifactId>spring-boot-starter-thymeleaf </artifactId>

</dependency>

                <!-- 引入jsp的依赖包. -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

</dependencies>
<build>
<plugins>
<!--添加Spring boot的插件,这里是maven启动方式需要加的,建议加上 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- java编译插件,设置jdk的版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>

3.在src/main/resources/创建一个templates文件夹,再分别创建freeMarker,thymeleaf,jsp三个模板引擎所对应的文件后缀分别为*.ftl  -*.html -*.jsp(Thymeleaf是一个java类库,他是一个xml/xhtml/html5的模板引擎,可以作为mvc的web应用的view层。)如图所示;

4.编写模块引擎所对应的controller如图所示:

FreeMarkerIndexController.java具体代码如下:

package com.demo.controller;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller //标示为controller层
public class FreeMarkerIndexController {
@RequestMapping("/freemarkerindexController")//请求路径,会去templates下去寻找freemarkerindex
public String freemarkerindexController(Map<String, Object> result) {
System.out.println("hello world");//在控制台输出打印hello world
result.put("name", "wangwu");//添加姓名
result.put("sex", 1);//添加性别
List<String> list = new ArrayList<String>();//创建一个无序集合
list.add("zhangsan");//添加张三
list.add("lisi");//添加李四
result.put("userlist", list);//添加结果
return "freemarkerindex";//返回templates的结果
}

}

 

5.创建springboot的主入口,我这里叫Main,(有的可能叫App主口,这个根据自己的习惯)。如图所示:

启动访问路径:http://localhost:8080/freemarkerindexController

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值