- SpringBoot介绍
- 什么是Spring Boot?
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
- Spring Boot有哪些特点?
- 构建SpringBoot项目以及启动器讲解
- Spring Boot常见的启动器有哪些?
spring-boot-starter-web是web的启动器,可以为全栈式的开发提供支持,包含tomcat、springmvc等jar包
spring-boot-starter-jdbc:支持spring以jdbc方式操作数据库的jar包的集合
spring-boot-starter-redis:支持redis键值存储的数据库操作
- Spring Boot的Web启动器的坐标是什么?
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>2.1.9.RELEASE</version>
- </dependency>
- 编写HelloWorld
- 如何编写Spring Boot启动类?
- package com.bjsxt.startapp;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication(scanBasePackages = "com.bjsxt")
- public class StartForTestListener2 {
- public static void main(String[] args) {
- SpringApplication.run(StartForTestListener2.class,args);
- }
- }
- 编写Spring Boot启动类时需要注意什么?
启动器存放的位置。启动器可以和 controller 位于同一个包下,或者位于 controller 的上一级 包中,但是不能放到 controller 的平级以及子包下。
- Spring Boot整合Servlet
- Spring Boot整合Servlet有几种方式?
两种
- 各种方式有什么特点?
1.注解方式:
Servlet类:
- package com.bjsxt.servlet;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- @WebServlet(name = "FirstServlet",urlPatterns = "/first")
- public class FirstServlet extends HttpServlet {
- @Override
- public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- System.out.println("first servlet");
- }
- }
启动类:
- package com.bjsxt.startapp;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.web.servlet.ServletComponentScan;
- @SpringBootApplication(scanBasePackages = "com.bjsxt")
- @ServletComponentScan(basePackages = "com.bjsxt.servlet")
- public class StartApplication {
- public static void main(String[] args) {
- SpringApplication.run(StartApplication.class, args);
- }
- }
配置类方式:
- 1.servlet类:
- package com.bjsxt.servlet;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- public class SecondServlet extends HttpServlet {
- @Override
- public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- System.out.println("second servlet");
- }
- }
- 2.配置类:
- package com.bjsxt.config;
- import com.bjsxt.servlet.SecondServlet;
- import org.springframework.boot.web.servlet.ServletRegistrationBean;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class ServletConfiguration {
- @Bean
- public ServletRegistrationBean getServletRegistrationBean(){
- ServletRegistrationBean bean=new ServletRegistrationBean(new SecondServlet());
- bean.addUrlMappings("/second");//相当于设置url-pattern
- return bean;
- }
- }
- 启动类:
- package com.bjsxt.startapp;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication(scanBasePackages = "com.bjsxt")
- public class StartApplication2 {
- public static void main(String[] args) {
- SpringApplication.run(StartApplication2.class, args);
- }
- }
- Spring Boot整合Filter
- Spring Boot整合Filter有几种方式?
两种:
- 注解方式
- 配置类方式
- 各种方式有什么特点?
1.注解方式:
Filter类:
- package com.bjsxt.filter;
- import javax.servlet.*;
- import javax.servlet.annotation.WebFilter;
- import java.io.IOException;
- @WebFilter(filterName = "FirstFilter",urlPatterns ="/first")
- public class FirstFilter implements Filter {
- @Override
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
- System.out.println("进入Filter");
- filterChain.doFilter(servletRequest, servletResponse);
- System.out.println("离开Filter");
- }
- }
启动类:
- package com.bjsxt.startapp;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.web.servlet.ServletComponentScan;
- @SpringBootApplication(scanBasePackages = "com.bjsxt")
- @ServletComponentScan("com.bjsxt.filter")
- public class StartForTestFilter1 {
- public static void main(String[] args) {
- SpringApplication.run(StartForTestFilter1.class,args);
- }
- }
2.配置类方式:
Filter类:
- package com.bjsxt.filter;
- import javax.servlet.*;
- import java.io.IOException;
- public class SecondFilter implements Filter {
- @Override
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
- System.out.println("进入Filter2");
- filterChain.doFilter(servletRequest, servletResponse);
- System.out.println("离开Filter2");
- }
- }
配置类:
- package com.bjsxt.config;
- import com.bjsxt.servlet.SecondServlet;
- import org.springframework.boot.web.servlet.ServletRegistrationBean;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class ServletConfiguration {
- @Bean
- public ServletRegistrationBean getServletRegistrationBean(){
- ServletRegistrationBean bean=new ServletRegistrationBean(new SecondServlet());
- bean.addUrlMappings("/second");
- return bean;
- }
- }
启动类:
- package com.bjsxt.startapp;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication(scanBasePackages = "com.bjsxt")
- public class StartForTestFilter2 {
- public static void main(String[] args) {
- SpringApplication.run(StartForTestFilter2.class,args);
- }
- }
- springBoot整合Listener
- Spring Boot整合Listener有几种方式?
两种 :注解方式和配置类方式
- 各种方式有什么特点?
1.注解:
Listener类:
- package com.bjsxt.listener;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import javax.servlet.annotation.WebListener;
- @WebListener
- public class FirstListener implements ServletContextListener {
- @Override
- public void contextInitialized(ServletContextEvent sce) {
- System.out.println("listener初始化");
- }
- @Override
- public void contextDestroyed(ServletContextEvent sce) {
- }
- }
启动类:
- package com.bjsxt.startapp;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.web.servlet.ServletComponentScan;
- @SpringBootApplication(scanBasePackages = "com.bjsxt")
- @ServletComponentScan("com.bjsxt.listener")
- public class StartForTestListener1 {
- public static void main(String[] args) {
- SpringApplication.run(StartForTestListener1.class,args);
- }
- }
方式二:配置类
- 1.Listener类:
- package com.bjsxt.listener;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import javax.servlet.annotation.WebListener;
- public class SecondListener implements ServletContextListener {
- @Override
- public void contextInitialized(ServletContextEvent sce) {
- System.out.println("listener2初始化");
- }
- @Override
- public void contextDestroyed(ServletContextEvent sce) {
- }
- }
- 2.配置类:
- package com.bjsxt.config;//package com.bjsxt.config;
- import com.bjsxt.listener.SecondListener;
- import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class ListenerConfiguration {
- @Bean
- public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
- return new ServletListenerRegistrationBean(new SecondListener());
- }
- }
- 3.启动类:
- package com.bjsxt.startapp;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.web.servlet.ServletComponentScan;
- @SpringBootApplication(scanBasePackages = "com.bjsxt")
- public class StartForTestListener2 {
- public static void main(String[] args) {
- SpringApplication.run(StartForTestListener2.class,args);
- }
- }
- Spring Boot访问静态资源
- 在Spring Boot中访问静态资源有几种方式?
两种:
1.方式一:访问classpath路径下的static目录:
2.方式二:在将静态资源放在src/main/webapp(需要自己创建)目录下
注意:该方式要在pom文件中添加:
- <build>
- <resources>
- <resource>
- <directory>src/main/webapp</directory>
- <targetPath>META-INF/resources</targetPath>
- <includes>
- <include>**/**</include>
- </includes>
- </resource>
- </resources>
- </build>
让src/main/webapp在编译的时候在resoureces路径下也生成webapp的文件
- Spring Boot文件上传
- 在Spring Boot中如何设置单个上传文件大小?
- 在Spring Boot中如何设置一次请求上传文件大小?
- Spring Boot整合jsp
- 在Spring Boot中整合jsp需要添加哪些坐标?
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.tomcat.embed</groupId>
- <artifactId>tomcat-embed-jasper</artifactId>
- <scope>provided</scope>
- </dependency>
- Spring Boot整合Freemarker
- 在Spring Boot中整合Freemarker需要添加哪些坐标?
- <!-- freemarker启动器的坐标 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-freemarker</artifactId>
- </dependency>
- Freemarker视图的扩建名是什么?
.ftl
- Thymeleaf入门-创建项目
- 在Spring Boot中整合Thymeleaf需要添加哪些坐标?
- <!--添加Thymeleaf启动器依赖-->
- <dependency>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- <groupId>org.springframework.boot</groupId>
- </dependency>
- Thymeleaf视图的扩建名是什么?
.html
- Thymeleaf视图要求放到项目的哪个目录下?
必须放在resources/templates目录下
- Thymeleaf入门-Thymeleaf基本使用
- Thymeleaf的特点是什么?
Thymeleaf:Thymelaef 是通过他特定语法对 html 的标记做渲染。
- 在使用Thymeleaf时页面会出现什么异常?
- 解决Thymeleaf中标签匹配的异常有几种方式?
两种:
- 让 html 的标记按照严禁的语法去编写。
- Thymeleaf.jar:更新为 3.0 以上的版本 thymeleaf-layout-dialect.jar:更新为 2.0 以上的版本