第一步:准备环境:jdk+maven+idea
第二步:idea中新建springboot项目,如下图:
第三步:项目的pom文件中引入freemarker:
<?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.7.14</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>demo</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
第四步:新建IndexController类:
package com.example.demo.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.math.BigDecimal; import java.util.*; @EnableAutoConfiguration @Controller public class IndexController { @RequestMapping("/") @ResponseBody public String first() { return "详细见:http://localhost:8080/index "; } // freemarker @RequestMapping("/index") public String index(Model model){ model.addAttribute("name","小威"); model.addAttribute("age","18"); CustInfo custInfo = new CustInfo(); custInfo.setTxnAmt(new BigDecimal("10000.00")); custInfo.setFeeAmt(new BigDecimal("300.00")); custInfo.setTerm(3); custInfo.setTxnDate(new Date()); model.addAttribute("cust",custInfo); // 首期计划 InstPlan instPlanOne = new InstPlan(); instPlanOne.setTerm(1); instPlanOne.setInstDate(getNextMonthDate(1)); instPlanOne.setTxnAmt(new BigDecimal("3334")); instPlanOne.setFeeAmt(new BigDecimal("100")); instPlanOne.setTotalAmt(new BigDecimal("3434")); // 第2期计划 InstPlan instPlanTwo = new InstPlan(); instPlanTwo.setTerm(2); instPlanTwo.setInstDate(getNextMonthDate(2)); instPlanTwo.setTxnAmt(new BigDecimal("3333")); instPlanTwo.setFeeAmt(new BigDecimal("100")); instPlanTwo.setTotalAmt(new BigDecimal("3433")); // 第3期计划 InstPlan instPlanThree = new InstPlan(); instPlanThree.setTerm(3); instPlanThree.setInstDate(getNextMonthDate(3)); instPlanThree.setTxnAmt(new BigDecimal("3333")); instPlanThree.setFeeAmt(new BigDecimal("100")); instPlanThree.setTotalAmt(new BigDecimal("3433")); //创建Map数据 TreeMap<String, InstPlan> treeMap = new TreeMap<>(); treeMap.put("plan1", instPlanOne); treeMap.put("plan2", instPlanTwo); treeMap.put("plan3", instPlanThree); //存放Map数据 model.addAttribute("instMap",treeMap); return "index"; } /** * 获取month个月后的今天 * */ public Date getNextMonthDate(int month){ Date currentDate = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(currentDate); // 在当前日期上加month月 calendar.add(Calendar.MONTH, month); Date newDate = calendar.getTime(); return newDate; } public static void main(String[] args) { SpringApplication.run(IndexController.class, args); } }
第五步:新建CustInfo类:
package com.example.demo.controller; import java.math.BigDecimal; import java.util.Date; public class CustInfo { //姓名 private String name; //年龄 private int age; //申请日期 private Date txnDate; //授信额度 private BigDecimal txnAmt; //费用 private BigDecimal feeAmt; //期数 private int term; 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; } public Date getTxnDate() { return txnDate; } public void setTxnDate(Date txnDate) { this.txnDate = txnDate; } public BigDecimal getTxnAmt() { return txnAmt; } public void setTxnAmt(BigDecimal txnAmt) { this.txnAmt = txnAmt; } public BigDecimal getFeeAmt() { return feeAmt; } public void setFeeAmt(BigDecimal feeAmt) { this.feeAmt = feeAmt; } public int getTerm() { return term; } public void setTerm(int term) { this.term = term; } @Override public String toString() { return "CustInfo{" + "name='" + name + '\'' + ", age=" + age + ", txnDate=" + txnDate + ", txnAmt=" + txnAmt + ", feeAmt=" + feeAmt + ", term=" + term + '}'; } }
第六步:新建InstPlan类:
package com.example.demo.controller; import java.math.BigDecimal; import java.util.Date; public class InstPlan { //期数 private int term; //还款日期 private Date instDate; //还款本金 private BigDecimal txnAmt; //还款利息 private BigDecimal feeAmt; //总金额 private BigDecimal totalAmt; public int getTerm() { return term; } public void setTerm(int term) { this.term = term; } public Date getInstDate() { return instDate; } public void setInstDate(Date instDate) { this.instDate = instDate; } public BigDecimal getTxnAmt() { return txnAmt; } public void setTxnAmt(BigDecimal txnAmt) { this.txnAmt = txnAmt; } public BigDecimal getFeeAmt() { return feeAmt; } public void setFeeAmt(BigDecimal feeAmt) { this.feeAmt = feeAmt; } public BigDecimal getTotalAmt() { return totalAmt; } public void setTotalAmt(BigDecimal totalAmt) { this.totalAmt = totalAmt; } @Override public String toString() { return "InstPlan{" + "term=" + term + ", instDate=" + instDate + ", txnAmt=" + txnAmt + ", feeAmt=" + feeAmt + ", totalAmt=" + totalAmt + '}'; } }
第七步:新建index.ftl配置文件(这个是freemarker的模板文件):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>huaqibao-freemarker</title> </head> <body> <b>恭喜您,您申请的小威宝借条已成功</b><br><br> 姓名:${name} 年龄:${age}<br> <hr> <b>授信详细信息如下:</b><br/> 申请日期:${cust.txnDate?string("yyyy-MM-dd")}<br/> 授信额度:${cust.txnAmt?string(',###.00')}元<br/> 利息:${cust.feeAmt?string(',###.00')}元<br/> 分期期数:${cust.term}期 <hr> 分期计划如下:<br/> <table> <tr> <td>期数</td> <td>还款日期</td> <td>还款本金</td> <td>还款利息</td> <td>总金额</td> </tr> <#list instMap?keys as key > <tr> <td>${instMap[key].term}</td> <td>${instMap[key].instDate?string("yyyy-MM-dd")}</td> <td>${instMap[key].txnAmt?string(',###.00')}</td> <td>${instMap[key].feeAmt?string(',###.00')}</td> <td>${instMap[key].totalAmt?string(',###.00')}</td> </tr> </#list> </table> </body> </html>
第八步:新建application.yml配置文件:
server: port: 8080 spring: application: name: huaqibao-freemarker-demo freemarker: template-loader-path: classpath:/templates/ cache: false charset: utf-8 check-template-location: true content-type: text/html expose-request-attributes: false expose-session-attributes: false request-context-attribute: request suffix: .ftl
第九步:以上文件目录如下图:
第十步:运行IndexController.main()方法,启动spring boot应用。
然后登陆页面:http://localhost:8080/index
效果如下:
总结:
以上就是基与springboot2.7.14+freemarker案例。
我在做项目的时候强行将springboot3.0.2降至2.7.14,否则jdk1.8上运行会报错。
springboot3.0.2对应jdk版本是17,而springboot2.7.14对应jdk版本只要1.8。