zoukankan      html  css  js  c++  java
  • 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,大功告成

  • 相关阅读:
    Java User Thread and Daemon Thread
    BFS 和 DFS
    fail-fast vs fail-safe iterator in Java
    通过先序遍历和中序遍历后的序列还原二叉树
    单例模式总结
    TCP性能陷阱
    数据库事务的四大特性和事务隔离级别
    深入理解Java虚拟机- 学习笔记
    字符串,引用变量与常量池
    深入理解Java虚拟机- 学习笔记
  • 原文地址:https://www.cnblogs.com/dannyyao/p/6993290.html
Copyright © 2011-2022 走看看