[JAVA]SpringBoot整合freemarker
FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。–百度百科
首先pom.xml导入freemarker依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
跟Spring Boot整合Jsp该篇文章类似,一个实体类用户返回结果,controller和启动类。
Student.java
package xyz.xioaxin12.springboot.controller.student;
public class Student {
private Integer id;
private String name;
private int age;
public Student() {
}
public Student(Integer id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
StudentController .java
package xyz.xioaxin12.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import xyz.xioaxin12.springboot.controller.student.Student;
import java.util.ArrayList;
import java.util.List;
@Controller
public class StudentController {
@RequestMapping("/show")
public String showStudent(Model model){
List<Student> list = new ArrayList<>();
list.add(new Student(1,"小信",22));
list.add(new Student(2,"天天",25));
list.add(new Student(1,"赵六",20));
model.addAttribute("list",list);
return "show";
}
}
启动类
package xyz.xioaxin12.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author 爱生活爱技术
*/
@SpringBootApplication
public class SpringBoot04FreemarkerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot04FreemarkerApplication.class, args);
}
}
最后编写视图
注意:模板形式的文件必须放在templates下,外界不可直接访问的!
<html>
<head>
<title>freemarker</title>
<meta charset="utf-8"/>
</head>
<body>
<table border="2" align="center" width="50%">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<#list list as s>
<tr>
<td>${s.id}</td>
<td>${s.name}</td>
<td>${s.age}</td>
</tr>
</#list>
</table>
</body>
</html>
freemarker官方在线手册:点击
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 xiaoxin1218@qq.com
文章标题:[JAVA]SpringBoot整合freemarker
文章字数:515
本文作者:周信
发布时间:2019-10-24, 00:43:38
最后更新:2023-05-03, 10:25:35
原始链接:http://zx21.xyz/2019/10/24/JAVA-SpringBoot整合freemarker/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。
