实质上,FreeMarker 的模板文件 只是FreeMarker引入的格式的文本文件 ,即 FreeMarker模板措辞(FTL)。
Freemarker可用于更换web mvc运用程序是图层上的jsp。
下面是我们将在本课中实行的运用程序的图像:
6.2. 创建运用程序
在eclipse上,选择:
File/New/Other…输入:
Name: SpringBootFreeMarkerGroup: me.laocatArtifact: SpringBootFreeMarkerDescription: Spring Boot and FreeMarkerPackage: me.laocat.freemarker选择2 Web和Freemarker技能
你的项目已创建:
pom.xml
<?xml version="1.0" encoding="UTF-8"?><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 https://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>2.1.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>me.laocat</groupId><artifactId>SpringBootFreeMarker</artifactId><version>0.0.1-SNAPSHOT</version><name>SpringBootFreeMarker</name><description>Spring Boot and Thymeleaf</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
6.3 FreeMarker 模版
我们将创建三个模板文件并将它们放在src/main/resources/templates目录中:
index.flt
<#import "/spring.ftl" as spring/> <!DOCTYPE HTML><html> <head> <meta charset="UTF-8" /> <title>首页</title> <link rel="stylesheet" type="text/css" href="<@spring.url '/css/style.css'/>"/> </head> <body> <h1>首页</h1> <#if message??> <h2>${message}</h2> </#if> <a href="<@spring.url '/userList'/>">用户列表</a> </body> </html>
userList.flt
<#import "/spring.ftl" as spring/> <html> <head> <title>用户列表</title> <link rel="stylesheet" type="text/css" href="<@spring.url '/css/style.css'/>"/> </head> <body> <h3>用户列表</h3> <a href="addUser">添加用户</a> <br><br> <div> <table border="1"> <tr> <th>用户名</th> <th>密码</th> </tr><#list users as user><tr><td>${user.userName}</td><td>${user.passWord}</td></tr></#list> </table> </div> </body></html>
addUser.flt
<#import "/spring.ftl" as spring/> <html> <head> <meta charset="UTF-8" /> <title>添加用户</title> <link rel="stylesheet" type="text/css" href="<@spring.url '/css/style.css'/>"/> </head> <body> <#if errorMessage??> <div style="color:red;font-style:italic;"> ${errorMessage} </div> </#if> <div> <fieldset> <legend>添加用户</legend> <form name="userForm" action="/addUser" method="POST"> 用户名: <@spring.formInput "userForm.userName" "" "text"/> <br/> 密码: <@spring.formInput "userForm.passWord" "" "password"/> <br/> <input type="submit" value="保存" /> </form> </fieldset> </div> </body></html>
所有模板文件都须要声明一个“自由标记宏”。
<!-- FreeMarker Macros --><#import "/spring.ftl" as spring/>
在模板文件中,有FreeMarker标记,它们是帮助FreeMarker引擎处理数据的指令。FreeMarker引擎剖析模板文件并结合java数据天生新文档。
下面是在Freemarker中利用Context Path的示例:
<!-- Example 1: --> <a href="<@spring.url '/mypath/abc.html'/>">A Link</a> Output: ==> <a href="/my-context-path/mypath/abc.html">A Link</a> <!-- Example 2: --> <form action="<@spring.url '/mypath/abc.html'/>" method="POST"> Output: ==> <form action="/my-context-path/mypath/abc.html" method="POST">
6.4 静态资源属性文件
对付静态资源,如css、javascript、image文件…您须要将它们放入src/main/resources/static文件夹或其子文件夹中。
style.css
h1 { color:#0000FF;} h2 { color:#FF0000;} table { border-collapse: collapse;} table th, table td { padding: 5px;}
application.properties
hello.message=Hello FreeMarkererror.message=用户名 和密码是必须的!
6.5 模型、表单、掌握器类
User.java
package me.laocat.freemarker.bean;public class User {private String userName;private String passWord;public User() {}public User(String userName, String passWord) {super();this.userName = userName;this.passWord = passWord;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassWord() {return passWord;}public void setPassWord(String passWord) {this.passWord = passWord;}}
UserForm类表示在“addUser”页上创建新的用户表单数据。
UserForm.java
package me.laocat.freemarker.form;public class UserForm {private String userName;private String passWord;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassWord() {return passWord;}public void setPassWord(String passWord) {this.passWord = passWord;}}
UserController是一个掌握器类,它处理用户的需求并掌握运用程序流转。
UserController.java
package me.laocat.freemarker.controller;import java.util.ArrayList;import java.util.List;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import me.laocat.freemarker.bean.User;import me.laocat.freemarker.form.UserForm;@Controllerpublic class UserController {private List<User> users = new ArrayList<User>();@Value("${hello.message}")private String message;@Value("${error.message}")private String errorMessage;/ 跳转到首页 /@RequestMapping(value={"/","/index"},method = RequestMethod.GET)public String index(Model model) {model.addAttribute("message", message);return "index";}/ 查看用户列表 /@RequestMapping(value={"/userList"},method = RequestMethod.GET)public String userList(Model model) {model.addAttribute("users", users);return "userList";}@RequestMapping(value={"/addUser"},method = RequestMethod.GET)public String showAddUserPage(Model model) { UserForm userForm = new UserForm(); model.addAttribute("userForm", userForm);return "addUser";}@RequestMapping(value={"/addUser"},method = RequestMethod.POST)public String saveUser(Model model,@ModelAttribute("userForm")UserForm userForm) {String userName = userForm.getUserName();String passWord = userForm.getPassWord();if(userName != null && userName.length()>0 && passWord != null && passWord.length() > 0){User user = new User(userName,passWord);users.add(user);return "redirect:/userList";}model.addAttribute("errorMessage", errorMessage);return "addUser";}}
6.6 运行运用
要运行运用程序,请右键单击项目并选择:
Run As/Spring Boot App.该运用程序已支配在嵌入式Web做事器上。
在浏览器上运行以下url:
http://localhost:8080/