在springboot中使用模板引擎的大致步骤:
① 在pom.xml中添加相应的依赖(thymeleaf或freemarker);
② 在application.properties中添加配置信息,开发中建议关闭缓存;
③ 编写模板文件, thymeleaf的默认后缀是.html; freemarker的默认后缀是.ftl(都可在application.properties中配置修改)
④ 编写访问模板文件的controller;
一. springboot使用thymeleaf模板引擎
总览:
① 在pom.xml中引入thymeleaf的依赖;
② 在application.propertiest中配置thymeleaf;
③ 在resource下创建模板文件夹templates; 并创建页面;
④ 编写controller, 返回视图名或ModelAndView对象;
步骤1:
在pom.xml中引入thymeleaf的依赖:
<!-- 引入模板引擎的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
步骤2 :
在application.properties中配置thymeleaf:
# 是否开启thymeleaf缓存
spring.thymeleaf.cache=false
# 前缀
#spring.thymeleaf.prefix=classpath:/templates/
# 后缀
#spring.thymeleaf.suffix=.jsp
#
#spring.thymeleaf.model=HTML5
# 编码
#spring.thymeleaf.encoding=UTF8
#spring.thymeleaf.content-type=text/html
步骤3 :
在resource下创建模板文件夹templates, 并创建页面:
步骤4 :
编写controller, 返回视图名或ModelAndView对象:
情况一: 仅仅返回视图
package online.bendou.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
@RequestMapping("/")
public String toIndex(){
return "index";
}
}
情况二: 返回视图和数据
在方法参数中添加Map<String, Object> map形参, 可以将值放在此map中可以携带回页面(具体是什么原理以后再说)
package online.bendou.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
@RequestMapping("/")
public String toIndex(Map<String,Object> map){
map.put("name", "Lx");
return "index";
}
}
页面使用EL取值:
<h3>welcom, <span th:text="${name}" ></span></h3>
二. springboot使用freemarker模板引擎
步骤1 :
在pom.xml中引入thymeleaf的依赖:
<!-- 引入freemarker模板引擎的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
步骤2 :
在application.properties中配置freemarker:
spring.freemarker.charset=UTF-8
spring.freemarker.cache=false
spring.freemarker.allow-request-override=false
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/
#spring.freemarker.view-names=
步骤3 :
在resource下创建模板文件夹templates, 并创建页面:
步骤4 :
编写controller, 返回视图名或ModelAndView对象:
情况一: 仅仅返回视图
package online.bendou.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/freemarker")
public class FreeMarkerController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
情况二: 返回视图和数据
在方法参数中添加Map<String, Object> map形参, 可以将值放在此map中可以携带回页面(具体是什么原理以后说)
package online.bendou.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/freemarker")
public class FreeMarkerController {
@GetMapping("/hello")
public String hello(Map<String, Object> map){
map.put("name", "Lx");
return "hello";
}
}
页面使用EL取值:
<h2>Hello, ${name}</h2>
注:
两个模板引擎( thymeleaf和freemarker )可以同时配置使用.
21万+

被折叠的 条评论
为什么被折叠?



