首页 > 代码库 > DEMO: springboot 与 freemarker 集成
DEMO: springboot 与 freemarker 集成
直接在 DEMO: springboot 与 mybatis 集成 基础上,进行修改。
1、pom.xml 中引用 依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> <version>1.4.1.RELEASE</version> </dependency>
2、src/main/resources/application.properties 中添加 freemarker 配置
关键是模板的位置:spring.freemarker.template-loader-path=classpath:/static/ftl
########################################################
###freemarker
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.allow-session-override=false
spring.freemarker.cache=true
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.prefer-file-system-access=true
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/static/ftl
spring.freemarker.settings.template_update_delay=0
spring.freemarker.settings.default_encoding=UTF-8
spring.freemarker.settings.classic_compatible=true
spring.freemarker.order=1
3、添加模板文件 src/main/resources/static/ftl/student.ftl
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <#list students as student > ${student.id} - ${student.name} - ${student.sex} - ${student.no} <br/> </#list> </body> </html>
4、修改 StudentController.java
1、方法返回,void 改成 String,返回模板名字 “student”
2、加个map入参,传数据给前端模板
package demo.springboot.controller; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import demo.springboot.model.Student; import demo.springboot.service.StudentService; @Controller @RequestMapping("/student") public class StudentController { private static final Logger LOGGER = LoggerFactory.getLogger(StudentController.class); @Autowired private StudentService studentService; @RequestMapping("/list") public String listStudent(Map<String, Object> map){ List<Student> students = new ArrayList<>(); students = studentService.listStudents(); for (Student student : students){ LOGGER.info(student.getId() + " - " + student.getName() + " - " + student.getNo() + " - " + student.getSex()); } map.put("students", students); return "student"; } }
5、启动应用,浏览器访问 http://localhost:8080/student/list,大功告成
DEMO: springboot 与 freemarker 集成
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。