SpringBoot

一、SpringBoot

1、SpringBoot介绍:

通过简化配置来进一步简化了Spring应用的整个搭建和开发过程。另外SpringBoot通过集成大量的框架使得依赖包的版本冲突,以及引用的不稳定性等问题得到了很好的解决。

2、SpringBoot继承父工程:

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.2</version>
  </parent>
  <groupId>com.pj</groupId>
  <artifactId>01SpringBoot</artifactId>
  <version>0.0.1-SNAPSHOT</version>

3、注入SpringBoot启动器坐标

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

3、什么是SpringBoot启动器:其实就是一些jar包的集合(44个启动器)

例:spring-boot-starter-web:支持全栈的web开发,包括Tomcat、SpringMVC等jar。

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

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

二、SpringBoot的HelloWord!

1、HelloWord(以json字符串格式返回)

/**·
 * HelloWord	
 * @author dell
 *
 */
@Controller
public class HelloWord {

	@ResponseBody
	@RequestMapping("/hello")
	public Map<String,Object> showHeolloWord(){
		Map<String,Object> map = new HashMap<>();
		map.put("msg","HelloWord");
		return map;
	}
	
}

2、SpringBoot启动器

/**
 * SpringBoot 启动类
 * @author dell
 *
 */
@SpringBootApplication
public class APP {

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

3、关于启动类的一些注意事项

启动器存放位置:可以存放在controller的父级包中,或着与controller放在同一级的包中。

不能放在与controller同级包中,或子级包中。

三、整合Servlet

1、通过注解扫描完成Servlet组件的注册

/**
 * SpringBoot整合Servlet方法一
 * 
 * 
 * 之前在web.xml文件中配置servlet
 * <servlet>
 * 	<servlet-name>FirstServlet</servlet-name>
 * 	<servlet-class>com.pj.servlet.FirstServlet</servlet-class>
 * </servlet>
 * 
 * <servlet-mapping>
 * 	<servlet-name>FirstServlet</servlet-name>
 * 	<url-pattern>/first</pattern>
 * </servlet-mapping>
 * 
 * @author dell
 *
 */
@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--------------------第一天学SpringBoot");
	}
}

注:在启动类中需添加注解扫描

@ServletComponentScan //在SpringBoot启动时会扫描@WebServlet,并将该类实例化

2、通过方法完成组件的注册


public class SecondServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
		System.out.println("SecondServlet-----------------第一天学SpringBoot");
	}
}

/**
 * SpringBoot启动类
 * SpringBoot整合Servlet方法二
 * @author dell
 *
 */
@SpringBootApplication
public class APP2 {

	public static void main(String[] args) {
		SpringApplication.run(APP2.class, args);
	}
	@Bean
	public ServletRegistrationBean<Servlet> getServlet() {
		ServletRegistrationBean<Servlet> bean = new ServletRegistrationBean<Servlet>(new SecondServlet());
		bean.addUrlMappings("/second");
		return bean;
	}
}

四、文件上传

1、编写Controller

/**
 * SpringBoot文件上传
 * 
 * @author dell
 *
 */
@RestController  //加在类上时,该类下的方法都会以json格式返回相当于在每个方法上加@ResponseBody
public class FileUploadController {

	@RequestMapping("/fileUpload")
	public Map<String,Object> fileUpload(@RequestParam("file") MultipartFile file) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("msg", "success");
		//获取文件名
		String fileName = file.getOriginalFilename();
		//保存文件
		file.transferTo(new File("d:/"+fileName));
		return map;
	}
}

2、编写启动类

/**
 * SpringBoot启动器:文件上传
 * @author dell
 *
 */
@SpringBootApplication
public class FileUploadAPP {

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

3、设置上=传文件的默认大小

application.properties

spring.http.multipart.maxFileSize=200MB   设置单个文件的最大值
spring.http.multipart.maxRequestSize=200MB 一次上传总容量的最大值

五、整合Filter

1、通过注解扫描完成Filter组件的注册。

编写Filter

/**
 * SpringBoot整合Filter方法一
 * 注解扫描
 * @author dell
 *
 */
@WebFilter(filterName = "FirstFilter",urlPatterns = "/first")
public class FirstFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		
		System.out.println("进入Filter");
		chain.doFilter(request, response);
		System.out.println("离开filter");
		
	}

}

编写启动类

/**
 * SpringBoot整合Filter方式一
 * 启动类
 * @author dell
 *
 */
@SpringBootApplication
@ServletComponentScan
public class FilterApp {

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

2、通过方法完成Filter组件的注册。

编写Filter

/**
 * SpringBoot整合Filter方法二
 * 
 * @author dell
 *
 */
public class SecondFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		System.out.println("进入SecondFilter");
		chain.doFilter(request, response);
		System.out.println("离开SecondFilter");
	}

	
}

编写启动类

/**
 * SpringBoot整合Filter方法二
 * 启动类
 * @author dell
 *
 */
@SpringBootApplication
public class FilterApp2 {

	public static void main(String[] args) {
		SpringApplication.run(FilterApp2.class, args);
	}
	/**
	 * 注册Servlet
	 * @return
	 */
	@Bean
	public ServletRegistrationBean<Servlet> getServlet2() {
		ServletRegistrationBean<Servlet> bean  = new ServletRegistrationBean<Servlet>(new SecondServlet());
		bean.addUrlMappings("/second");
		return bean;
	}
	/**
	 * 注册Filter
	 */
	@Bean
	public FilterRegistrationBean<Filter> getFiler() {
		FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<Filter>(new SecondFilter());
		bean.addUrlPatterns("/second");
		return bean;
 	}
}

六、整合Listener

1、通过扫描注解完成Listener组件的注册

编写Listener

/**
 * SpringBoot整合Listener方法一
 * @author dell
 *
 */
@WebListener
public class FirstListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		ServletContextListener.super.contextInitialized(sce);
		System.out.println("FirstListener-------");
	}
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		ServletContextListener.super.contextDestroyed(sce);
	}
}

编写启动类

/**
 * 启动类
 * @author dell
 *
 */
@SpringBootApplication
@ServletComponentScan
public class ListenerApp {

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

2、通过方法完成Listener组件的注册

编写Listener

/**
 * SpringBoot整合Listener方法二
 * @author dell
 *
 */

public class SecondListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		ServletContextListener.super.contextDestroyed(sce);
	}
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		ServletContextListener.super.contextInitialized(sce);
		System.out.println("SecondListener-----");
	}
}

编写启动类

/**
 * 启动类
 * @author pj
 *
 */
@SpringBootApplication
public class ListenerApp2 {

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

七、访问静态资源

1、SpringBoot从classpath/static的目录

目录名称必须是static

2、ServletContext的根目录

在src/main/webapp下,必须是是webapp

八、SpringBoot视图层技术

1、SpringBoot整合jsp技术

编写全局配置文件

我管他叫:视图解析器

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

在pom.xml中添加一下代码

在spring-boot-starter-web中并不支持jstl和jasper的jar包。

<!-- 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>

编写Controller(实体类就不在这写了)

/**
 * SpringBoot整合jsp
 * @author dell
 *
 */
@Controller
public class UserController {

	@RequestMapping("/show")
	public String show(Model model) {
		List<Users> list = new ArrayList<>();
		list.add(new Users(01,"庞杰",22));
		list.add(new Users(02,"庞二杰",23));
		list.add(new Users(03,"庞三杰",24));
		//model对象
		model.addAttribute("list",list);
		//跳转视图
		//return "/WEB-INF/jsp/userList.jsp";
		return "userList";
	}
}

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<table border="1">
	<tr>
		<td>学号</td>
		<td>姓名</td>
		<td>年龄</td>
	</tr>
	<c:forEach items="${list }" var="user">
		<tr>
			<td>${user.id}</td>
			<td>${user.name}</td>
			<td>${user.age}</td>
		</tr>
	</c:forEach>
</table>
</body>
</html>

启动类

/**
 * 启动类
 * @author dell
 *
 */
@SpringBootApplication
public class UserApp {

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

注:以上写完之后呢,运行后报404错误,在Controller中试了一下return全路径:“/WEB-INF/jsp/userList.jsp”。结果可行。

初步判断是application.properties配置文件没有生效。

去网上查资料在pom.xml中添加如下代码方可生效。

<build>
     <resources>
         <resource>
             <directory>src/main/java</directory>
             <includes>
                 <include>**/*.yml</include>
                 <include>**/*.properties</include>
                 <include>**/*.xml</include>
             </includes>
             <filtering>false</filtering>
         </resource>
         <resource>
             <directory>src/main/resources</directory>
             <includes>
                 <include>**/*.yml</include>
                 <include>**/*.properties</include>
                 <include>**/*.xml</include>
             </includes>
             <filtering>false</filtering>
         </resource>
     </resources>
 </build>

添加后运行,结果可行。

但是,终点来了!!!!!!!将此段代码删除后也可生效。(使用一次,终生有效)[滑稽][滑稽]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值