当先锋百科网

首页 1 2 3 4 5 6 7
  1. SpringBoot介绍
    1. 什么是Spring Boot?

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

  1. Spring Boot有哪些特点?

v2-c9063a71707c221802a172419db1994c_b.jpg
  1. 构建SpringBoot项目以及启动器讲解
    1. Spring Boot常见的启动器有哪些?

spring-boot-starter-web是web的启动器,可以为全栈式的开发提供支持,包含tomcat、springmvc等jar包

spring-boot-starter-jdbc:支持spring以jdbc方式操作数据库的jar包的集合

spring-boot-starter-redis:支持redis键值存储的数据库操作

  1. Spring Boot的Web启动器的坐标是什么?
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <version>2.1.9.RELEASE</version>
  5. </dependency>
  1. 编写HelloWorld
    1. 如何编写Spring Boot启动类?
  1. package com.bjsxt.startapp;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication(scanBasePackages = "com.bjsxt")
  5. public class StartForTestListener2 {
  6. public static void main(String[] args) {
  7. SpringApplication.run(StartForTestListener2.class,args);
  8. }
  9. }
  1. 编写Spring Boot启动类时需要注意什么?

启动器存放的位置。启动器可以和 controller 位于同一个包下,或者位于 controller 的上一级 包中,但是不能放到 controller 的平级以及子包下。

  1. Spring Boot整合Servlet
    1. Spring Boot整合Servlet有几种方式?

两种

  1. 各种方式有什么特点?

1.注解方式:

Servlet类:

  1. package com.bjsxt.servlet;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. @WebServlet(name = "FirstServlet",urlPatterns = "/first")
  9. public class FirstServlet extends HttpServlet {
  10. @Override
  11. public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  12. System.out.println("first servlet");
  13. }
  14. }

启动类:

  1. package com.bjsxt.startapp;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.web.servlet.ServletComponentScan;
  5. @SpringBootApplication(scanBasePackages = "com.bjsxt")
  6. @ServletComponentScan(basePackages = "com.bjsxt.servlet")
  7. public class StartApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(StartApplication.class, args);
  10. }
  11. }

配置类方式:

  1. 1.servlet类:
  2. package com.bjsxt.servlet;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. public class SecondServlet extends HttpServlet {
  9. @Override
  10. public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  11. System.out.println("second servlet");
  12. }
  13. }
  14. 2.配置类:
  15. package com.bjsxt.config;
  16. import com.bjsxt.servlet.SecondServlet;
  17. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  18. import org.springframework.context.annotation.Bean;
  19. import org.springframework.context.annotation.Configuration;
  20. @Configuration
  21. public class ServletConfiguration {
  22. @Bean
  23. public ServletRegistrationBean getServletRegistrationBean(){
  24. ServletRegistrationBean bean=new ServletRegistrationBean(new SecondServlet());
  25. bean.addUrlMappings("/second");//相当于设置url-pattern
  26. return bean;
  27. }
  28. }
  29. 启动类:
  30. package com.bjsxt.startapp;
  31. import org.springframework.boot.SpringApplication;
  32. import org.springframework.boot.autoconfigure.SpringBootApplication;
  33. @SpringBootApplication(scanBasePackages = "com.bjsxt")
  34. public class StartApplication2 {
  35. public static void main(String[] args) {
  36. SpringApplication.run(StartApplication2.class, args);
  37. }
  38. }
  1. Spring Boot整合Filter
    1. Spring Boot整合Filter有几种方式?

两种:

  1. 注解方式
  2. 配置类方式
    1. 各种方式有什么特点?

1.注解方式:

Filter类:

  1. package com.bjsxt.filter;
  2. import javax.servlet.*;
  3. import javax.servlet.annotation.WebFilter;
  4. import java.io.IOException;
  5. @WebFilter(filterName = "FirstFilter",urlPatterns ="/first")
  6. public class FirstFilter implements Filter {
  7. @Override
  8. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  9. System.out.println("进入Filter");
  10. filterChain.doFilter(servletRequest, servletResponse);
  11. System.out.println("离开Filter");
  12. }
  13. }

启动类:

  1. package com.bjsxt.startapp;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.web.servlet.ServletComponentScan;
  5. @SpringBootApplication(scanBasePackages = "com.bjsxt")
  6. @ServletComponentScan("com.bjsxt.filter")
  7. public class StartForTestFilter1 {
  8. public static void main(String[] args) {
  9. SpringApplication.run(StartForTestFilter1.class,args);
  10. }
  11. }

2.配置类方式:

Filter类:

  1. package com.bjsxt.filter;
  2. import javax.servlet.*;
  3. import java.io.IOException;
  4. public class SecondFilter implements Filter {
  5. @Override
  6. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  7. System.out.println("进入Filter2");
  8. filterChain.doFilter(servletRequest, servletResponse);
  9. System.out.println("离开Filter2");
  10. }
  11. }

配置类:

  1. package com.bjsxt.config;
  2. import com.bjsxt.servlet.SecondServlet;
  3. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. public class ServletConfiguration {
  8. @Bean
  9. public ServletRegistrationBean getServletRegistrationBean(){
  10. ServletRegistrationBean bean=new ServletRegistrationBean(new SecondServlet());
  11. bean.addUrlMappings("/second");
  12. return bean;
  13. }
  14. }

启动类:

  1. package com.bjsxt.startapp;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication(scanBasePackages = "com.bjsxt")
  5. public class StartForTestFilter2 {
  6. public static void main(String[] args) {
  7. SpringApplication.run(StartForTestFilter2.class,args);
  8. }
  9. }
  1. springBoot整合Listener
    1. Spring Boot整合Listener有几种方式?

两种 :注解方式和配置类方式

  1. 各种方式有什么特点?

1.注解:

Listener类:

  1. package com.bjsxt.listener;
  2. import javax.servlet.ServletContextEvent;
  3. import javax.servlet.ServletContextListener;
  4. import javax.servlet.annotation.WebListener;
  5. @WebListener
  6. public class FirstListener implements ServletContextListener {
  7. @Override
  8. public void contextInitialized(ServletContextEvent sce) {
  9. System.out.println("listener初始化");
  10. }
  11. @Override
  12. public void contextDestroyed(ServletContextEvent sce) {
  13. }
  14. }

启动类:

  1. package com.bjsxt.startapp;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.web.servlet.ServletComponentScan;
  5. @SpringBootApplication(scanBasePackages = "com.bjsxt")
  6. @ServletComponentScan("com.bjsxt.listener")
  7. public class StartForTestListener1 {
  8. public static void main(String[] args) {
  9. SpringApplication.run(StartForTestListener1.class,args);
  10. }
  11. }

方式二:配置类

  1. 1.Listener类:
  2. package com.bjsxt.listener;
  3. import javax.servlet.ServletContextEvent;
  4. import javax.servlet.ServletContextListener;
  5. import javax.servlet.annotation.WebListener;
  6. public class SecondListener implements ServletContextListener {
  7. @Override
  8. public void contextInitialized(ServletContextEvent sce) {
  9. System.out.println("listener2初始化");
  10. }
  11. @Override
  12. public void contextDestroyed(ServletContextEvent sce) {
  13. }
  14. }
  15. 2.配置类:
  16. package com.bjsxt.config;//package com.bjsxt.config;
  17. import com.bjsxt.listener.SecondListener;
  18. import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
  19. import org.springframework.context.annotation.Bean;
  20. import org.springframework.context.annotation.Configuration;
  21. @Configuration
  22. public class ListenerConfiguration {
  23. @Bean
  24. public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
  25. return new ServletListenerRegistrationBean(new SecondListener());
  26. }
  27. }
  28. 3.启动类:
  29. package com.bjsxt.startapp;
  30. import org.springframework.boot.SpringApplication;
  31. import org.springframework.boot.autoconfigure.SpringBootApplication;
  32. import org.springframework.boot.web.servlet.ServletComponentScan;
  33. @SpringBootApplication(scanBasePackages = "com.bjsxt")
  34. public class StartForTestListener2 {
  35. public static void main(String[] args) {
  36. SpringApplication.run(StartForTestListener2.class,args);
  37. }
  38. }
  1. Spring Boot访问静态资源
    1. 在Spring Boot中访问静态资源有几种方式?

两种:

1.方式一:访问classpath路径下的static目录:

v2-5862b378d5e17ef6b4dbab09f56dbd28_b.jpg

2.方式二:在将静态资源放在src/main/webapp(需要自己创建)目录下

v2-93a8a8edb9abfd9f59eb2a94350c7a79_b.jpg

注意:该方式要在pom文件中添加:

  1. <build>
  2. <resources>
  3. <resource>
  4. <directory>src/main/webapp</directory>
  5. <targetPath>META-INF/resources</targetPath>
  6. <includes>
  7. <include>**/**</include>
  8. </includes>
  9. </resource>
  10. </resources>
  11. </build>

让src/main/webapp在编译的时候在resoureces路径下也生成webapp的文件

  1. Spring Boot文件上传
    1. 在Spring Boot中如何设置单个上传文件大小?

v2-0fb97705e8d3f4b3f66c1ecece0c50d8_b.jpg
  1. 在Spring Boot中如何设置一次请求上传文件大小?

v2-17a0755115faf170e2d5982f0e286ed8_b.jpg
  1. Spring Boot整合jsp
    1. 在Spring Boot中整合jsp需要添加哪些坐标?
  1. <dependency>
  2. <groupId>javax.servlet</groupId>
  3. <artifactId>jstl</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.apache.tomcat.embed</groupId>
  7. <artifactId>tomcat-embed-jasper</artifactId>
  8. <scope>provided</scope>
  9. </dependency>
  1. Spring Boot整合Freemarker
    1. 在Spring Boot中整合Freemarker需要添加哪些坐标?
  1. <!-- freemarker启动器的坐标 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-freemarker</artifactId>
  5. </dependency>
  1. Freemarker视图的扩建名是什么?

.ftl

v2-65c5a706782089be6c78649d8f24bb80_b.jpg
  1. Thymeleaf入门-创建项目
    1. 在Spring Boot中整合Thymeleaf需要添加哪些坐标?
  1. <!--添加Thymeleaf启动器依赖-->
  2. <dependency>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. <groupId>org.springframework.boot</groupId>
  5. </dependency>
  1. Thymeleaf视图的扩建名是什么?

.html

v2-1ff816e71a2de9b572bbc31e839d103a_b.jpg
  1. Thymeleaf视图要求放到项目的哪个目录下?

必须放在resources/templates目录下

  1. Thymeleaf入门-Thymeleaf基本使用
    1. Thymeleaf的特点是什么?

Thymeleaf:Thymelaef 是通过他特定语法对 html 的标记做渲染。

  1. 在使用Thymeleaf时页面会出现什么异常?

v2-9eb869c4e054a84c98d754bee7978637_b.jpg
  1. 解决Thymeleaf中标签匹配的异常有几种方式?

两种:

  1. 让 html 的标记按照严禁的语法去编写。
  2. Thymeleaf.jar:更新为 3.0 以上的版本 thymeleaf-layout-dialect.jar:更新为 2.0 以上的版本