目录
一、Freemarker入门
1,新建一个springboot项目
2,配置freemarker
二、Freemarker的基本语法
1、application.yml
mybatis:
mapper-locations: classpath:mappers/*xml
type-aliases-package: com.xnx.spboot04.model
server:
port: 8080
servlet:
context-path: /spboot04
spring:
application:
name: spboot04
datasource:
driver-class-name: com.mysql.jdbc.Driver
name: defaultDataSource
password: 123456
url: jdbc:mysql://localhost:3306/t280?useUnicode=true&characterEncoding=UTF-8
username: root
freemarker:
cache: false
charset: utf-8
expose-request-attributes: true
expose-session-attributes: true
suffix: .ftl
template-loader-path: classpath:/templates/
mvc:
static-path-pattern: /static/**
# resources:
# static-locations: classpath:/static/# Ӧ�÷��� WEB ���ʶ˿�
pagehelper:
reasonable: true
supportMethodsArguments: true
page-size-zero: true
helper-dialect: mysql
logging:
level:
com.xnx.spboot04: debug
2、IndexController
package com.zhoujuan.spboot04.controller;
import com.zhoujuan.spboot04.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Arrays;
/**
* @author zhoujuan
* @create 2022-11-01 17:45
*/
@Controller
public class IndexController {
// @RequestMapping("/")
// public ModelAndView index(){
springmvc
// ModelAndView mv = new ModelAndView();
// mv.addObject("uname","aa");
// mv.setViewName("index");
// System.out.println("come in");
// return mv;
// }
@RequestMapping("/")
public String index(Model model){
model.addAttribute("sex","mimi");
// model.addAttribute("uname","aa");
model.addAttribute("users", Arrays.asList(new User(1,"zs"),
new User(2,"ls"),new User(3,"ww")));
model.addAttribute("arr",new Integer[]{3,4,5,6,7,8,9});
System.out.println("come in");
return "index";
}
}
3、head.ftl
<#assign ctx>
${springMacroRequestContext.contextPath}
</#assign>
<#global ctx2>
${springMacroRequestContext.contextPath}
</#global>
4、index.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h3>取值</h3>
<p>1.1提供默认值</p>
<#--当接收的参数不为空会报错。这个问题是需要避免的-->
<#--${uname}-->
<#--{uname !}-->
${uname ! 'bb'}
<br>
<p>1.2对null先进行判断</p>
1)exist用在逻辑判断
<#if uname?exists>
uname}
</#if>
<br>
2)??判断对象是否为空 springboot项目需要对查询的结果进行遍历,那么遍历之前必须判空
<#if uname??>
${uname}
</#if>
<br>
3)if_exist打印东西
${uname?if_exists}
<br>
<h3>条件</h3>
<#if sex="nv">
女
<#elseif sex="nan">
男
<#else>
未知
</#if>
<h3>循环</h3>
1)取出数组中的元素
<#list arr as a>
${a} /
</#list>
<br>
2)取出集合中的对象
<#list users as a>
${a.id}:${a.name}
</#list>
<br>
<h3>4.include</h3>
<#include '/head.ftl'>
<h3>5.局部变量/全局变量</h3>
${ctx} : ${ctx2}
<br>
</body>
</html>
5、测试
三、Freemarker综合案例
1、PagerAspect
package com.zhoujuan.spboot04.aspect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhoujuan.spboot04.util.PageBean;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Aspect
public class PagerAspect {
/**
* *:返回值类型
* *..:无限包
* *Service:以Service结尾接口名
* *Pager:以Pager方法
* 只要同时匹配上诉四个条件,就会被列为目标对象
* 上述配置要生效,代理注解<aop:aspectj-autoproxy/>不能少
* @param args
* @return
* @throws Throwable
*/
@Around("execution(* *..*Biz.*Pager(..))")
public Object invoke(ProceedingJoinPoint args) throws Throwable {
Object[] params = args.getArgs();
PageBean pageBean = null;
for (Object param : params) {
if(param instanceof PageBean){
pageBean = (PageBean)param;
break;
}
}
if(pageBean != null && pageBean.isPagination())
PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
// 执行目标方法
Object list = args.proceed(params);
if(null != pageBean && pageBean.isPagination()){
PageInfo pageInfo = new PageInfo((List) list);
pageBean.setTotal(pageInfo.getTotal()+"");
}
return list;
}
}
2、ClazzBizImpl
package com.zhoujuan.spboot04.biz.impl;
import com.zhoujuan.spboot04.biz.ClazzBiz;
import com.zhoujuan.spboot04.mapper.ClazzMapper;
import com.zhoujuan.spboot04.model.Clazz;
import com.zhoujuan.spboot04.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class ClazzBizImpl implements ClazzBiz {