整合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

项目结构:

springboot整合视图层之freemarker的更多相关文章

  1. springboot整合视图层之jsp

    在springboot中不推荐视图层使用jsp展示,但是人们以前已经习惯使用jsp,所以对jsp也有支持,但是是解耦性的.也就是说并没有像其他组件一样直接集成到启动器中,所以像jsp引擎之类的需要额外 ...

  2. springboot整合视图层之Thymeleaf

    Thymeleaf中有自己的表达式,和自己的语法,可以把数据取出来后再进行判断,遍历等操作,另外它还封装了strings,dates....对象,可以利用这些对象对已有的数据进行简单的逻辑处理: 1. ...

  3. springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层

    2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static  /public  /resourc ...

  4. 【SpringBoot】09.SpringBoot整合Freemarker

    SpringBoot整合Freemarker 1.修改pom文件,添加坐标freemarker启动器坐标 <project xmlns="http://maven.apache.org ...

  5. springboot整合freemarker

    前后端分离现在越来越多,如何有效的使用springboot来整合我们的页面是一个很重要的问题. springboot整合freemarker有以下几个步骤,也总结下我所犯的错误: 1.加依赖: 2.配 ...

  6. springboot整合mybatis,freemarker

    springboot 整合mybaits,,freemarker pom.xml文件 <?xml version="1.0" encoding="UTF-8&quo ...

  7. springboot 整合 freemarker

    springboot 整合 freemarker 依赖 <parent> <groupId>org.springframework.boot</groupId> & ...

  8. SpringBoot整合freemarker 引用基础

    原 ElasticSearch学习笔记Ⅲ - SpringBoot整合ES 新建一个SpringBoot项目.添加es的maven坐标如下: <dependency> <groupI ...

  9. 【Springboot】Springboot整合Thymeleaf模板引擎

    Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...

随机推荐

  1. Confluence 6 "Duplicate Key" 相关问题解决

    如果你遇到了下面的错误信息,例如: could not insert: [bucket.user.propertyset.BucketPropertySetItem#bucket.user.prope ...

  2. Dig skipfish proxystrike

    1.DNS域名信息收集,(Dig,挖掘局域网的信息之前的博客中已经做过介绍) 查询需要认证的域名服务器  dig -t ns    +   网址 使用工具 fierce 判断主机上存活的其他域名服务器 ...

  3. python WebDriver如何处理右键菜单

    WebDriver如何处理右键菜单 一.背景 在学习selenium webdriver的过程中,遇到这样一个问题.ActionChains类中提供了context_click的方法,它可以用来在we ...

  4. JMeter 提供了六种定时器

    JMeter提供了六种定时器,下面让我们一起来学习下JMeter的定时器. 先明确一些概念: 1)定时器是在每个sampler(采样器)之前执行的,而不是之后: 是的,你没有看错,不管这个定时器的位置 ...

  5. unicode解码

    var newStr = System.Text.RegularExpressions.Regex.Unescape(str);

  6. vetur插件提示 [vue-language-server] Elements in iteration expect to have 'v-bind:key' directives错误的解决办法

    错误提示: [vue-language-server] Elements in iteration expect to have 'v-bind:key' directives.Renders the ...

  7. Mac OS X10.8.3-bash基本命令失效后的修复

    -bash基本命令都失败了. 比如: -bash: ls :command not found   顿时心都凉了. 想要找到.bash_profile文件也不是那么容易的.     step1. 在t ...

  8. 【第一部分】04Leetcode刷题

    一.反转链表 II /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; ...

  9. Mongodb for .Net Core 驱动的应用

    一:我在做.net core 应用mongodb的sdk时,查阅了不少资料,故记录下来,以方便查阅.mongodb类库的版本 mongodb driver 2.4.3,一下方法均来自此版本文件 先看看 ...

  10. JQuery调用WCF服务

    一:创建一个wcf服务项目 [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(RequestF ...