整合freemarker要求必须将视图文件放在 src/main/resources下的templates文件夹下,该文件夹是安全的不可直接访问的,必须由controller之类的接受请求类去跳转,因为如果直接访问就意味着需要及时响应,而springboot需要给展示文件去渲染,这需要时间,所以他是不允许被直接访问的。

1.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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.10.RELEASE</version></parent><groupId>com.mr.li</groupId><artifactId>springboot_004</artifactId><version>0.0.1-SNAPSHOT</version><properties><java.version>1.7</java.version></properties><dependencies><!-- 添加启动器 --><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></dependencies>
</project>

2.实体类

package com.mr.li.pojo;public class User {private int id;private String name;private int age;public User(int id, String name, int age) {super();this.id = id;this.name = name;this.age = age;}public User() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

3.controller

    package com.mr.li.controller;import java.util.ArrayList;
import java.util.List;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;import com.mr.li.pojo.User;@Controller
public class MyController {@RequestMapping("/show")public String show(Model model) {//生成jsp页面需要展示的数据源List<User> list = new ArrayList<User>();list.add(new User(1, "小明", 15));list.add(new User(2, "小红", 16));list.add(new User(3, "小丽", 17));//这里前面的list是在jsp中使用EL表达式所使用的名称model.addAttribute("list", list);//这里返回的是jsp页面名称,前后缀在配置文件中return "users";}
}

5.users.ftl  此文件名字叫users因为controller中show方法返回的名字叫users,里面的EL表达式中的list是controller类中的model对象的addAttribute方法的key,参数名为list.

<html><head><title>展示用户数据</title><meta charset="utf-9"></meta></head><body><table border="1" align="center" width="50%"><tr><th>ID</th><th>Name</th><th>Age</th></tr><#list list as user ><tr><td>${user.id}</td><td>${user.name}</td><td>${user.age}</td></tr></#list>    </table></body>
</html>

访问的url: http://localhost:8080/show

项目结构:

转载于:https://www.cnblogs.com/li-yan-long/p/10777212.html

springboot整合视图层之freemarker相关推荐

  1. springboot整合视图层Thymeleaf、freemarker

    springboot整合视图层以thymeleaf和freemarker为例 1.整合Thymeleaf 主要配置application.properties #是否开启缓存,默认为true spri ...

  2. springboot整合freemarker案例

    springboot整合freemarker案例 1.pom.xml 2.application.properties 3.实体类 4.controller 5.取普通变量的值 6.遍历集合 7.遍历 ...

  3. springboot整合freemarker FTL stack trace (“~“ means nesting-related): - Failed at: #

    springboot整合freemarker FTL stack trace ("~" means nesting-related): - Failed at: 问题: 后台mod ...

  4. 八、springboot整合Spring Security

    springboot整合Spring Security 简介 Spring Security是一个功能强大且可高度自定义的身份验证和访问控制框架.它是保护基于Spring的应用程序的事实标准. Spr ...

  5. 【模板引擎】Springboot整合ThymeleafThymeleaf基本语法

    Thymeleaf介绍 thymeleaf是一个XML/XHTML/HTML5模板引擎,可用于Web与非Web环境中的应用开发.它是一个开源的Java库,基于Apache License 2.0许可, ...

  6. springmvc整合redis_111 SpringBoot整合Servlet JSP Filter Listener

    1. SpringBoot介绍 (1) 什么是Spring Boot? (2) Spring Boot有哪些特点? Spring Boot 设计目的是用来简化新 Spring 应用的初始搭建以及开发过 ...

  7. springboot整合jpa之连接MySql实现crud

    整体思路:搭建springboot一定要注意版本问题,各个jar包之间是否兼容.搭建可以从上往下进行,是一个从Controller层和Service层-->Dao层的一个过程. 1.添加mave ...

  8. Java学习之SpringBoot整合SSM Demo

    背景:在Java Web中Spring家族有着很重要的地位,之前JAVA开发需要做很多的配置,一堆的配置文件和部署调试一直是JavaWeb开发中的一大诟病,但现在Spring推出了SpringBoot ...

  9. 超详细教程:SpringBoot整合MybatisPlus

    本文分享自华为云社区<SpringBoot整合MybatisPlus[超详细]>,原文作者:牛哄哄的柯南. 创建个SpringBoot项目 选生所需的依赖:== 我把application ...

最新文章

  1. MLPerf基准测试再发榜,浪潮AI服务器刷新18项纪录
  2. 解决远程连接mysql很慢的方法(mysql_connect 打开连接慢)
  3. Windows 快速删除 大量文件
  4. 可以在没有main()的情况下编写C程序吗?
  5. Debian 系统安装 Nagios 服务器监控端
  6. UsernamePasswordAuthenticationFilter源码分析
  7. 米家小白增强固件_中考体育:男1000米/女800米想拿满分,掌握呼吸法是关键
  8. 把一个base64编码的图片绘制到canvas (canvas的图片在转成dataurl)
  9. 记录一次成功的EC、BIOS降级操作
  10. 小米手机只能进fastboot怎么办?
  11. ThingJS学习总结
  12. python识别字母代码_【Python-Opencv】KNN英文字母识别
  13. Nginx配置https(ssl证书)并反向代理Springboot Jar项目
  14. 原生JavaScript实现entries和fromEntries
  15. java手机教程_Java手机基础教程 (普通高等教育“十二五”规划教材(动漫游戏类))...
  16. buntu18.04无线网卡无法识别问题
  17. mysql中部分语句
  18. 敏捷开发项目管理流程
  19. java火车票订票系统 论文_毕业设计(论文)-基于JAVA的火车票售票系统.doc
  20. php tts,给博客添加TTS语音朗读 简单快速版

热门文章

  1. java项目分页_Java项目实战分页类
  2. 23种设计模式[1]:单例模式
  3. oracle基础学习---------1
  4. [BZOJ 4571][Scoi2016]美味(主席树)
  5. Python中布尔类型
  6. WdatePicker日历控件使用方法(转)
  7. 第二阶段个人总结1(5.25)
  8. 抽象类和普通类主要有三点比较大的区别,super
  9. Servlet 介绍
  10. SQL解析引擎Apache Calcite