初始化git 基础代码
This commit is contained in:
49
heima-leadnews-test/freemarker-demo/pom.xml
Normal file
49
heima-leadnews-test/freemarker-demo/pom.xml
Normal file
@@ -0,0 +1,49 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.heima</groupId>
|
||||
<artifactId>heima-leadnews</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>freemarker-demo</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
<!-- lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- apache 对 java io 的封装工具库 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.heima.freemarker;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class FreemarkerDemoApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(FreemarkerDemoApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.heima.freemarker.controller;
|
||||
|
||||
import com.heima.freemarker.entity.Student;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Controller
|
||||
public class HelloController {
|
||||
|
||||
@GetMapping("/basic")
|
||||
public String hello(Model model) {
|
||||
model.addAttribute("name", "freemarker");
|
||||
Student student = new Student();
|
||||
student.setName("小明");
|
||||
student.setAge(18);
|
||||
model.addAttribute("stu", student);
|
||||
return "01-basic";
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
public String list(Model model){
|
||||
|
||||
Student stu1 = new Student();
|
||||
stu1.setName("小强");
|
||||
stu1.setAge(18);
|
||||
stu1.setMoney(1000.86f);
|
||||
stu1.setBirthday(new Date());
|
||||
|
||||
//小红对象模型数据
|
||||
Student stu2 = new Student();
|
||||
stu2.setName("小红");
|
||||
stu2.setMoney(200.1f);
|
||||
stu2.setAge(19);
|
||||
|
||||
//将两个对象模型数据存放到List集合中
|
||||
List<Student> stus = new ArrayList<>();
|
||||
stus.add(stu1);
|
||||
stus.add(stu2);
|
||||
|
||||
//向model中存放List集合数据
|
||||
model.addAttribute("stus",stus);
|
||||
|
||||
Map<String, Student> stuMap = new HashMap<>();
|
||||
stuMap.put("stu1", stu1);
|
||||
stuMap.put("stu2", stu2);
|
||||
|
||||
model.addAttribute("stuMap",stuMap);
|
||||
model.addAttribute("today",new Date());
|
||||
model.addAttribute("point",123123123123123L);
|
||||
|
||||
return "02-list";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.heima.freemarker.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
public class Student {
|
||||
private String name;//姓名
|
||||
private int age;//年龄
|
||||
private Date birthday;//生日
|
||||
private Float money;//钱包
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8881 #服务端口
|
||||
spring:
|
||||
application:
|
||||
name: freemarker-demo #指定服务名
|
||||
freemarker:
|
||||
cache: false #关闭模板缓存,方便测试
|
||||
settings:
|
||||
template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
|
||||
suffix: .ftl #指定Freemarker模板文件的后缀名
|
||||
template-loader-path: classpath:/templates/
|
||||
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Hello World!</title>
|
||||
</head>
|
||||
<body>
|
||||
<b>普通文本 String 展示:</b><br><br>
|
||||
Hello ${name!'------'} <br>
|
||||
<hr>
|
||||
<b>对象Student中的数据展示:</b><br/>
|
||||
姓名:${stu.name}<br/>
|
||||
年龄:${stu.age}
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,82 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Hello World!</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<#-- list 数据的展示 -->
|
||||
<b>展示list中的stu数据:</b>
|
||||
<br>
|
||||
<br>
|
||||
<table>
|
||||
<tr>
|
||||
<td>序号</td>
|
||||
<td>姓名</td>
|
||||
<td>年龄</td>
|
||||
<td>钱包</td>
|
||||
</tr>
|
||||
<#if stus??>
|
||||
<#list stus as stu>
|
||||
<#if stu.name="小红">
|
||||
<tr style="color: red">
|
||||
<td>${stu_index + 1}</td>
|
||||
<td>${stu.name}</td>
|
||||
<td>${stu.age}</td>
|
||||
<td>${stu.money}</td>
|
||||
</tr>
|
||||
<#else >
|
||||
<tr>
|
||||
<td>${stu_index + 1}</td>
|
||||
<td>${stu.name}</td>
|
||||
<td>${stu.age}</td>
|
||||
<td>${stu.money}</td>
|
||||
</tr>
|
||||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
|
||||
stus集合大小:${stus?size}
|
||||
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<#-- Map 数据的展示 -->
|
||||
<b>map数据的展示:</b>
|
||||
<br/><br/>
|
||||
<a href="###">方式一:通过map['keyname'].property</a><br/>
|
||||
输出stu1的学生信息:<br/>
|
||||
姓名:${stuMap['stu1'].name}<br/>
|
||||
年龄:${stuMap['stu1'].age}<br/>
|
||||
<br/>
|
||||
<a href="###">方式二:通过map.keyname.property</a><br/>
|
||||
输出stu2的学生信息:<br/>
|
||||
姓名:${stuMap.stu2.name}<br/>
|
||||
年龄:${stuMap.stu2.age}<br/>
|
||||
|
||||
<br/>
|
||||
<a href="###">遍历map中两个学生信息:</a><br/>
|
||||
<table>
|
||||
<tr>
|
||||
<td>序号</td>
|
||||
<td>姓名</td>
|
||||
<td>年龄</td>
|
||||
<td>钱包</td>
|
||||
</tr>
|
||||
<#list stuMap?keys as key>
|
||||
<tr>
|
||||
<td>${key_index + 1}</td>
|
||||
<td>${stuMap[key].name}</td>
|
||||
<td>${stuMap[key].age}</td>
|
||||
<td>${stuMap[key].money}</td>
|
||||
</tr>
|
||||
</#list>
|
||||
</table>
|
||||
<hr>
|
||||
当前的日期为:${today?datetime} <br>
|
||||
当前的日期为:${today?string("yyyy年MM月")}<br>
|
||||
<hr>
|
||||
${point?c}
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.heima.freemarker.test;
|
||||
|
||||
import com.heima.freemarker.FreemarkerDemoApplication;
|
||||
import com.heima.freemarker.entity.Student;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.util.*;
|
||||
|
||||
@SpringBootTest(classes = FreemarkerDemoApplication.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
public class FreemarkerTest {
|
||||
|
||||
@Autowired
|
||||
private Configuration configuration;
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
Template template = configuration.getTemplate("02-list.ftl");
|
||||
/**
|
||||
* 合成方法
|
||||
*/
|
||||
template.process(getData(),new FileWriter("d:/list.html"));
|
||||
}
|
||||
|
||||
private Map getData(){
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
|
||||
Student stu1 = new Student();
|
||||
stu1.setName("小强");
|
||||
stu1.setAge(18);
|
||||
stu1.setMoney(1000.86f);
|
||||
stu1.setBirthday(new Date());
|
||||
|
||||
//小红对象模型数据
|
||||
Student stu2 = new Student();
|
||||
stu2.setName("小红");
|
||||
stu2.setMoney(200.1f);
|
||||
stu2.setAge(19);
|
||||
|
||||
//将两个对象模型数据存放到List集合中
|
||||
List<Student> stus = new ArrayList<>();
|
||||
stus.add(stu1);
|
||||
stus.add(stu2);
|
||||
|
||||
//向model中存放List集合数据
|
||||
map.put("stus",stus);
|
||||
|
||||
Map<String, Student> stuMap = new HashMap<>();
|
||||
stuMap.put("stu1", stu1);
|
||||
stuMap.put("stu2", stu2);
|
||||
|
||||
map.put("stuMap",stuMap);
|
||||
map.put("today",new Date());
|
||||
map.put("point",123123123123123L);
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8881 #服务端口
|
||||
spring:
|
||||
application:
|
||||
name: freemarker-demo #指定服务名
|
||||
freemarker:
|
||||
cache: false #关闭模板缓存,方便测试
|
||||
settings:
|
||||
template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
|
||||
suffix: .ftl #指定Freemarker模板文件的后缀名
|
||||
template-loader-path: classpath:/templates/
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Hello World!</title>
|
||||
</head>
|
||||
<body>
|
||||
<b>普通文本 String 展示:</b><br><br>
|
||||
Hello ${name!'------'} <br>
|
||||
<hr>
|
||||
<b>对象Student中的数据展示:</b><br/>
|
||||
姓名:${stu.name}<br/>
|
||||
年龄:${stu.age}
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,82 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Hello World!</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<#-- list 数据的展示 -->
|
||||
<b>展示list中的stu数据:</b>
|
||||
<br>
|
||||
<br>
|
||||
<table>
|
||||
<tr>
|
||||
<td>序号</td>
|
||||
<td>姓名</td>
|
||||
<td>年龄</td>
|
||||
<td>钱包</td>
|
||||
</tr>
|
||||
<#if stus??>
|
||||
<#list stus as stu>
|
||||
<#if stu.name="小红">
|
||||
<tr style="color: red">
|
||||
<td>${stu_index + 1}</td>
|
||||
<td>${stu.name}</td>
|
||||
<td>${stu.age}</td>
|
||||
<td>${stu.money}</td>
|
||||
</tr>
|
||||
<#else >
|
||||
<tr>
|
||||
<td>${stu_index + 1}</td>
|
||||
<td>${stu.name}</td>
|
||||
<td>${stu.age}</td>
|
||||
<td>${stu.money}</td>
|
||||
</tr>
|
||||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
|
||||
stus集合大小:${stus?size}
|
||||
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<#-- Map 数据的展示 -->
|
||||
<b>map数据的展示:</b>
|
||||
<br/><br/>
|
||||
<a href="###">方式一:通过map['keyname'].property</a><br/>
|
||||
输出stu1的学生信息:<br/>
|
||||
姓名:${stuMap['stu1'].name}<br/>
|
||||
年龄:${stuMap['stu1'].age}<br/>
|
||||
<br/>
|
||||
<a href="###">方式二:通过map.keyname.property</a><br/>
|
||||
输出stu2的学生信息:<br/>
|
||||
姓名:${stuMap.stu2.name}<br/>
|
||||
年龄:${stuMap.stu2.age}<br/>
|
||||
|
||||
<br/>
|
||||
<a href="###">遍历map中两个学生信息:</a><br/>
|
||||
<table>
|
||||
<tr>
|
||||
<td>序号</td>
|
||||
<td>姓名</td>
|
||||
<td>年龄</td>
|
||||
<td>钱包</td>
|
||||
</tr>
|
||||
<#list stuMap?keys as key>
|
||||
<tr>
|
||||
<td>${key_index + 1}</td>
|
||||
<td>${stuMap[key].name}</td>
|
||||
<td>${stuMap[key].age}</td>
|
||||
<td>${stuMap[key].money}</td>
|
||||
</tr>
|
||||
</#list>
|
||||
</table>
|
||||
<hr>
|
||||
当前的日期为:${today?datetime} <br>
|
||||
当前的日期为:${today?string("yyyy年MM月")}<br>
|
||||
<hr>
|
||||
${point?c}
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user