重点:
0、回顾;
1、springboot之thymeleaf模板
2、springboot之freemarker模板
重点解析:
0、回顾加补充
0.1、springboot入门中新建spring boot项目时三个坑:
(0.1.1)、必须联网,不然无法填写项目名,无法完成项目创建。
(0.1.2)、创建springboot项目名的时候是需要小写的,不然无法进行下一步。
(0.1.3)、在进行项目创建最后一步的时候不要掉以轻心。因为第一遍可能必须创建成功。将项目删除之后重新创建一次基本上就没有多大的问题。如果在pom.xml里面还是报错,就在本地仓库找到相关依赖删除,然后重新下载一遍也可以。
0.2、application回顾
0.2.1、内置属性,直接在applocation里面添加
server.port
server.servlet.context-path:
0.2.2、自定义属性在application声明,但是必须是 key=value
mysql.url
mysql.driver
在java里面必须要通过@value("${key}"),获取里面的值
0.2.3、属性封装类 注意必须要在实体类做添加@component和
@configuationProperties(“prefix=mysql”)
spring boot模块共4个:Thymeleaf、Freemarker、Mustache、Groovy Templates。 前面两个使用率比较高,基本后面两个没什么人使用,就不进行进解了。如果要使用springboot模块请点击Template Engines如下面图中进行选择模块。
1、springboot之thymeleaf模板
关于Thymeleaf的优点,我只说一条:它就是html页面。下面直接上代码
相关pom依赖
org.springframework.boot
spring-boot-starter-thymeleaf
Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行
spring.thymeleaf.cache=false
正式环境还是要将缓存开启的
实体类方法:user.java
public class User {
private Integer uid;
private String uname;
private String upwd;
public User(){
}
public User(Integer uid, String uname, String upwd) {
this.uid = uid;
this.uname = uname;
this.upwd = upwd;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
}
代码分享:IndexController.java
@RequestMapping("/user/list")
public ModelAndView userlist(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("/user/list");
modelAndView.addObject("title","用户列表");
List list=new ArrayList<>();
list.add(new User(1,"张晨光","6789"));
list.add(new User(2,"韩伊婷","5675678"));
modelAndView.addObject("users",list);
return modelAndView;
}
代码分享:list.html
id
用户名
密码
id
用户名
密码
2、springboot之freemarker模板
导入pom依赖
org.springframework.boot
spring-boot-starter-freemarker
application.yml文件的默认配置
spring:
thymeleaf:
cache: false
freemarker:
# 设置模板后缀名
suffix: .ftl
# 设置文档类型
content-type: text/html
# 设置页面编码格式
charset: UTF-8
# 设置页面缓存
cache: false
# 设置ftl文件路径,默认是/templates,为演示效果添加role
template-loader-path: classpath:/templates/role
mvc:
static-path-pattern: /static/**
IndexController.java代码分享:
@RequestMapping("/role/list")
public ModelAndView rolelist() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("list");
modelAndView.addObject("name",“嘿嘿”);
modelAndView.addObject("sex","boy");
List list=new ArrayList<>();
list.add(new User(1,"张晨光","6789"));
list.add(new User(2,"韩伊婷","5675678"));
modelAndView.addObject("users",list);
return modelAndView;
}
@RequestMapping("/role/login")
public String rolelogin() {
return "login";
}
common.ftl代码分享:
<#-- assign 设置全局变量-->
<#assign ctx>
${springMacroRequestContext.contextPath}
#assign>
foot.ftl代码分享:
版本号
<#-- 第一个无法点击进去,因为springboot不允许直接点击,必须进行跳转才可以跳转进去 -->
登陆1
登陆2
login.ftl代码分享:
login
登陆
list.ftl 代码 :
角色列表
<#-- 静态页面的调用-->
<#include 'common.ftl'>
取值
<#-- 取值里面必须有值,如果没有或者是null就直接显示500。解决方法 ${值 ! '空'} ,这里的!就代表判断,后面的空是进行替换-->
welcome 【${name ! '空'}】 to page
非空判断
<#if name?exists>
里面有值
#if>
条件表达式
<#if sex=='boy'>
男
<#elseif sex=='girl'>
女
<#else>
#if>
循环
id
用户名
密码
<#list users as user>
${user.uid}
${user.uname}
${user.upwd}
#list>
include
<#include 'foot.ftl'>
变量(局部,全局)
<#assign ctxl>
${springMacroRequestContext.contextPath}
#assign>
<#global ctx2>
${springMacroRequestContext.contextPath}
#global>
${ctxl}.${ctx2}
第一步:点击setting,进入设置然后点击 file and code Templates ,注意左边出来的是includes,要点击files。
第二步:将html里面的内容进行复制,然后点击 +,将在html里面复制的内容,粘贴在最大空白里。注意要填写name,就是创建 .ftl的项目名称。e’xtension里面填写项目的后缀名。
第三步:点击Apply,就可以在新建里面了