SpringBoot与Web开发(持续更新中)

1、使用SpringBoot:

**1)、创建SpringBoot应用,选中我们需要的模块,如图;**

在这里插入图片描述
2)、SpringBoot已经默认将这些场景配置好了(自动配置原理),只需要在配置文件中指定少量配置就可以运行起来;
3)、自己编写业务代码
下面创建一个restful的crud实际项目
利用spring Initializr创建一个wenb工程

2、SpringBoot对静态资源的映射规则

  public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

            }
        }
		//配置欢迎页映射
 		@Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
            return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
        }

		//配置喜欢的图标
 		@Configuration
        @ConditionalOnProperty(
            value = {"spring.mvc.favicon.enabled"},
            matchIfMissing = true
        )
        public static class FaviconConfiguration implements ResourceLoaderAware {
            private final ResourceProperties resourceProperties;
            private ResourceLoader resourceLoader;

            public FaviconConfiguration(ResourceProperties resourceProperties) {
                this.resourceProperties = resourceProperties;
            }

            public void setResourceLoader(ResourceLoader resourceLoader) {
                this.resourceLoader = resourceLoader;
            }

            @Bean
            public SimpleUrlHandlerMapping faviconHandlerMapping() {
                SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
                mapping.setOrder(-2147483647);
                mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
                return mapping;
            }

            @Bean
            public ResourceHttpRequestHandler faviconRequestHandler() {
                ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
                requestHandler.setLocations(this.resolveFaviconLocations());
                return requestHandler;
            }

1)、所有/webjars/,都去classpath:/META-INF/resources/webjars/找资源**
webjars:以jar包的方式引入静态资源
https://www.webjars.org/
在这里插入图片描述
localhost:8080/webjars/jqury/3.3.1/jqury.js 可以访问到
在pom引入jqury的依赖

  <!--引入jquery的webjar-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1</version>
        </dependency>

2)、"/"访问当前项目的任何资源,(静态资源的文件夹)**

"classpath:/META-INF/resources/", 
"classpath:/resources/", 
"classpath:/static/", 
"classpath:/public/"
"/":当前项目的根路径

3)、欢迎页;静态资源文件夹下的所有index.html页面;被"/"映射;localhost:8080/ 就会去找index.html**

4)、所有的“*/favicon.ico” 都是在静态资源文件下找:

3、模板引擎

JSP、Velocity、Freemarker、Thymeleaf
在这里插入图片描述
SpringBoot推荐使用Thymeleaf;
语法更简单,功能更强大

1、引入模板引擎Thymeleaf

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

2、Thymeleaf使用及语法

只要我们把HTML页面放在classpath:/templates/,thymeleaf就会自动渲染
使用:
1、导入thymeleaf的名称空间;

<html lang="en" xmlns:th="http://www.thymeleaf.org">

2、使用thymeleaf语法;

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>成功!</h1>
<!--th:test 将div里边的文本内容设置为-->
<div th:text="${hello}"></div>
</body>
</html>

3、语法规则

1)、th:test;改变当前元素里面的文本内容
th :任意html属性;来替换原生属性的值
在这里插入图片描述
2)、能写哪些表达式

${}:获取变量值
		获取对象的属性、调用方法
		使用内置的基本对象
		内置的工具对象
*{}:选择表达式和${}在功能上一样,补充:配合th:object=“${session.user}”
#{}:获取国际化内容
@{}:定义url
~{}:片段引用表达式

前台页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>成功!</h1>
<!--th:test 将div里边的文本内容设置为-->
<div th:text="${hello}"></div>
<hr/>
<div th:text="${hello}"></div>
<div th:utext="${hello}"></div>
<hr/>

<!--th:each写在哪个标签上,每次都会遍历都会生成当前这个标签  3个h4-->
<h4 th:text="${user}" th:each="user:${user}"></h4>
<hr/>
<h4>
    <span th:each="user:${user}">[[${user}]]</span>
</h4>
</body>
</html>

后台controller

 @RequestMapping("/success")
    public String success(Map<String,Object> map){
        map.put("hello","<h1>你好</h1>");
        map.put("user", Arrays.asList("张三","lisi","王五"));
        //classpath:/templates/success.html
        return "success";
    }

在这里插入图片描述

4、SpringMVC自动配置

1)、SpringBoot都自动配置好了SpringMVC
2)、扩展SpringMVC
编写一个配置类(@Configuration),是WebMvcConfigureAdapter类型,不能标注@EnableWebMvc
既保留了所有的自动配置,也能用我们自己的配置

@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //浏览器发送的/springboot请求来到success页面
        registry.addViewController("/springboot").setViewName("success");
        //super.addViewControllers(registry);
    }
}

3)、全面接管SpringMVC
SpringBoot对SpringMVC的自动配置都不需要了,所有都是我们自己配置;所有的SpringMVC自动配置都失效了;我们需要在配置类中添加@EnableWebMvc

5、如何修改SpringBoot的默认配置

模式:
1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component),如果有就用用户自己配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;
2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值