1、简介
Springboot是一个用来把spring相关技术和第三方技术整合的框架
2、创建
2.1 利用maven
<!--配置SpringBoot父工程的坐标,继承SpringBoot父类的(pom文件)-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
</parent>
<!--导入springboot包-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.0</version>
</dependency>
-
创建一个sptingboot启动类
- @EnableAutoConfiguration 使用这个注解,SpringBoot下的所有项目,都会启用这个类
- @Configuration 用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器
- @ComponentScan 扫描包
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class StartwebBoot {
public static void main(String[] args) {
SpringApplication.run(StartwebBoot.class,args);
}
}
@RestController
public class ConfingHello {
@RequestMapping(value = "/helloText")
public String helloText(){
return "SpringBoot Hello";
}
}
2.2 利用IDEA 自带的 Spring Initializr创建
- 创建好以后,会自动形成一个类,而这个类和第一种创建的StartwebBoot.class功能一样
package com.bdit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//@EnableAutoConfiguration
//@Configuration
//@ComponentScan
@SpringBootApplication
public class SpringbootDom2Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootDom2Application.class, args);
}
}
- 之后再创建一个测试类
package com.bdit.confing;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Package: com.bdit.confing
* @ClassName: ConfingHello
* @Author: Ling
* @Date: 2020/11/27 10:46
* @Description:
*/
@RestController
public class ConfingHello {
@RequestMapping(value = "/hell0")
public String helloText(){
return "hello";
}
}
- 在浏览器输入 :http://localhost:8080/hell0 即可访问到
2.3 注意
- 测试类中注解为:@RestController,而不是:@Controller
- 也可以:@ResponseBody + @Controller合在一起
- 修改SpringBoot运行时,自动在控制台生成的图标方法:在src/main/resources 创建一个 banner.txt 在里面输入自己想输入的即可,也可以什么不写
- 在使用IDEA自带的创建时:如果创建成功但无法运行,可以通过右击项目,查看其项目是否是一个maven,然后再手动导入最后刷新一下maven即可
3、SpringBoot原理
3.1 starters原理
- starters原理
- starters是依赖关系的整理和封装,它是一套以来坐标的整合,可以让导入应用开发的依赖坐标更方便利用依赖传递的特征,进行依赖打包
3.2 依赖管理
- 继承 spring-boot-starter-parent 的好处和特点
- 默认编译 :Java1.8
- 默认编码:UTF-8
- 通过spring-boot-dependencies的pom管理所有公共的starter依赖版本
- spring boot dependencies 通过maven的一个dependencyManagement标签来实现版本的管理
- 随用随取,不用继承于符类所有的starter依赖
4、SpeingBoot 的配置文件
4.1 三种配置
- properties 配置文件
server.port=8088
server.address=127.0.0.1
- xml
<server>
<port>8088</port>
<address>127.0.0.1</address>
</server>
- yml/yaml
server:
port: 8089
address: 127.0.0.1
- SpringBoot采用约定大于配置的策略,所有很多配置都是默认值,如果想修改默认配置,可以使用 application.properties或application.yml(application.yaml)定义配置。SpringBoot默认从Resource目 录加载自定义配置文件。application.properties是键值对类型。application.yml是SpringBoot中一种新 的配置文件方式。
4.2 yml/ymal配置文件
- YML文件格式是YAML(YAML Ain’t a Markup Language)编写的文件格式,可以直观的识别,容易与脚本语言的交互,支持各种的编程语言,它以数据为核心,比xml更简洁,扩展名为 .yml 或 .yaml
4.3 配置文件语法
- 大小写敏感
- 数据值前边必须有空格,作为分隔符
- 使用缩进表示层级关系
- 缩进不允许使用tab,只允许空格
- 缩进的空格数不重要,只有相同层级的元素左对齐即可
- #表示注释,从这个字符一直到行尾,都会被解析器忽略
- 数值和集合使用 “-”表示数组每个元素
4.3.1 yml/yaml 案例
- 单个
# 单引号忽略转义字符
message: 'hello \n world'
# 双引号识别转移字符
message2: "hello \n world"
- 对象(map)键值对集合
person:
name: lisi
age: 31
address: beijing
#行内写法
person2: {name:zhangsan,age:33,address:beijing}
- 数组:一组按次序排列的值
city:
- beijing
- shanghai
- jinan
- 集合
students:
- name: zhangsan
age: 18
- name: lisi
age: 22
- name: wangwu
age: 25
- 配置引用
name: haha
teacher:
name: ${name}
5、配置文件属性注入Bean
- 使用注解@Value映射
- @value注解将配置文件的值映射到Spring管理的Bean属性值
@Component
public class Stance {
@Value("${stance.name}")
private String name;
@Value("${stance.password}")
private String password;
......
}
- 使用注解@ConfigurationProperties(prefix=“配置文件中的key的前缀”)可以将配置文件中的配置自动与实体类中进行映射
- 使用@ConfigurationProperties 方式必需提供Setter方法,使用@Value注解不需要setting方法
@Component
@ConfigurationProperties(prefix = "streand")
public class Streand implements Serializable {
private String name;
private String password;
......
}
- application.yml
streand:
name: zhangsan
password: 1234
stance:
name: lisi
password: 1234
6、Springboot整合mybatis
6.1 基于配置文件
- pom.xml
<?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.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bdit</groupId>
<artifactId>springboot_dom4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_dom4</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- sqlMapper映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bdit.mapper.IAccountMapper">
<insert id="save" parameterType="accountstr">
INSERT INTO accountstr(name,password,sex,xueli) VALUES(#{name},#{password},#{sex},#{xueli})
</insert>
<delete id="delete" parameterType="int">
DELETE FROM accountstr WHERE id=#{id}
</delete>
<update id="update" parameterType="accountstr">
UPDATE accountstr SET name=#{name},password=#{password},sex=#{sex},xueli=#{xueli} WHERE id=#{id}
</update>
<select id="queryById" parameterType="int" resultType="accountstr">
SELECT <include refid="accountableIn"/> FROM accountstr WHERE id = #{id}
</select>
<select id="queryAll" resultType="accountstr">
SELECT <include refid="accountableIn"/> FROM accountstr
</select>
<select id="queryxx" resultType="accountstr" parameterType="string">
SELECT * FROM accountstr WHERE name=#{arg0} and password=#{arg1}
</select>
<sql id="accountableIn">
id,name,password,sex,xueli
</sql>
</mapper>
- Accountstr
package com.bdit.pojo;
import lombok.Data;
import java.io.Serializable;
/**
* @Package: com.bdit.pojo
* @ClassName: Accountstr
* @Author: Ling
* @Date: 2020/11/26 12:07
* @Description:
*/
@Data
public class Accountstr implements Serializable {
private int id;
private String name;
private String password;
private int sex;
private int xueli;
}
- IAccountMapper
package com.bdit.mapper;
import com.bdit.pojo.Accountstr;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @Package: com.bdit.mapper
* @ClassName: IAccountMapper
* @Author: Ling
* @Date: 2020/11/19 22:58
* @Description:
*/
@Component
public interface IAccountMapper {
/**
* 增加
* @param account
* @return
*/
public int save(Accountstr account);
/**
* 删除
* @param integer
* @return
*/
public int delete(Integer integer);
/**
* 修改
* @param account
* @return
*/
public int update(Accountstr account);
/**
* 按照ID查询
* @param integer
* @return
*/
public Accountstr queryById(Integer integer);
/**
* 查询全部
* @return
*/
public List<Accountstr> queryAll();
public Accountstr queryxx(String name,String password);
}
- IAccountService
package com.bdit.service;
import com.bdit.pojo.Accountstr;
import java.util.List;
/**
* @Package: com.bdit.service
* @ClassName: IAccountService
* @Author: Ling
* @Date: 2020/11/19 23:35
* @Description:
*/
public interface IAccountService {
/**
* 增加
* @param account
* @return
*/
public boolean save(Accountstr account);
/**
* 删除
* @param integer
* @return
*/
public boolean delete(Integer integer);
/**
* 修改
* @param account
* @return
*/
public boolean update(Accountstr account);
/**
* 按照ID查询
* @param integer
* @return
*/
public Accountstr queryById(Integer integer);
/**
* 查询全部
* @return
*/
public List<Accountstr> queryAll();
public Accountstr queryxx(String name,String password);
}
- AccountServiceImpl
package com.bdit.service.impl;
import com.bdit.mapper.IAccountMapper;
import com.bdit.pojo.Accountstr;
import com.bdit.service.IAccountService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @Package: com.bdit.service.impl
* @ClassName: AccountServiceImpl
* @Author: Ling
* @Date: 2020/11/19 23:50
* @Description:
*/
@Service(value = "/accountServiceImpl")
public class AccountServiceImpl implements IAccountService {
@Resource
private IAccountMapper accountMapper;
@Override
public Accountstr queryxx(String name, String password) {
Accountstr accountstr = accountMapper.queryxx(name,password);
return accountstr;
}
@Override
public boolean save(Accountstr account) {
int a = 0;
a = accountMapper.save(account);
if (a>0){
return true;
}else {
return false;
}
}
@Override
public boolean delete(Integer integer) {
int a = 0;
a = accountMapper.delete(integer);
if (a>0){
return true;
}else{
return false;
}
}
@Override
public boolean update(Accountstr account) {
int a = 0;
a = accountMapper.update(account);
if (a>0){
return true;
}else{
return false;
}
}
@Override
public Accountstr queryById(Integer integer) {
Accountstr account = null;
account = accountMapper.queryById(integer);
return account;
}
@Override
public List<Accountstr> queryAll() {
List<Accountstr> accountList = null;
accountList = accountMapper.queryAll();
return accountList;
}
}
- ControerText
package com.bdit.controller;
import com.bdit.pojo.Accountstr;
import com.bdit.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Package: com.bdit.controller
* @ClassName: ControerText
* @Author: Ling
* @Date: 2020/11/27 21:07
* @Description:基于半注解半xml的springboot联合mybatis
* http://localhost:8080/queryAll
*/
@RestController
public class ControerText {
@Autowired
@Qualifier(value = "/accountServiceImpl")
private IAccountService accountService;
@RequestMapping(value = "/queryAll")
public List<Accountstr> queryAll(){
List<Accountstr> accountList = accountService.queryAll();
for (Accountstr acc : accountList){
System.out.println(acc);
}
return accountList;
}
@RequestMapping(value = "/queryById/{id}")
public Accountstr queryById(@PathVariable("id") Integer integer){
Accountstr accountstr = accountService.queryById(integer);
return accountstr;
}
// http://localhost:8080/delete/1
@RequestMapping(value = "/delete/{id}")
public int delete(@PathVariable("id") Integer integer){
boolean bool = accountService.delete(integer);
if (bool){
return 1;
}else{
return 0;
}
}
}
- application.yml
server:
port: 8080
spring:
datasource:
username: root
password: 1234
url: jdbc:mysql://localhost:3306/acc_vue?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.bdit.pojo
6.2 基于注解但不要sqlMapper映射文件
方法:
- 在每一个mapper接口上添加@Mapper 注解
- 在启动类上添加 @MapperScann(basePack=“包名.mapper”)
例:
- IAccountMapper(mapper接口)
package com.bdit.mapper;
import com.bdit.pojo.Accountstr;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.annotation.Resources;
import java.util.List;
/**
* @Package: com.bdit.mapper
* @ClassName: IAccountMapper
* @Author: Ling
* @Date: 2020/11/19 22:58
* @Description:
*/
@Mapper
@Component
public interface IAccountMapper {
/**
* 增加
* @param account
* @return
*/
@Insert("INSERT INTO accountstr(name,password,sex,xueli) VALUES(#{name},#{password},#{sex},#{xueli})")
public int save(Accountstr account);
/**
* 删除
* 如果 属性名不一致则可以使用 @Results
* @param integer
* @return
*/
@Delete("DELETE FROM accountstr WHERE id=#{id}")
@Results({
@Result(property = "id",column = "id"),
@Result(property = "name",column = "name")
})
public int delete(Integer integer);
/**
* 修改
* @param account
* @return
*/
@Update("UPDATE accountstr SET name=#{name},password=#{password},sex=#{sex},xueli=#{xueli} WHERE id=#{id}")
public int update(Accountstr account);
/**
* 按照ID查询
* @param integer
* @return
*/
@Select("SELECT id,name,password,sex,xueli FROM accountstr WHERE id = #{id}")
public Accountstr queryById(Integer integer);
/**
* 查询全部
* @return
*/
@Select("SELECT * FROM accountstr")
public List<Accountstr> queryAll();
@Select("SELECT * FROM accountstr WHERE name=#{arg0} and password=#{arg1}")
public Accountstr queryxx(String name,String password);
}
7、 SpringBoot整合Thymeleaf
7.1 Thymeleaf
- Thymeleaf是一个跟Freemarker、Veloctiy类似的模板引擎,完全可以代替JSP,Thymeleaf支持HTML原型,通过Thymeleaf特殊标签可以基本实现JSP中动态数据的展示效果
7.2 配置
- 引入依赖(pom.xml)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
-
Thymeleaf支持html,但需要遵循一定规则:
- 在SpringBoot中,讲Thymeleaf渲染的页面一般放在:src\main\resources\templates 下,这样Thymeleaf会自动识别
- 在SpringBoot项目中resources/static目录下的文件可以直接通过浏览器访问,而 resources/templates下的HTML不能通过浏览器直接访问,而需要Spring MVC框架映射到相应的 页面地址。
-
修改 application.yml
spring: thymeleaf: mode: LEGACYHTML5 encoding: utf-8 servlet: content-type: text/html suffix: .html # 开发时关闭缓存,要不没法看到页面 cache: false
- 其中spring.thymeleaf.mode=LEGACYHTML5 配置thymeleaf的模式,不要使用spring.thymeleaf.mode=HTML5,因为严格遵循HTML5规范会非常严格的,
-
在需要使用 Thymeleaf 的页面引入
7.3 示例:
package com.bdit.controller;
import com.bdit.pojo.Accountstr;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Package: com.bdit.controller
* @ClassName: ControllerText
* @Author: Ling
* @Date: 2020/11/28 11:14
* @Description:http://localhost:8080/context1
*/
@Controller
public class ControllerText {
@RequestMapping(value = "/context1", method = RequestMethod.GET)
public String context1(Model model){
return "index";
}
}
【return “index” 中 index对应的是:src\main\resources\templates下的index.html 文件】
7.3.1 获取
7.3.1.1 普通数据类型
- java
@Controller
public class ControllerText {
@RequestMapping(value = "/context1", method = RequestMethod.GET)
public String context1(Model model){
//普通数据类型
model.addAttribute("usual","测试");
return "index";
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<p>普通</p>
<p th:text="${usual}"></p>
<hr/>
</body>
7.3.1.2 对象、循环、Map遍历
- Accountstr.java
package com.bdit.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
/**
* @Package: com.bdit.pojo
* @ClassName: Accountstr
* @Author: Ling
* @Date: 2020/11/26 12:07
* @Description:
*/
@Data
@AllArgsConstructor
public class Accountstr implements Serializable {
private int id;
private String name;
private String password;
private int sex;
private int xueli;
}
- ControllerText.java
package com.bdit.controller;
import com.bdit.pojo.Accountstr;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Package: com.bdit.controller
* @ClassName: ControllerText
* @Author: Ling
* @Date: 2020/11/28 11:14
* @Description:http://localhost:8080/context1
*/
@Controller
public class ControllerText {
@RequestMapping(value = "/context1", method = RequestMethod.GET)
public String context1(Model model){
//对象
Accountstr accountstr = new Accountstr(8,"eight","1269",1,3);
model.addAttribute("accountsrt",accountstr);
//循环
List<Accountstr> list = new ArrayList<>();
Accountstr accountstr1 = new Accountstr(1,"eight1","12691",1,3);
Accountstr accountstr2 = new Accountstr(2,"eight2","12692",2,4);
Accountstr accountstr3 = new Accountstr(3,"eight3","12693",1,5);
list.add(accountstr1);
list.add(accountstr2);
list.add(accountstr3);
model.addAttribute("list",list);
//Map遍历
Map<String,Accountstr> map = new HashMap<>();
map.put("111",new Accountstr(4,"eight4","12694",1,4));
model.addAttribute("map",map);
return "index";
}
}
- index.html
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<p>对象</p>
<hr/>
<p th:text="'编号:'+${accountsrt.id}"></p>
<p th:text="'姓名:'+${accountsrt.name}"></p>
<p th:text="'密码:'+${accountsrt.password}"></p>
<p th:text="'性别:'+${accountsrt.sex}"></p>
<p th:text="'学历:'+${accountsrt.xueli}"></p>
<hr/>
<!--或者-->
<div th:object="${accountsrt}">
<span th:text="'编号:'+ *{id}"></span>
<span th:text="'姓名:'+ *{name}"></span>
<span th:text="'密码:'+ *{password}"></span>
<span th:text="'性别:'+ *{sex}"></span>
<span th:text="'学历:'+ *{xueli}"></span>
</div>
<hr/>
<p>字符串拼接</p>
<p th:text="'hello'+${accountsrt.name}">
<hr/>
<p>if/unless条件判断</p>
<span th:text="${accountsrt.sex}"></span>
<span th:if="${accountsrt.sex == 1}">男</span>
<span th:unless="${accountsrt.sex == 2}">女</span>
<hr/>
<p>each循环</p>
<table>
<tr>
<th>编号</th>
<th>ID</th>
<th>姓名</th>
<th>密码</th>
<th>性别</th>
<th>学历</th>
</tr>
<tr th:each="item,property:${list}">
<td th:text="${property.index}"></td>
<td>
<span th:text="${item.id}"></span>
</td>
<td>
<span th:text="${item.name}"></span>
</td>
<td>
<span th:text="${item.password}"></span>
</td>
<td>
<span th:text="${item.sex}"></span>
</td>
<td>
<span th:text="${item.xueli}"></span>
</td>
</tr>
</table>
<!--
property 称作状态变量,属性有:
index:当前迭代对象的index(从0开始计算)
count: 当前迭代对象的index(从1开始计算)
size:被迭代对象的大小
current:当前迭代变量
even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)
first:布尔值,当前循环是否是第一个
last:布尔值,当前循环是否是最后一个
-->
<hr/>
<p>each循环</p>
<table>
<tr>
<!-- <th>编号</th>-->
<th>ID</th>
<th>姓名</th>
<th>密码</th>
<th>性别</th>
<th>学历</th>
</tr>
<tr th:each="item:${list}">
<td>
<span th:text="${item.id}"></span>
</td>
<td>
<span th:text="${item.name}"></span>
</td>
<td>
<span th:text="${item.password}"></span>
</td>
<td>
<!--<span th:if="${item.sex}==1">男</span>
<span th:unless="${item.sex}==2">女</span>
<span th:text="${item.sex}"></span>-->
<div th:switch="${item.sex}">
<span th:case="1">男</span>
<span th:case="2">女</span>
<span th:case="*">未知</span>
</div>
</td>
<td>
<div th:switch="${item.xueli}">
<span th:case="1">高中</span>
<span th:case="2">大学</span>
<span th:case="3">硕士</span>
<span th:case="4">研究生</span>
<span th:case="*">未知</span>
</div>
<!-- <span th:text="${item.xueli}"></span>-->
</td>
</tr>
</table>
<!--<hr/>
<p>Map遍历</p>
<span th:each="item:${map}">
<span th:text="${item.key}"></span>
<span th:text="${item.value.name}"></span>
</span>-->
</body>
</html>
7.3.1.3 Thymeleaf 可以进行URL表达式
- 效果一样
- 也可以:
7.3.1.4 表达式支持
-
字面
- 文本、数字、布尔、空、文字标记
-
文本操作
- 字符串连接、文本替换
-
算术运算
- +、-、*、/、%
-
布尔操作
- and、or、!、not
-
比较和等价
- 比较:>、<、>=、<=(gt、lt、ge、le)
- 等值运算符:==、!= (eq、ne)
-
条件运算符
- if-then:(if)?(then)
- if-then-else:(if)?(then):(else)
- Default:(value)?:(defaultvalue)
-
这些都可组合并嵌套
'User is of type ' + (${user.isAdmin()} ? 'Administrator' : ({user.type} ?: 'Unknown'))
7.3.1.5 内联JS
有时,我们需要在 javascript 中使用 model 中的元素,而在 javascript 中无法用 th 标签,那么就可 以这样使用:
<b>内联JS</b>
<script th:inline="javascript">
var username = [[${user.username}]];
console.log(username);
</script>
使用内联JS,要在
7.3.1.6 内嵌变量
Thymeleaf提供了一系列的Utility对象(内置于Context中)可以通过#访问
dates : java.util.Date的功能方法类。
calendars : 类似#dates,面向java.util.Calendar
numbers : 格式化数字的功能方法类
strings : 字符串对象的功能类,contains,startWiths,prepending/appending等等。
objects: 对objects的功能类操作。
bools: 对布尔值求值的功能方法。
arrays:对数组的功能类方法。
lists: 对lists功能类方法
8、使用Thymeleaf布局
8.1 介绍
- 一个页面通常可以分为:1、头部 2、侧边栏 3、主要区域 4、尾部
- 其实换分的几部分仅仅是将不同的
标签存放在不同的html中
- 因为子页面无需html的结构,也就没有这个标签库,直接在子页面中用 th: 可能IDE会报 错,但是实际上是语法正确,因为在 main.html 中使用了这个标签库。 另外,也可以这样使用避免IDE的报错:
8.2 常见布局
- 在主 html 中引入其它子页面
<div th:replace="common/common-css"></div>
<div th:replace="common/header"></div>
<div th:replace="common/side"></div>
- 使用iframe布局,如果嵌套的页面
使用 <frameset>、<iframe>,就可以嵌套