第二章SpringBoot集成web开发

1 概述

本章节通过demo操作演示,完成在Spring Boot中集成Servlet、Filter、Listener。了解Spring Boot访问静态资源访相关知识点。通过上传文件demo了解Spring Boot的配置文件相关知识点,开发工作中要注意新旧版本中同一个配置的名称变化。

2 创建Spring Boot项目

2.1 创建maven项目

2.2 修改jdk版本

SpringBoot 2.0以后的版本使用的是jdk 1.8.

SpringBoot 2.0 以前的版本使用jdk1.7

可以通过修改pom文件来修改jdk版本

2.3 添加Spring Boot启动器依赖

3.集成Servlet

3.1 注解方式集成

3.1.1 创建Servlet

package com.liulg.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns="/first")  //Servlet注解,不需要配置web.xml
public class FirstServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getOutputStream().print("first servlet!");
	}
}

3.1.2 创建启动类

package com.liulg;

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

@SpringBootApplication
@ServletComponentScan //spring启动时扫描@WebServlet注解
public class Application {

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

3.1.3 运行结果

3.2 方法注册集成

3.2.1 创建Servlet

package com.liulg.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getOutputStream().print("second servlet!");
	}
}

3.2.2 创建启动类

package com.liulg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import com.liulg.servlet.SecondServlet;
@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Bean
	public ServletRegistrationBean<SecondServlet> getServletRegistrationBean() {
		ServletRegistrationBean<SecondServlet> bean = new ServletRegistrationBean<SecondServlet>(new SecondServlet());
		bean.addUrlMappings("/second");
		return bean;
	}
}

3.2.3 运行结果

4 集成Filter

4.1 注解方式集成

4.1.1 创建Filter

package com.liulg.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
//@WebFilter(urlPatterns= {"*.do","*.jsp"})
@WebFilter(urlPatterns="/first")
public class FirstFilter implements Filter {
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("first filter!");
		chain.doFilter(request, response);
	}
}

4.1.2 创建启动类

package com.liulg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class Application {

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

}

4.1.3 运行结果

4.2 方法注册集成

4.2.1 创建Filter

package com.liulg.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SecondFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("second filter!");
		chain.doFilter(request, response);
	}
}

4.2.2 创建启动类

package com.liulg;

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 com.liulg.filter.SecondFilter;
import com.liulg.servlet.SecondServlet;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
		
	//注册servlet
	@Bean
	public ServletRegistrationBean<SecondServlet> getServletRegistrationBean() {
		ServletRegistrationBean<SecondServlet> bean = new ServletRegistrationBean<SecondServlet>(new SecondServlet());
		bean.addUrlMappings("/second");
		return bean;
	}
	//注册filter
		@Bean
		public FilterRegistrationBean<SecondFilter> getFilterRegistrationBean() {
			FilterRegistrationBean<SecondFilter> bean = new FilterRegistrationBean<SecondFilter>(new SecondFilter());
			bean.addUrlPatterns("/second");
			return bean;
		}
}

4.2.3 运行结果

5 集成Listener

5.1 注解方式集成

5.1.1 创建Listener

package com.liulg.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("first listener init....");
	}
	
}

5.1.2 创建启动类

package com.liulg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class Application {

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

}

5.1.3 运行结果

5.2 方法注册集成

5.2.1 创建Listener

package com.liulg.listener;

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

public class SecondListener implements ServletContextListener{
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("second listener init....");
	}
}

5.2.2 创建启动类

package com.liulg;

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 com.liulg.listener.SecondListener;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
		
	//注册Listener
	@Bean
	public ServletListenerRegistrationBean<SecondListener>  getServletListenerRegistrationBean() {
		ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
		return bean;
	}
}

5.2.3 运行结果

6 访问静态资源

Spring Boot默认会从两个路径访问静态资源,一个是classpath下的static目录,static目录必须手动创建,并且目录名必须是static;另一个是ServletContext根目录下的webapp目录,webapp目录也必须手动创建,并且目录名必须是webapp。

6.1 classpath/static

6.2 ServletContext/webapp

7 文件上传

7.1 项目结构

7.2 创建Controller

package com.liulg.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
public class FileUploadController {
	@RequestMapping("fileUploadController")
	public Map<String, String> fileUpload(MultipartFile file)  throws Exception {
			System.out.println(file.getOriginalFilename());
			file.transferTo(new File("e:/"+file.getOriginalFilename()));
			Map<String,String>map = new HashMap<String,String>();
			map.put("msg", "ok");
			return map;
	}

7.3 创建启动类

package com.liulg;

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

@SpringBootApplication
public class Application {

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

}

7.4 创建页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
		<form action="fileUploadController" method="post" enctype="multipart/form-data">
			文件:<input type="file" name="file" />  <!—这里的name属性必须和controller里的参数名保持一致-->
			<input type="submit"/>
		</form>
</body>
</html>

7.5 配置及注意事项

Spring上传文件大小默认不能超过10MB,超过这个大小后台会报异常。

因此,需要在Spring Boot中添加配置信息,Spring Boot可识别的配置文件有三种格式,xml、properties、yml,任意选择一种即可。配置文件的存放路径为classpath下,配置名称为application.properties。

注意SpringBoot2.0之前配置如下:

#设置单个上传文件的大小

spring.http.multipart.maxFileSize=200MB


#设置一次请求上传文件的总大小

spring.http.multipart.maxRequestSize=200MB

Spring Boot2.0及以后版本配置如下:

spring.servlet.multipart.max-file-size=200MB

spring.servlet.multipart.max-request-size=200MB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值