Springboot integrates common web technologies

1. premise

  • The maven project has been created and the springboot-weblauncher has been imported
  • The maven project can start normally

2. Integrate servelt in springboot

2.1 Scan servelt by way of annotation

Use annotation ServeltComponentScanannotations on the startup class of the project
ServeltComponentScanto scan all servelts under the current package level

package cn.liuhao.project;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class App {
    
    

	public static void main(String[] args) {
    
    
		SpringApplication.run(App.class, args);
	}

}

2.2 Register servlet components through methods

package cn.liuhao.project;

import javax.servlet.Servlet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import cn.liuhao.project.web.servlet.FirstServlet;

@SpringBootApplication
public class App {
    
    

	public static void main(String[] args) {
    
    
		SpringApplication.run(App.class, args);
	}
//	一定要加注解@Bean,这样springboot才会扫描执行这个方法
	@Bean
	public ServletRegistrationBean getServletRegistraionBean() {
    
    
//		注册servlet bean实例,初始化的参数就是所需要注册servlet对象
		ServletRegistrationBean<Servlet> bean = new ServletRegistrationBean<Servlet>(new FirstServlet());
//		如果不在servlet上加注解webServlet来设置urlMapping,可以使用方法addUrlMappings设置url mapping
		bean.addUrlMappings("/fisrt");
		return bean;
	}
}

3. Integrate filter in springboot

3.1 Integrate filters through annotations

Use annotation ServeltComponentScanannotations on the startup class of the project
ServeltComponentScanto scan all filters under the current package level

Insert picture description here

3.2 Register the filter component by method

package cn.liuhao.project;

import javax.servlet.Filter;
import javax.servlet.Servlet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import cn.liuhao.project.web.filter.FirstFilter;
import cn.liuhao.project.web.servlet.FirstServlet;

@SpringBootApplication
public class App {
    
    

	public static void main(String[] args) {
    
    
		SpringApplication.run(App.class, args);
	}
	@Bean
	public ServletRegistrationBean<Servlet> getServletRegistration() {
    
    
		ServletRegistrationBean<Servlet> bean = new ServletRegistrationBean<Servlet>(new FirstServlet());
		bean.addUrlMappings("/first");
		return bean;
	}

	// 使用方法注册filter不需要写注解@ServletComponentScan,但是servlet同样也需要以方法的形式注册
	@Bean
	public FilterRegistrationBean<Filter> getFilterRegistration() {
    
    
		// 创建需要注册filter实例
		FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<Filter>(new FirstFilter());
		// 设置拦截url
		bean.addUrlPatterns(new String[] {
    
     "/*", "*.jsp" });
		// 返回值
		return bean;
	}
}

Insert picture description here

4. Integrate listener (listener) in springboot

4.1. Integrate filters through annotations

Use annotation ServeltComponentScanannotations on the startup class of the project
ServeltComponentScanto scan all the current package levellistener

Insert picture description here

4.2 Register listener (listener) by method

listener

package cn.liuhao.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class Listener implements ServletContextListener {
    
    

	@Override
	public void contextInitialized(ServletContextEvent sce) {
    
    
		System.out.println("初始化");
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
    
    
		System.out.println("销毁");
	}
}

App startup class

package cn.liuhao;

import javax.servlet.ServletContextListener;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;

import cn.liuhao.listener.Listener;

@SpringBootApplication
public class App {
    
    

	public static void main(String[] args) {
    
    

		SpringApplication.run(App.class, args);

	}

	// 注解@Bean
	// 一定不要忘记
	@Bean
	public ServletListenerRegistrationBean<ServletContextListener> getRegistrationBean() {
    
    

		// 注册listener bean
		return new ServletListenerRegistrationBean<ServletContextListener>(new Listener());
	}
}

Insert picture description here

5. The springboot project accesses static resources

5.1 Access static resources through the classpath/static directory

In the maven project, we need to create a new static directory under the resoures folder. The
name must be static
Insert picture description here
.
Insert picture description here

5.2 Access static resources through the ServletContext directory

In the maven project, the servletContext directory corresponds to the src/main/webapp directory. If
there is no src/main/webapp directory, create it yourself. The
folder name must be webapp

Insert picture description here
effect
Insert picture description here

6. Springboot realizes file upload

6.1 Front page

  • You need to change the formlabel enctypeattribute tomultipart/form-data
  • method must be post method
  • Action directly write the interface defined by the Controller
<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
		<title>文件上传</title>
	</head>
	<body>
		<!-- 上传文件夹需要更改enctype属性为multipart/form-data -->
		<form action="api/fileupload" method="post" enctype="multipart/form-data">
			<!-- input的type属性为file -->
			<input type="file" name="filename" value="" />
			<p></p>
			<input type="submit" value="提交"/>
		</form>
	</body>
</html>

6.2controller interface and startup class

Start class

package cn.liuhao.fileupload;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    
    

	public static void main(String[] args) {
    
    
		SpringApplication.run(App.class, args);
	}

}

controller class

package cn.liuhao.fileupload.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 
 * 注解 @RestController 保留了 @Controller的作用同时指定了当前Controller上的所有返回结果都换为json格式
 * 也就是说我们不需要在每一个接口都写 @ResponseBody
 * 
 * @author admin
 *
 */
@RestController
public class WebController {
    
    

	/**
	 * 文件上传用MultipartFile接口类型接受
	 * 
	 * 这个参数名称必须和你在html中定义的input的name属性相同,否则接受为null
	 * 
	 * @param filename
	 * @return
	 */
	@RequestMapping("api/fileupload")
	public Map<String, Object> apiFileupload(MultipartFile filename) throws Exception {
    
    

		Map<String, Object> result = new HashMap<String, Object>();

		if (filename != null) {
    
    
			// 打印文件的原名称
			System.out.println(filename.getOriginalFilename());
			filename.transferTo(new File("d:/" + filename.getOriginalFilename()));
			result.put("msg", "ok");
		} else {
    
    
			result.put("msg", "failture");
		}

		return result;
	}
}

6.3 Application property file of springboot project

  • The application property file of the springboot project must be placed in the resources folder, not in the static folder
    Insert picture description here

Use the application attribute file to specify the file upload size

document content

# 设置spring项目中文件上传最大值的大小,必须带单位MB
spring.servlet.multipart.maxFileSize = 200MB
# 设置上传文件的总大小
spring.servlet.multipart.maxRequestSize=2000MB

Note: Different versions of springboot are written differently

  • Spring Boot 1.3 版本: multipart.maxFileSize
  • Spring Boot 1.4 version and 1.5 version: spring.http.multipart.maxFileSize
  • Spring Boot 2.0 版本以上: spring.servlet.multipart.maxFileSize

For specific details, please refer to this article: Springboot file upload exceeds the limit processing

Guess you like

Origin blog.csdn.net/qq_42418169/article/details/108901113