springboot整合视图层之freemarker
整合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的更多相关文章
- springboot整合视图层之jsp
在springboot中不推荐视图层使用jsp展示,但是人们以前已经习惯使用jsp,所以对jsp也有支持,但是是解耦性的.也就是说并没有像其他组件一样直接集成到启动器中,所以像jsp引擎之类的需要额外 ...
- springboot整合视图层之Thymeleaf
Thymeleaf中有自己的表达式,和自己的语法,可以把数据取出来后再进行判断,遍历等操作,另外它还封装了strings,dates....对象,可以利用这些对象对已有的数据进行简单的逻辑处理: 1. ...
- springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层
2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static /public /resourc ...
- 【SpringBoot】09.SpringBoot整合Freemarker
SpringBoot整合Freemarker 1.修改pom文件,添加坐标freemarker启动器坐标 <project xmlns="http://maven.apache.org ...
- springboot整合freemarker
前后端分离现在越来越多,如何有效的使用springboot来整合我们的页面是一个很重要的问题. springboot整合freemarker有以下几个步骤,也总结下我所犯的错误: 1.加依赖: 2.配 ...
- springboot整合mybatis,freemarker
springboot 整合mybaits,,freemarker pom.xml文件 <?xml version="1.0" encoding="UTF-8&quo ...
- springboot 整合 freemarker
springboot 整合 freemarker 依赖 <parent> <groupId>org.springframework.boot</groupId> & ...
- SpringBoot整合freemarker 引用基础
原 ElasticSearch学习笔记Ⅲ - SpringBoot整合ES 新建一个SpringBoot项目.添加es的maven坐标如下: <dependency> <groupI ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
随机推荐
- 断路器Feign
Feign是自带断路器,需要在配置文件中开启断路器 改造消费者项目(FeignDemo) 1.在application.yml配置文件中开启断路器 eureka: client: service-ur ...
- 【python】python为何多线程无法切换
写了一个kafka传输消息,celery发布任务的脚本. 有四个线程,分别读取不同的kafka队列信息 问题是,只有第一个线程会启动,剩下的三个线程连start都运行不了. 而且这个问题不是一开始就发 ...
- laravel 多对多关联 attach detach sync
用户表和角色表,多对多关联,一个用户有多个角色,一个角色属于多个用户 添加多对多关联 attach: 给1号用户添加1号角色,并把关联表的column字段赋值为$value,后边的数组需要的时候再添加 ...
- HTML&javaSkcript&CSS&jQuery&ajax(六)
一.HTML表单 1.<input type="text">定义文本输入的单上输入字段,<form> First name:<br> < ...
- Python中的构造方法
在Java等语言中都有构造方法[进行对象的创建及初始化]这个东东,示例代码如下: public class Student { //成员变量 private String name; private ...
- ajax之全局函数
1.全局函数:$.each(array,function(){1,value}),通过$/jQuery直接调用 对象函数:$("#name").val(); jQuery UI:$ ...
- [转] Lodash常用API笔记
原生用法 直接使用的API _.reject 根据条件去除某个元素. var foo = [ {id: 0, name: "aaa", age: 33}, {id: 1, name ...
- java内嵌jetty服务器
有的时候需要将一个简单的功能封装为服务,相比python使用flask.web.py的简洁,使用java-web显得太重量级,幸好,我们可以直接在java项目中使用jetty来搭建简易服务 1.pom ...
- jQuery选择器总结 jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法
新年第一编文章 jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值 ...
- Python_lambda简单函数表达式
lambda表达式只能用于简单函数的书写 def funx(a): a+=1 return a print(funx(99)) 用lambda实现上面函数: funx = lambda a: a+1 ...