SpringBoot集成SpringMVC详解

SpringBoot整合SpringMVC

springboot在开发web项目的时候具备天然的优势,现在的很多企业级开发都是依托于springboot的。

使用springboot的步骤:

​ 1、创建一个SpringBoot应用,选择我们需要的模块,SpringBoot就会默认将我们的需要的模块自动配置好
​ 2、手动在配置文件中配置部分配置项目就可以运行起来了
​ 3、专注编写业务代码,不需要考虑以前那样一大堆的配置了。

一、SpringBoot整合Servlet

1、编写Servlet类

编写Servlet类,用来接收浏览器请求

@WebServlet(name = "myServlet",urlPatterns = "/srv")
public class MyServlet extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("111");
        super.doGet(req, resp);
    }
}

2、配置启动类

在启动类中配置扫描Servlet,以及注册Servlet的bean对象。

@SpringBootApplication
@ServletComponentScan//添加扫描Servlet的注解
public class SpringbootWebApplication {
    
    

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

    //将我们的Servlet注册为bean对象,使其启动就生效
    @Bean
    public ServletRegistrationBean<MyServlet> getServletRegistrationBean(){
    
    
        ServletRegistrationBean<MyServlet> bean = new ServletRegistrationBean<>(new MyServlet());
        bean.setLoadOnStartup(1);
        return bean;
    }
}

实际工作中我们很少使用Servlet,讲Servlet是为了引出下面的过滤器和监听器。这两个东西是比较常用的

3、编写Filter

filter也是一个非常重要的模块,比如字符编码过滤器等。要注意过滤器使用了责任链模式,是链式执行的,需要执行chain.doFilter(request, response);判断是否向下执行。

@WebFilter(filterName = "MyFilter", urlPatterns = "/*")//过滤所有请求
public class MyFilter implements Filter {
    
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    
    
        System.out.println("init");
    }

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

    @Override
    public void destroy() {
    
    
        System.out.println("destory");
    }
}

4、编写Listener

listener是servlet规范定义的一种特殊类,用于监听servletContext,HttpSession和ServletRequest等域对象的创建和销毁事件。监听域对象的属性发生修改的事件,用于在事件发生前、发生后做一些必要的处理。可用于以下方面:
1、统计在线人数和在线用户
2、系统启动时加载初始化信息
3、统计网站访问量
4、记录用户访问路径

public class MyHttpSessionListener implements HttpSessionListener {
    
    

    public static int online=0;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
    
    
        System.out.println("创建session");
        online++;
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
    
    
        System.out.println("销毁session");
    }
}

在配置类中注册listener

@SpringBootApplication
@ServletComponentScan//添加扫描Servlet的注解
public class SpringbootWebApplication {
    
    

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

    //注册我们自定义的监听器
    @Bean
    public ServletListenerRegistrationBean listenerRegist(){
    
    
        ServletListenerRegistrationBean srb = new ServletListenerRegistrationBean();
        srb.setListener(new MyHttpSessionListener());
        System.out.println("listener");
        return srb;
    }
}

5、编写Controller代码

@Controller
public class ThymeleafController {
    
    
    @RequestMapping("/login")
    public String login(HttpServletRequest request){
    
    
        HttpSession session = request.getSession(true);
        return "login";
    }

    @RequestMapping("online")
    @ResponseBody
    public String online(){
    
    
        return "当前在线人数:"+MyHttpSessionListener.online +"人";
    }

}

先发送login请求,就会创建Serssion,就会计数;然后再发送online的请求,就能够获取Session数,也就是网站在线人数。我们就完成了简易版的统计网站在线人数功能。

二、静态资源的配置

默认情况下,Spring Boot 将在 classpath 或者 ServletContext 根目录下从名为 /static (/public、/resources 或 /META-INF/resources)目录中获取静态内容。它使用了 Spring MVC 的 ResourceHttpRequestHandler,因此可以通过添加自己的 WebMvcConfigurerAdapter 并重写 addResourceHandlers 方法来修改此行为。

1、加载webjars文件

在pom文件中添加jquery的相关依赖,直接可以通过浏览器访问到http://localhost:8080/webjars/jquery/3.4.1/jquery.js

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.4.1</version>
</dependency>

上面的操作,表示我们能在浏览器中直接访问webjars文件,也就是我们pom依赖所引入的文件。

2、加载静态资源(如html、js等)

当查找静态资源的时候能够发现静态资源的路径是/**,会去ResourceProperties这个类,可以看到对应的资源目录。

源码解释:

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
    
    

	private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
    
     "classpath:/META-INF/resources/",
			"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
	
	/**
	 * Locations of static resources. Defaults to classpath:[/META-INF/resources/,
	 * /resources/, /static/, /public/].
	 */
	private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
	private String[] appendSlashIfNecessary(String[] staticLocations) {
    
    
		String[] normalized = new String[staticLocations.length];
		for (int i = 0; i < staticLocations.length; i++) {
    
    
			String location = staticLocations[i];
			normalized[i] = location.endsWith("/") ? location : location + "/";
		}
		return normalized;
	}

通过源码我们能够发现:SpringBoot支持的静态资源的目录一共有如下几个:

  1. "classpath:/resources/"
  2. "classpath:/static/"
  3. "classpath:/public/"

所以说,静态资源不能乱放,基本上都是放在resource资源目录下:
在这里插入图片描述

补充:我们一般情况下都只使用项目创建好之后自动生成的static目录。所有的html、css、js页面一般都是扔在static目录结构之下,很少自己创建resources、public目录。

但是我们要知道SpringBoot是怎么查找和识别静态资源的机制,防止以后随便创建目录如zhangsan,然后把html扔在里面,那样是查不到的。这样的错误如果你不知道SpringBoot查找静态资源的机制,是很难排查的。

静态资源一定要放在classpath:/resources/ classpath:/static/ classpath:/public/ 这三个资源目录下。

3、欢迎页面

我们发现在开发过程中,欢迎页面总是起名为index.html,凭什么只能起名为index.html?我张三不服气,就是想把zhangsan.html当作欢迎页面,凭什么就不行?

要想知道为什么欢迎页面只能起名为index.html,就得去看SpringBoot的源码:

首页的配置信息关键源码:

@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
				FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
    
    
			WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
					new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
					this.mvcProperties.getStaticPathPattern());
			welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
			return welcomePageHandlerMapping;
		}

		private Optional<Resource> getWelcomePage() {
    
    
			String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
			return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
		}

private Resource getIndexHtml(String location) {
    
    
			return this.resourceLoader.getResource(location + "index.html");
		}

通过源码我们可以看到,Spring Boot查找欢迎页面的时候,就是指定查找index.html。
所以我们的欢迎页面是不能乱起名字的。

三、SpringBoot对SpringMVC的支持

1、SpringBoot集成SpringMVC

springmvc框架是一个mvc的web框架,springmvc允许创建@controller和@RestController bean来处理传入的HTTP请求,控制器中的方法通过@RequestMapping注解映射到HTTP请求。

Springboot提供了适用于大多数SpringMVC应用的自动配置。

  • 引入 ContentNegotiatingViewResolver 和 BeanNameViewResolver bean视图解析器。
  • 支持服务静态资源,包括对 WebJar 的支持。
  • 自动注册 Converter(网页传入的数据封装成对象,完成数据类型的转化)、GenericConverter 和 Formatter bean(将日期转换成规定的格式)。
  • 支持 HttpMessageConverter,用来转换http请求和响应。
  • 自动注册 MessageCodesResolver,定义错误代码生成规则。
  • 支持静态 index.html。
  • 自动使用 ConfigurableWebBindingInitializer bean,将请求树绑定到javaBean中。

也就是说,SpringBoot集成了几乎所有的SpringMVC的功能,能够完成我们绝大多数的功能需求。

2、SpringBoot视图解析器

SpringBoot中自动配置了ViewResolver,其实就是我们MVC中学习的视图解析器——即根据方法的返回值取得视图对象(View),然后由视图对象决定如何渲染,以及转发和重定向。

SpringBoot源码验证:

//WebMvcAutoConfiguration
@Bean //我们在这里确实看到已经给容器中注册了一个bean
        @ConditionalOnBean({
    
    ViewResolver.class})
        @ConditionalOnMissingBean(
            name = {
    
    "viewResolver"},
            value = {
    
    ContentNegotiatingViewResolver.class}
        )
        public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
    
    
            ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
            resolver.setContentNegotiationManager((ContentNegotiationManager)beanFactory.getBean(ContentNegotiationManager.class));
            resolver.setOrder(-2147483648);
            return resolver;
        }


//ContentNegotiatingViewResolver
//处理视图解析器名称
@Nullable
    public View resolveViewName(String viewName, Locale locale) throws Exception {
    
    
        RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
        Assert.state(attrs instanceof ServletRequestAttributes, "No current ServletRequestAttributes");
        List<MediaType> requestedMediaTypes = this.getMediaTypes(((ServletRequestAttributes)attrs).getRequest());
        if (requestedMediaTypes != null) {
    
    
            //获取候选的视图对象
            List<View> candidateViews = this.getCandidateViews(viewName, locale, requestedMediaTypes);
            //选择一个最适合的视图对象,然后把这个对象返回
            View bestView = this.getBestView(candidateViews, requestedMediaTypes, attrs);
            if (bestView != null) {
    
    
                return bestView;
            }
        }

        String mediaTypeInfo = this.logger.isDebugEnabled() && requestedMediaTypes != null ? " given " + requestedMediaTypes.toString() : "";
        if (this.useNotAcceptableStatusCode) {
    
    
            if (this.logger.isDebugEnabled()) {
    
    
                this.logger.debug("Using 406 NOT_ACCEPTABLE" + mediaTypeInfo);
            }
    
            return NOT_ACCEPTABLE_VIEW;
        } else {
    
    
            this.logger.debug("View remains unresolved" + mediaTypeInfo);
            return null;
        }
    }


//getCandidateViews
private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes)
			throws Exception {
    
    

		List<View> candidateViews = new ArrayList<>();
		if (this.viewResolvers != null) {
    
    
			Assert.state(this.contentNegotiationManager != null, "No ContentNegotiationManager set");
			for (ViewResolver viewResolver : this.viewResolvers) {
    
    
				View view = viewResolver.resolveViewName(viewName, locale);
				if (view != null) {
    
    
					candidateViews.add(view);
				}
				for (MediaType requestedMediaType : requestedMediaTypes) {
    
    
					List<String> extensions = this.contentNegotiationManager.resolveFileExtensions(requestedMediaType);
					for (String extension : extensions) {
    
    
						String viewNameWithExtension = viewName + '.' + extension;
						view = viewResolver.resolveViewName(viewNameWithExtension, locale);
						if (view != null) {
    
    
							candidateViews.add(view);
						}
					}
				}
			}
		}
		if (!CollectionUtils.isEmpty(this.defaultViews)) {
    
    
			candidateViews.addAll(this.defaultViews);
		}
		return candidateViews;
	}

//initServletContext
@Override
	protected void initServletContext(ServletContext servletContext) {
    
    
		Collection<ViewResolver> matchingBeans =
				BeanFactoryUtils.beansOfTypeIncludingAncestors(obtainApplicationContext(), ViewResolver.class).values();
		if (this.viewResolvers == null) {
    
    
			this.viewResolvers = new ArrayList<>(matchingBeans.size());
			for (ViewResolver viewResolver : matchingBeans) {
    
    
				if (this != viewResolver) {
    
    
					this.viewResolvers.add(viewResolver);
				}
			}
		}
		else {
    
    
			for (int i = 0; i < this.viewResolvers.size(); i++) {
    
    
				ViewResolver vr = this.viewResolvers.get(i);
				if (matchingBeans.contains(vr)) {
    
    
					continue;
				}
				String name = vr.getClass().getName() + i;
				obtainApplicationContext().getAutowireCapableBeanFactory().initializeBean(vr, name);
			}

		}
		AnnotationAwareOrderComparator.sort(this.viewResolvers);
		this.cnmFactoryBean.setServletContext(servletContext);
	}

在上面的源码中我们可以发现:SpringBoot中有关于视图解析器完整的、整体的处理环节

另外通过上面的代码分析,我们知道了springboot是在容器中去找视图解析器,因此,我们可以给容器自定义添加视图解析器,这个类会帮我们将它组合起来。

补充:但是我们一般情况下是不会自定义视图解析器的,SpringBoot提供的功能可以满足我们大部分的需求,除非SpringBoot满足不了需求,这时候才会必须自定义视图解析器。

如果您想保留 Spring Boot MVC 的功能,并且需要添加其他 MVC 配置(interceptor、formatter 和视图控制器等),可以添加自己的 WebMvcConfigurerAdapter 类型的 @Configuration 类,但不能带 @EnableWebMvc 注解

最好不要使用 @EnableWebMvc 注解,因为使用此注解表示我们自己完全掌控了SpringMVC,而SpringBoot对SpringMVC的默认配置支持就不再起作用了。

也就是说原有配置都失效。

如果您想自定义 RequestMappingHandlerMapping、RequestMappingHandlerAdapter 或者 ExceptionHandlerExceptionResolver 实例,可以声明一个 WebMvcRegistrationsAdapter 实例来提供这些组件。

1、添加模板引擎支持

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>

2、编写自己的视图解析器

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    
    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    
    

        //此时我们发送http://localhost:8080/mjt请求,可以请求到templates目录下的success.html静态资源文件
        registry.addViewController("/mjt").setViewName("success");
    }
}

此时我们就能够通过发送http://localhost:8080/mjt,能够请求到templates资源目录下的静态文件。

猜你喜欢

转载自blog.csdn.net/qq_42583242/article/details/107094447
今日推荐