
目录
2.2 在springboot中添加freemarker配置
3.1 将以下内容添加IndexController文件里面
3.2 将以下内容添加到 创建好的 index 模板页面里面
FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据,并用来生成输出文(HTML网页、电子邮件、配置文件、源代码等)的通用工具。
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-freemarkerartifactId>
- dependency>
将以下内容添加到application.properties配置文件里面
- #配置jdbc
- spring:
- #配置freemarker
- freemarker:
- #指定HttpServletRequest的属性是否可以覆盖controller的model的同名项
- allow-request-override: false
- #req访问request
- request-context-attribute: req
- #后缀名freemarker默认后缀为.ftl,当然你也可以改成自己习惯的.html
- suffix: .ftl
- #设置响应的内容类型
- content-type: text/html;charset=utf-8
- #是否允许mvc使用freemarker
- enabled: true
- #是否开启template caching
- cache: false
- #设定模板的加载路径,多个以逗号分隔,默认: [“classpath:/templates/”]
- template-loader-path: classpath:/templates/
- #设定Template的编码
- charset: UTF-8
- # 设置静态文件路径,js,css等
- mvc:
- static-path-pattern: /static/**
File --> Settings
File --> Settings
2.4.1 在pom.xml引入springweb的相关依赖包
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
2.4.2 新建一个freemaker模板
src/man/resources/templates目录下新建一个freemarker模板
2.4.3 编写Controller层代码 名称:IndexController
- package com.jmh.springboot03.controller;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.servlet.ModelAndView;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * @author 蒋明辉
- * @data 2022/9/29 12:40
- */
- @Controller
- public class IndexController {
-
- @RequestMapping("/") //里面路径为/ 会在启动项目之后自动访问此方法
- public ModelAndView index(){
- //实例一个ModelAndView
- //作用即可作为作用域保存数据也可以作为页面跳转等等...
- ModelAndView model=new ModelAndView();
- model.setViewName("index");//跳转视图名
- return model;
- }
-
- }
2.4.4 进入主类启动服务器
2.4.5 在网页地址栏输入 localhost:8080/ 访问项目
- package com.jmh.springboot03.controller;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.servlet.ModelAndView;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * @author 蒋明辉
- * @data 2022/9/29 12:40
- */
- @Controller
- public class IndexController {
-
- //定义一个内部类
- @Data
- @AllArgsConstructor
- public class student{
- private Integer id;
- private String name;
- private float price;
- }
-
- @RequestMapping("/")
- public ModelAndView index(){
- ModelAndView model=new ModelAndView();
- model.addObject("name","蒋明辉");
- model.addObject("sex","男");
- //数组
- model.addObject("arrays",new String[]{"zs","ls","ww"});
- //集合
- List
studentList=new ArrayList<>(); - studentList.add(new student(1,"zs",12f));
- studentList.add(new student(2,"ls",13f));
- studentList.add(new student(3,"ww",14f));
- studentList.add(new student(4,"ff",15f));
- model.addObject("list",studentList);
-
-
-
- model.setViewName("index");
- return model;
- }
-
- }
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>title>
- head>
- <body>
- <h1>springboot渲染技术 CTO${name!'董事长'}h1>
- <h3>1)exists用在逻辑判断;h3>
- <#if name?exists>
- ${name}
- #if>
- <h3>2)if_exists用来打印东西h3>
- ${name?if_exists}
- <h3>3)??是判断对象是否为空h3>
- <#if name??>
- ${name}
- #if>
- <h3>4)if条件h3>
- <#if sex='男'>
- 是男的
- <#elseif sex='女'>
- 是女的
- <#else >
- 是人妖
- #if>
- <h3>5)循环01 数组h3>
- <#list arrays as it>
- ${it}
- #list>
- <h3>6)循环02 集合h3>
- <#list list as li>
- 编号:${li.id}
- 名称:${li.name}
- 价格:${li.price}
- #list>
- <h3>7)include全局变量&局部变量h3>
- <#include "common/head.ftl">
- 局部变量:${o}
- 全局变量:${t}
- <div style="margin-bottom: 300px">div>
- body>
- html>
每一个freemarker语法都标有提示注释 想了解的就去好好的看一下就明白 简单易懂