一、SpringBoot简介
SpringBoot是由Pivotal团队在2013年开始研发、2014年4月发布第一个版本的全新开源的轻量级框架。它基于Spring4.0设计,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程。另外SpringBoot通过集成大量的框架使得依赖包的版本冲突,以及引用的不稳定性等问题得到了很好的解决。
SpringBoot所具备的特征有:
(1)可以创建独立的Spring应用程序,并且基于其Maven或Gradle插件,可以创建可执行的JARs和WARs;
(2)内嵌Tomcat或Jetty等Servlet容器;
(3)提供自动配置的“starter”项目对象模型(POMS)以简化Maven配置;
(4)尽可能自动配置Spring容器;
(5)提供准备好的特性,如指标、健康检查和外部化配置;
(6)绝对没有代码生成,不需要XML配置。
重要策略:
SpringBoot框架中还有两个非常重要的策略:开箱即用和约定优于配置。开箱即用,Outofbox,是指在开发过程中,通过在MAVEN项目的pom文件中添加相关依赖包,然后使用对应注解来代替繁琐的XML配置文件以管理对象的生命周期。这个特点使得开发人员摆脱了复杂的配置工作以及依赖的管理工作,更加专注于业务逻辑。约定优于配置,Convention over configuration,是一种由SpringBoot本身来配置目标结构,由开发者在结构中添加信息的软件设计范式。这一特点虽降低了部分灵活性,增加了BUG定位的复杂性,但减少了开发人员需要做出决定的数量,同时减少了大量的XML配置,并且可以将代码编译、测试和打包等工作自动化。
二,构建 Spring Boot 项目及启动器讲解
1.使用 maven 构建 SpringBoot 项目
2.修改 pom 文件 将 jdk 的版本更新为 1.7
<properties>
<java.vesion>1.7</java.vesion>
</properties>
3.注入 SpringBoot 启动坐标
<dependencies>
<!--springboot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
4.SpringBoot 启动器。
所谓的 springBoot 启动器其实就是一些 jar 包的集合。SprigBoot 一共提供 44 启动器。
4.1 spring-boot-starter-web
支持全栈式的 web 开发,包括了 romcat 和 springMVC 等 jar
4.2 spring-boot-starter-jdbc
支持 spring 以 jdbc 方式操作数据库的 jar 包的集合
4.3 spring-boot-starter-redis
支持 redis 键值存储的数据库操作
三,Spring Boot 入门 HelloWorld
1.编写返回 HelloWorld 的 Controller
@Controller
public class HelloWorld {
@RequestMapping("/hello")
@ResponseBody
public Map<String, Object> showHelloWorld(){
Map<String, Object> map = new HashMap<>();
map.put("msg", "Helloworld");
return map;
}
}
2.启动 SpringBoot 编写启动类
/**
* springBoot的启动类
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3.关于编写启动器需要注意的问题
启动器存放的位置。启动器可以和 controller 位于同一个包下,或者位于 controller 的上一级包中,但是不能放到 controller 的平级以及子包下。
四、SpringBoot整合servlet
1.通过注解扫描完成 Servlet 组件的注册
1.1 编写 servlet
/**
*SpringBoot整合Servlet方式一
*
*<servlet>
* <servlet-name>FirstServlet</servlet-name>
* <servlet-class>com.bjsxt.servlet.FirstServlet</servlet-class>
*</servlet>
*
*<servlet-mapping>
* <servlet-name>FirstServlet</servlet-name>
* <url-pattern>/first</url-pattern>
*</servlet-mapping>
*
*/
@WebServlet(name="FirstServlet",urlPatterns="/first")
public class FirstServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("FirstServlet.........");
}
}
1.2 编写启动类
/**
* SpringBoot整合Servlet方式一
*/
@SpringBootApplication
@ServletComponentScan //在springBoot启动时会扫描@WebServlet,并将该类实例化
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2.通过方法完成 Servlet 组件的注册
2.1 编写 servlet
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Springboot整合servlet方式二
* @author Administrator
*/
public class SecondServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("SecondServlet.............");
}
}
2.2 编写启动类
/**
* Springboot整合servlet方式二
*/
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
@Bean
public ServletRegistrationBean getServletRegistrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/second");
return bean;
}
}
五、SpringBoot整合 Filter
1,通过注解扫描完成 Filter 组件的注册
1.1 编写 Filter
/**
*SpringBoot整合Filter 方式一
*<filter>
* <filter-name>FirstFilter</filter-name>
* <filter-class>com.bjsxt.filter.FirstFilter</filter-class>
*</filter>
*<filter-mapping>
* <filter-name>FirstFilter</filter-name>
* <url-pattern>/first</url-pattern>
*</filter-mapping>
*/
//@WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.jsp"})
@WebFilter(filterName="FirstFilter",urlPatterns="/first")
public class FirstFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("进入Filter");
chain.doFilter(request, response);
System.out.println("退出Filter");
}
}
1.2 编写启动类
/**
*SpringBoot整合Filter 方式一
*
*/
@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2,通过方法完成 Filter 组件的注册
2.1 编写 Filter
/**
*SpringBoot整合Filter 方式二
*/
public class SecondFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("进入SecondFilter..");
chain.doFilter(request, response);
System.out.println("退出SecondFilter..");
}
}
2.2 编写启动类
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
/**
* 注册Servlet
* @return
*/
@Bean
public ServletRegistrationBean getServletRegistrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/second");
return bean;
}
/**
* 注册Filter
*/
@Bean
public FilterRegistrationBean getFilterRegistrationBean() {
FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
bean.addUrlPatterns("/second");
return bean;
}
}
六,整合 Listener
1,通过注解扫描完成 Listener 组件的注册
1.1 编写 Listener
/**
* springBoot整合Listener
*
*<listener>
* <listener-class>com.bjsxt.listener.FirstListener</listener-class>
*</listener>
*/
@WebListener
public class FirstListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Listener......init....");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
ServletContextListener.super.contextDestroyed(sce);
}
}
1.2 编写启动类
/**
*SpringBoot整合Listener 方式一
*
*/
@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2. 通过方法完成 Listener 组件注册
2.1 编写 Listener
/**
* springBoot整合Listener方式二。
*/
public class SecondListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("SecondListener....init....");
}
}
2.2 编写启动类
/**
* SpringBoot整合Listener方式二
*/
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
/**
* 注册listener
*/
public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean() {
ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
return bean;
}
}
七、访问静态资源
访问静态资源classpath/static 的目录 优先级高于 ServletContext 根目录
1. SpringBoot 从 classpath/static 的目录
注意目录名称必须是 static
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>静态资源访问方式一</title>
</head>
<body>
静态资源访问方式一
<hr/>
<img src="images/11.jpeg">
</body>
</html>
2. ServletContext 根目录下
在 src/main/webapp 目录名称必须要 webapp
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>静态资源访问方式二</title>
</head>
<body>
静态资源访问方式二
<hr/>
<img src="images/11.jpeg">
</body>
</html>
八,文件上传
1. 编写 Controller
@RestController
public class FileUploadController {
//处理文件上传
@RequestMapping("/fileUploadController")
public Map<String, Object> fileUpload(MultipartFile filename) throws Exception{
System.out.println(filename.getOriginalFilename());
filename.transferTo(new File("F:/"+filename.getOriginalFilename()));
Map<String, Object> map = new HashMap<>();
map.put("msg", "ok");
return map;
}
}
2.设置上传文件大小的默认值
需要添加一个 springBoot 的配置文件
3.SpringBoot 1.5版本配置上传文件大小的控制:
设置单个上传文件的大小
spring.http.multipart.maxFileSize=200MB
设置一次请求上传文件的总容量
spring.http.multipart.maxRequestSize=200MB
4.SpringBoot 2.1.x版本配置上传文件大小的控制
spring.servlet.multipart.maxFileSize=200MB
spring.servlet.multipart.maxRequestSize=200MB
九、SpringBoot 整合 jsp 技术
1,创建项目
2,修改 pom 文件,添加坐标
<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 http://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.1.10.RELEASE</version>
</parent>
<groupId>com.sxt</groupId>
<artifactId>08spring-boot-view-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!--springboot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- jasper -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
3,创建 springBoot 的全局配置文件,application.properties
4,创建 Controller
/**
* SpringBoot整合jsp
* @author Administrator
*/
@Controller
public class UserController {
/*
* 处理请求,产生数据
*/
@RequestMapping("/showUser")
public String showUser(Model model) {
List<Users> list = new ArrayList<>();
list.add(new Users(1,"张三",20));
list.add(new Users(2,"李四",18));
list.add(new Users(3,"王五",22));
//需要一个Model对象
model.addAttribute("list", list);
//跳转视图
return "userList";
}
}
5,创建 jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" align="center" width="50%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<c:forEach items="${list }" var="user">
<tr>
<td>${user.userid }</td>
<td>${user.username }</td>
<td>${user.userage }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
十,SpringBoot 整合 Freemarker
修改 pom 添加坐标
<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 http://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.1.10.RELEASE</version>
</parent>
<groupId>com.sxt</groupId>
<artifactId>09spring-boot-view-freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!--springboot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- freemarker启动器的坐标 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
</project>
编写视图
注意:springBoot 要求模板形式的视图层技术的文件必须要放到 src/main/resources 目录下必须要一个名称为 templates
<html>
<head>
<title>展示用户数据</title>
<meta charset="utf-8"/>
</head>
<body>
<table border="1" align="center" width="50%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<#list list as user >
<tr>
<td>${user.userid}</td>
<td>${user.username}</td>
<td>${user.userage}</td>
</tr>
</#list>
</table>
</body>
</html>
十一、SpringBoot 整合 Thymeleaf (重点讲解)
1. 创建 Thymeleaf 的入门项目
1.1 创建项目
1.2 修改 pom 文件添加坐标
<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 http://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.1.10.RELEASE</version>
</parent>
<groupId>com.sxt</groupId>
<artifactId>10spring-boot-view-thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!--springboot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf启动器的坐标 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
1.3 创建存放视图的目录
目录位置:src/main/resources/templates
templates:该目录是安全的。意味着该目录下的内容是不允许外界直接访问的。
2. Thymeleaf 的基本使用
2.1Thymeleaf 特点:
Thymelaef 是通过他特定语法对 html 的标记做渲染。
2.2 编写 Controller
/**
* Thymeleaf入门案例
*/
@Controller
public class DemoController {
@RequestMapping("/show")
public String showInfo(Model model) {
model.addAttribute("msg","Thymeleaf 第一个案例");
return "index";
}
}
2.3 创建视图 .html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Thymeleaf入门</title>
</head>
<body>
<span th:text="Hello"></span>
<hr/>
<span th:text="${msg}"></span>
</body>
</html>
2.4 解决异常
解决异常方式一:
让HTML的标记按照严禁的语法去编写
解决异常方式二:
Thymeleaf.jar:更新为 3.0 以上的版本
thymeleaf-layout-dialect.jar:更新为 2.0 以上的版本
<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 http://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>1.5.10.RELEASE</version>
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>10-spring-boot-view-thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.7</java.version>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>