准备工作 页面downloadboostrap的官方实例页面,方便练习
资源来自:https://getbootstrap.com/docs/4.3/examples/
资源的目录如下:
1.controller层 package com.changyue.springbootresetfucrud.controller; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpSession; import java.util.Map; /** * @program: spring-boot-restful-crud * @description: 用户控制器 * @author: YuanChangYue * @create: 2019-08-13 17:59 */ @Controller public class LoginController { /** * 用户登录 * * @param username 用户名 默认测试 123 * @param password 密码 默认测试 123 * @param map map * @param httpSession session * @return 页面名称 */ @PostMapping("/user/login") public String login(@RequestParam("username") String username, @RequestParam("password") String password, Map<String, Object> map, HttpSession httpSession) { if ("123".
Spring Boot 与 Spring MVC 根据SpringBoot 官方文档中的资料 ,可以看出SpringBoot和SpringMVC的关系。
本章节提到的官文文档地址如下: <https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-auto-configuration
1. Spring Boot 中Spring MVC 自动配置 官方文档中的原文如下:
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.
The auto-configuration adds the following features on top of Spring’s defaults:
Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
自动配置viewResolver (视图解析器:根据方法返回的值获得视图对象 ) ContentNegotiatingViewResolver : 组合所有的试图解析器 自定义:==可以自己给容器中添加视图解析器 ,自动为我们组合起来== Support for serving static resources, including support for WebJars (covered later in this document)).
1. 开始使用 创建springBoot 应用 选择需要的模块 创建springboot
选择模块
SpringBoot 已经为我们把需要的配置自动装配好,只需要在后续的开发中,在配置文件中进行设置即可
编写代码
2. 自动配置 XXXXXAutoConfiguration :在容器中自动配置组件 XXXXXProperties : 配置类 这些文件都在通过Mavan引入的外部库中,org.springframework.boot.autoconfigure。
3. 静态资源映射规则 默认创建的 SpringBoot 项目是没有web文件夹的,那如何映射css,js等静态资源呢?
Spring boot 会自动装配WebMvcAutoConfiguration类,会对静态资源的映射做出规定,
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { if (!this.resourceProperties.isAddMappings()) { logger.debug("Default resource handling disabled"); return; } Duration cachePeriod = this.resourceProperties.getCache().getPeriod(); CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl(); if (!registry.hasMappingForPattern("/webjars/**")) { customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/") .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); if (!