知名网站建设定制【Springboot】SpringBoot基础知识及整合Thymeleaf模板引擎

文章目录

🌕博客x主页:🌕!
🌎文章说明:spring🌎
✅系列专栏:
🌴本篇内容:对SpringBoot知名网站建设定制进行一个入门学习及对知名网站建设定制模板引擎进行整合(知名网站建设定制对所需知识点进行选择阅读呀~)🌴
☕️每日一语:知名网站建设定制在人生的道路上,知名网站建设定制即使一切都失去了,知名网站建设定制只要一息尚存,知名网站建设定制你就没有丝毫理由绝望。知名网站建设定制因为失去的一切,知名网站建设定制又可能在新的层次上复得。☕️
🕤作者详情:知名网站建设定制作者是一名双非大三在校生,喜欢Java,知名网站建设定制欢迎大家探讨学习,知名网站建设定制喜欢的话请给博主一个三连鼓励。🕤
🚩 交流社区:(知名网站建设定制优质编程社区)

SpringBoot简介

SpringBoot是什么

①Spring Boot是由Pivotal知名网站建设定制团队提供的全新框架,知名网站建设定制其设计目的是用来简化新Spring知名网站建设定制应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

为什么要学习SpringBoot

一、SpringBoot能简化配置、从 2002 年开始,Spring 一直在飞速的发展,如今已经成为了在Java EE(Java Enterprise Edition)开发中真正意义上的标准,但是随着技术的发展,Java EE使用 Spring 逐渐变得笨重起来,大量的 XML 文件存在于项目之中。

SpringBoot的优势

1、减少开发,测试时间和努力。
2、使用JavaConfig有助于避免使用XML。
3、避免大量的Maven导入和各种版本冲突。
4、提供意见发展方法。
5、通过提供默认值快速开始开发。
6、没有单独的Web服务器需要。这意味着你不再需要启动Tomcat,Glassfish或其他任何东西。

学习SpringBoot前要具备的基础

声明:这不是在劝退,而是在告诉同志们,学习是一个循序渐进的过程,不可操之过急。一步一个脚印的走,才能到达终点站。

学习这个知识点你应该具备的一些基础知识:
基础篇:javase基础,包括但不限于:面向对象,封装,继承,多态,类与接口,集合,IO等等。
基础篇:Spring、SpringMVC:知道Spring是用来管理bean,能够基于Restful实现页面请求交互功能
基础篇:熟练掌握Mybatis

创建第一个SpringBoot项目

在Spring官方下创建SpringBoot项目

一、打开Spring官方—>projects—>SpringBoot—>Spring initializr;
或者点击看到以下图片:

按照上述图片完成后执行第7步会下载一个压缩包:

二、找到压缩包位置解压


三、导入创建好的SpringBoot项目
打开IEDA:file—>open:找到刚才文件解压位置,加载进来即可

或者直接把解压好的文件拖进IDEA也可以:

如图

使用IDEA创建SpringBoot项目

说明:我们一般是不会在Spring的官网上创建SpringBoot项目的,而是使用IDEA创建项目,因为IEDA为我们准备好了创建工具,这样的创建更加省时省力。

一、打开IEDA新建一个项目,就叫做SpringBoot:

二、在项目里新建一个modules,取名为SpringBoot-demo1

在上面点击Next之后,跳到下面图片

选中之后点击finish—>apply—>ok即可

三、编写测试程序
在Application尝试输出一段话,检查一下代码系统是否正确:

package com.example.springbootdemo1;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringBootDemo1Application {    public static void main(String[] args) {        SpringApplication.run(SpringBootDemo1Application.class, args);        System.out.println("鸡到底美不美?");    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15


很明显,我们创建的第一个SpringBoot成功了。

SpringBoot配置文件详解

属性配置

一、 我们可以看到,SpringBoot项目下的Resources目录下有一个application.properties的配置文件,SpringBoot通过配置文件application.properties就可以修改默认的配置,什么意思呢?举个例子,因为SpringBoot采用的是内嵌tomcat的方式,所以默认端口是8080,我现在通过application.properties配置文件把8080修改为8081:

server.port=8081
  • 1
  • 2

执行测试:可以看到端口号已经由8080变为8081

上述例子说明了application.properties配置文件可以更改SpringBoot提供的默认配置,设置成我们实际需要的配置。

配置文件分类

SpringBoot使用一个全局的配置文件,配置文件名是固定的(都必须是application.开头),支持三种格式(properties、yaml、yml),由于properties类型的我们已经用过很多,这里就不再过多赘述,这里介绍一下yaml或者yml。

一、创建application.yaml、application.yml文件
yml:

yaml:

可以看到两者的用法和格式完全一样,那么是不是说两个作用一样呢?答案是两者一样,所以在使用过程中,我们一般选择yml格式

二、yml、yaml的注意点

1、以空格的缩进来控制层级关系,左对齐的一列数据,属于同一个层级
2、yaml、yml 格式:k: v,之间的空格必须有
3、k 和 v 对大小写敏感
4、k: v,字符串默认不用加上单引号或者双引号
5、在这两个配置文件中#表示注释

三、yml中各种数据类型的使用

对象、map类:
普通写法:

 Practitioner:  name: kunkun  age: 3 
  • 1
  • 2
  • 3
  • 4
  • 5

行内写法:

Practitioner: {name: kunkun,age: 3}
  • 1
  • 2

数组:
普通写法:

#缩略格式Practitioner:  - name: kunkun    age:3    likes:      - sing      - dance      - rap      - basketball  - name: xiaoheizi    age: 4    likes:       - kunkun      - zhenxiatou      - suzhi666
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

行内写法:

Practitioner: [{name: kunkun,age: 3,hobby: [sing,dance,rap,basketball]},{name: xiaoheizi,age: 4,hobby: [kunkun,zhenxiatou,suzhi666]}]
  • 1
  • 2

四、配置文件读取

1、读取单个数据

package com.example.springbootdemo1.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.servlet.ModelAndView;/** * @author 不止于梦想 * @date 2022/10/21 21:12 */@RestControllerpublic class indexController {    @Value("${name}")    public String username;    @ResponseBody    @RequestMapping("/index")    public String welcomeView(){        ModelAndView mv = new ModelAndView();        mv.addObject("user",username);        System.out.println(username);        return "hello";    }}//配置文件name: kunkun
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

结果:
技巧:如果数据存在多层级,依次书写层级名称即可

注意:这里踩一个坑,我们在写配置文件的时候,一般都是key value键值对的,而key的值除非是你想要的,不然不要写成user,写成user系统会自动赋值为你系统的管理员:

#这里尝试验证user:  name: kunkunpackage com.example.springbootdemo1.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.servlet.ModelAndView;/** * @author 不止于梦想 * @date 2022/10/21 21:12 */@Controllerpublic class indexController {    @Value("${user.name}")    public String username;    @ResponseBody    @RequestMapping("/index")    public String welcomeView(){        ModelAndView mv = new ModelAndView();        mv.addObject("user",username);        System.out.println(username.toString());        return "hello";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

结果如下:

2、读取全部数据
提问:读取全部数据是使用@value标签给所有属性都注入值?
从上面我们可以看到,如果配置文件里的值是少量的,那么可以用@value标签给需要的属性赋值,但是在我们的日常开发中,配置文件的内容很多,一个一个注入得把我们累死!

解决方案:SpringBoot提供了一个对象,能够把所有的数据都封装到这一个对象中,这个对象叫做Environment,使用自动装配注解可以将所有的yaml数据封装到这个对象中

3、读取对象数据
因为Java是一个面向对象的语言,很多情况下,我们会将一组数据封装成一个对象。SpringBoot也提供了可以将一组yml对象数据封装一个Java对象的操作:

第一步:编写配置文件

student:  username: kunkun  age: 3  hobby: dance    
  • 1
  • 2
  • 3
  • 4
  • 5

第二步:编写一个对象类,并且将该对象类作为bean注入到Spring容器中,然后使用注解@ConfigurationProperties指定该对象加载哪一组yaml中配置的信息。

package com.example.springbootdemo1.pojo;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component//交给Spring容器保管@ConfigurationProperties(prefix = "student")//对应的application.yml配置文件的位置public class Student{    private String username;    private int age;    private String hobby;    @Override    public String toString() {        return "Student{" +                "username='" + username + '\'' +                ", age=" + age +                ", hobby='" + hobby + '\'' +                '}';    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getHobby() {        return hobby;    }    public void setHobby(String hobby) {        this.hobby = hobby;    }    public Student() {    }    public Student(String username, int age, String hobby) {        this.username = username;        this.age = age;        this.hobby = hobby;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

第三步,编写测试类

package com.example.springbootdemo1;import com.example.springbootdemo1.pojo.Student;import org.junit.jupiter.api.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestclass SpringBootDemo1ApplicationTests {    @Autowired    private Student student;    @Test    void contextLoads() {        System.out.println(student);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

测试结果如下:

总结:@value和@ConfigurationProperties都是获取配置文件信息的;@value常用在获取单个或者少量的配置文件,而后者常需要对应配置文件属性的封装类,即把文件属性映射写成一个封装的类,并把类交给spring管理(常配合Component使用)。然后使用@ConfigurationProperties添加前缀以对应起来配置文件的属性。

SpringBoot整合Thymeleaf

Thymeleaf简介

一、Thymeleaf是一个流行的模板引擎,该模板引擎采用Java语言开发,模板引擎是一个技术名词,是跨领域跨平台的概念,在Java语言体系下有模板引擎,在C#、PHP语言体系下也有模板引擎。除了thymeleaf之外还有Velocity、FreeMarker等模板引擎,功能类似

二、Thymeleaf的主要目标是将优雅的自然模板带到您的开发工作流程中—HTML能够在浏览器中正确显示,并且可以作为静态原型,从而在开发团队中实现更强大的协作。Thymeleaf能够处理HTML,XML,JavaScript,CSS甚至纯文本。

三、Thymeleaf的主要目标是提供一个优雅和高度可维护的创建模板的方式。 为了实现这一点,它建立在自然模板的概念之上,以不影响模板作为设计原型的方式将其逻辑注入到模板文件中。 这改善了设计沟通,弥合了前端设计和开发人员之间的理解偏差。

示例:Thymeleaf 通过在 html 标签中,增加额外属性来达到“模板+数据”的展示方式,示例代码如下。

<!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><!--引入thymeleaf命名空间--><head>    <meta charset="UTF-8">    <title>Title</title></head><body><!--th:text 为 Thymeleaf 属性,用于在展示文本--><h1 th:text="欢迎来到Thymeleaf">欢迎访问静态页面</h1></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

当直接使用浏览器打开时,浏览器展示结果如下。

欢迎来到Thymeleaf

当通过 Web 应用程序访问时,浏览器展示结果如下。

欢迎访问静态页面

整合

SpringBoot整合Thymeleaf需要三个步骤:

一、引入对应Thymeleaf的starter其实在以后的整合中,要整合什么技术,就引入该技术的starter即可:

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf --><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-thymeleaf</artifactId>    <version>2.7.3</version></dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二、创建模板文件,并放在指定目录下

2.1 由于 SpringBoot 为 thymeleaf 提供了一整套的自动化配置方案,我们几乎不用做任何更改就可以直接使用,如下是SpringBoot中thymeleaf的默认配置

@ConfigurationProperties(prefix = "spring.thymeleaf")public class ThymeleafProperties {	private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;	public static final String DEFAULT_PREFIX = "classpath:/templates/";	public static final String DEFAULT_SUFFIX = ".html";	/**	 * Whether to check that the template exists before rendering it.	 */	private boolean checkTemplate = true;	/**	 * Whether to check that the templates location exists.	 */	private boolean checkTemplateLocation = true;	/**	 * Prefix that gets prepended to view names when building a URL.	 */	private String prefix = DEFAULT_PREFIX;	/**	 * Suffix that gets appended to view names when building a URL.	 */	private String suffix = DEFAULT_SUFFIX;	/**	 * Template mode to be applied to templates. See also Thymeleaf's TemplateMode enum.	 */	private String mode = "HTML";	...}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

通过 org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties 找到 SpringBoot 中 thymeleaf 的默认配置:

 @ConfigurationProperties //这个注解熟不熟悉,这不就是上面的读取配置文件注入的注解吗?DEFAULT_ENCODING :默认字符编码,为utf-8DEFAULT_PREFIX  默认前缀表达式,值为"classpath:/templates/",即类路径下的templates,对应了SpringBoot项目下的Resource下的templates。DEFAULT_SUFFIX 后缀表达式,默认为.html
  • 1
  • 2
  • 3
  • 4
  • 5

从上面代码可以看出,thymeleaf 模板文件默认放在 resources/templates 目录下,默认的后缀是 .html。
当然,我们有时候不想用默认配置,因为需求是多样的,这个时候SpringBoot也是提供了个性化的方法供我们使用。
在SpringBoot配置文件中:

spring:  #thymeleaf模板引擎  thymeleaf:    #是否开启缓存    cache: false    #编码格式    encoding: UTF-8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这里我把缓存关闭,就能使页面进行实时的更新。

三、整合thymeleaf 模板引擎
上面已经把starter导入了,然后配置了thymeleaf,接下来编写测试

编写一个Controller:

package com.example.springbootdemo1.controller;import com.example.springbootdemo1.pojo.User;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.servlet.ModelAndView;import java.util.ArrayList;import java.util.List;/** * @author 不止于梦想 * @date 2022/10/21 21:12 */@RestControllerpublic class indexController {    @RequestMapping("/index")    public ModelAndView indexView(){        ModelAndView mv = new ModelAndView();        List<User> userList = new ArrayList<>();        for(int i=0;i<5;i++){            User user = new User("zhangsan"+i,5+i);            userList.add(user);        }        mv.addObject("user",userList);        mv.setViewName("index");        return mv;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

编写要跳转的index.html界面

<!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><!--引入thymeleaf命名空间--><head>    <meta charset="UTF-8">    <title>Title</title>74</head><body><table border="1">    <tr>        <td>用户姓名</td>        <td>用户年龄</td>    </tr>    <tr th:each="item : ${userList}">        <td th:text="${item.userName}"></td>        <td th:text="${item.userAge}"></td>    </tr></table></body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

在配置文件中,编写视图解析器:

spring:  #thymeleaf模板引擎  thymeleaf:    #是否开启缓存    cache: false    #编码格式    encoding: UTF-8  mvc:    view:      prefix: /pages/      suffix: .html
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

总结

SpringBoot是一门十分重要的技术,而学习这门技术有一定的门槛,需要我们掌握Spring技术栈的相关东西,而Spring Boot对于后面分布式的服务又是至关重要的,SpringCloud是在Spring Boot基础之上才能加以学习的,所以每一个Java技术人员,都应该学好SpringBoot。

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发