SpringBoot入门-2

本文介绍了SpringBoot中自动配置的原理,特别是针对静态资源的处理,包括webjars和公共目录的访问。同时,详细讲解了如何定制首页,并介绍了Thymeleaf模版引擎的使用,包括导入依赖和配置。通过引入Thymeleaf,可以方便地处理动态页面并实现数据绑定。
摘要由CSDN通过智能技术生成

SpringBoot入门-2

一、SpringBoot Web开发

SpringBoot到底帮我们配置了什么?我们能不能进行修改?能修改那些东西?能不能扩展?

  • xxxAutoConfiguration:向容器中自动配置组件
  • xxxProperties:自动配置类,装配配置文件中自定义的一些内容

需要解决的问题:

  • 导入静态资源
  • 首页(http://localhost:端口号/
  • jsp,模版引擎Thymeleaf
  • 装配扩展SpringMVC
  • 增删改查
  • 拦截器
  • 国际化

1、静态资源

打开WebMvcAutoConfiguration,可以看到有一个静态类,WebMvcAutoConfigurationAdapter

protected void addResourceHandlers(ResourceHandlerRegistry registry) {
	//如果静态资源的东西被自定义了
	//怎么自定义,找到WebMvcAutoConfigurationAdapter的注解中的WebMvcProperties,对其中关于静态资源的配置,自己在配置文件中配置一下即可。
    super.addResourceHandlers(registry);
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
    } else {
        ServletContext servletContext = this.getServletContext();
        //
        this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
        //getStaticPathPattern函数返回this.staticPathPattern,初始化:this.staticPathPattern = "/**";
        //表示当前目录下的所有东西都识别
        this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
            registration.addResourceLocations(this.resourceProperties.getStaticLocations());
            if (servletContext != null) {
                registration.addResourceLocations(new Resource[]{new ServletContextResource(servletContext, "/")});
            }

        });
    }
}

什么是wenjars,webjars可以通过maven的方式导入资源,在url中输入/webjars/**相当于在classpath:/META-INF/resources/webjars/下面找。

比如通过http://localhost:8080/webjars/3.4.1/jquery.js就可以访问到这个js文件。

webjars官网

在这里插入图片描述

  • addResourceHandlers函数调用的getStaticPathPattern函数会返回this.staticPathPattern,初始化:this.staticPathPattern = “/**”;
    //表示当前目录下的所有东西都识别
  • registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(this.resourceProperties.getStaticLocations());
    //this.resourceProperties.getStaticLocations()对应下面四个的位置
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
总结

1、在SpringBoot中,我们可以使用以下方式处理静态资源

  • webjars 通过localhost:8080/webjars/访问
  • public,static,/**,resources 通过localhost:8080/访问

2、优先级:resources>static>public
3、如果自定义目录,默认目录会失效。

二、首页

首页如何定制。还是在WebMvcAutoConfiguration里。

@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
	//getWelcomePage会获取CLASSPATH_RESOURCE_LOCATIONS路径
	//this.mvcProperties.getStaticPathPattern()会返回/**
    WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
    welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
    welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
    return welcomePageHandlerMapping;
}

从源码可以看出,在Resources目录下的public、resources、static中创建index.html,则可以通过http://localhost:端口号进行访问到。

模版引擎

前端交给我们的页面,是html页面,如果是我们以前开发,我们需要把他们转成jsp页面,jsp的好处就是当我们查出一些数据转发到jsp页面以后,我们可以用jsp轻松实现数据的现实及交互等。jsp支持非常强大的功能,包括能以jar的方式,不是war,像第二,我们用的还是嵌入式的Tomcat。所以呢,他现在默认是不支持jsp的。

那不支持jsp,如果我们直接用纯静态页面的方式,那给我们开发辉带来非常大的麻烦,那怎么办呢?SpringBoot推荐你可以来使用模版引擎。

那么这模版引擎,我们其实大家听到很多,其实jsp就是一个模版引擎,还有用的比较多的freemarker,包括Springboot给我们推荐的Thymeleaf。模版引擎有非常多,但再多的模版引擎,他们的思想都是一样的。

在这里插入图片描述

Thymeleaf

第一步:引入thymeleaf,怎么引入呢?对于SpringBoot来说,什么事情不都是一个start的事情嘛,我们去在项目中引入一下,给大家三个网址:

thymeleaf依赖:

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

Thymeleaf的自动配置类:ThymeleafProperties
前缀:“classpath:/templates”
后缀:".html"

public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";

如果需要访问Resources/templates下的test.html,可以通过以下定义的Controller进行访问,http://localhost:8080/test

@Controller
public class IndexController {
    @RequestMapping("/test")
    public String test()
    {
        return "test";
    }
}

结论:只要需要使用thymeleaf,只需要导入对应的依赖就可以了。我们将html放在我们的templates目录下即可。

P16

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值