SpringBoot 学习笔记

一、Springboot 自动转配原理

1、@Controller 与@RestController 的区别

  • 页面跳转使用 @Controller,使用@Controller注解,需要返回数据的方法要加@ResponseBody
  • 获取数据使用 @RestController,相当于@ResponseBody + @Controller合在一起的作用
  • @Controller 与@RestController 都只能使用在class上,不能使用在方法上

在这里插入图片描述

二、thymeleaf 模板引擎(了解,建议学习VUE)

1、增加 spring-boot-starter-thymeleaf 依赖:

  • spring-boot-starter-thymeleaf:自动装配 Thymeleaf 模板引擎
	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

2、YML配置 Thymeleaf属性

application.yml

spring:
  thymeleaf:
    cache: false                  # 是否开启模板缓存,默认为:true,开发时关闭缓存,不然没法看到实时页面!
    mode: HTML                    # 指定模板的模式,默认为:HTML
    encoding: UTF-8               # 指定模板的编码,默认为:UTF-8
    prefix: classpath:/templates/ # 指定模板的前缀,默认为:classpath:/templates/
    suffix: .html                 # 指定模板的后缀,默认为:.html
    servlet:
      content-type: text/html     # 指定 Content-Type 值,默认为:text/html

3、引入命名空间:

<html  xmlns:th="http://www.thymeleaf.org">

4、简单表达式

4.1 ${…} 变量表达式 :

使用方法:直接使用th:xx = “${}” 获取对象属性 。例如:

	<form id="userForm">
	    <input id="id" name="id" th:value="${user.id}"/>
	    <input id="username" name="username" th:value="${user.username}"/>
	    <input id="password" name="password" th:value="${user.password}"/>
	</form>
	
	<div th:text="hello"></div>
	
	<div th:text="${user.username}"></div>

4.2 *{…} 选择变量表达式:

使用方法:首先通过th:object 获取对象,然后使用th:xx = "*{}"获取对象属性

	<form id="userForm" th:object="${user}">
	    <input id="id" name="id" th:value="*{id}"/>
	    <input id="username" name="username" th:value="*{username}"/>
	    <input id="password" name="password" th:value="*{password}"/>
	</form>

4.3 #{…} 消息表达式:

通常用于的获取国际化属性:th:xx="#{} "用于获取国际化语言翻译值。例如:

	<title th:text="#{user.title}"></title>

4.4 @{…} 链接url表达式

使用方法:通过链接表达式@{}直接拿到应用路径,然后拼接静态资源路径。例如:

	<a th:href="@{http://www.baidu.com}">跳转百度</a>
	<script th:src="@{/webjars/jquery/jquery.js}"></script>
	<link th:href="@{/webjars/bootstrap/css/bootstrap.css}" rel="stylesheet" type="text/css">

三、LOMBOK 结构化插件

  1. 创建实体类:
	@Data
	@AllArgsConstructor		//全参构造函数
	@NoArgsConstructor		//无参构造函数
	public class Deptment {
	    private Integer id;
	    private String deptName;
	}
  1. 初始化实体:(使用全参注释@AllArgsConstructor 效果)

在这里插入图片描述

四、i18n国际化

  1. 创建国际化i18n包
  2. 创建国际语言
  3. 定义国际化字段
  4. 配置国际化
  5. 使用国际化地段,使用#{ }

IDEA 创建i18n的文件夹,并创建国际化语言
在这里插入图片描述
使用idea 的可视化Resource bundle 工具定义国际化字段
在这里插入图片描述
配置国际化文件

spring:
  messages:
    basename: i18n.index         #配置国际化i18n文件位置

使用thymeleaf语法使用国际化配置

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<body>
    <h1 th:text="#{ index.uesr}"></h1>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值