zoukankan      html  css  js  c++  java
  • SpringBoot web开发

     要解决的问题:

    • 导入静态资源
    • 首页想办法定制
    • 这里面写不了jsp页面,我们要学模板引擎(Thymeleaf)来解决
    • 装配扩展springmvc
    • 增删改查怎么做
    • 拦截器
    • 国际化

    静态资源

    首先,我们搭建一个普通的SpringBoot项目,回顾一下HelloWorld程序!【演示】

    那我们要引入我们小实验的测试资源,我们项目中有许多的静态资源,比如,css,js等文件,这个SpringBoot怎么处理呢?

     如果我们是一个web应用,我们的main下会有一个webapp,我们以前都是将所有的页面导在这里面的,对吧!

    但是我们现在的pom呢,打包方式是为jar的方式,那么这种方式SpringBoot能不能来给我们写页面呢?当然是可以的,但是SpringBoot对于静态资源放置的位置,是有规定的!

    我们先来聊聊这个静态资源映射规则;

    SpringBoot中,SpringMVC的web配置都在 WebMvcAutoConfiguration  这个配置里面,我们可以去看看 WebMvcAutoConfigurationAdapter 中有很多配置方法;

    比如:addResourceHandlers

    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));
                    }
    
                }
            }

    读一下源代码:比如所有的 /webjars/** , 都需要去 classpath:/META-INF/resources/webjars/ 找对应的资源,那什么是webjars呢?

    webjars本质就是以jar包的方式引入我们的静态资源 , 我们以前要导入一个静态资源文件,直接导入即可。使用SpringBoot需要使用webjars,我们可以去搜索一下

    网站:https://www.webjars.org/ 【网站带看,并引入jQuery测试】

    要使用jQuery,我们只要要引入jQuery对应版本的pom依赖即可!【导入完毕,查看webjars目录结构,并访问Jquery.js文件】

    导入依赖:

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

     

    那我们项目中要是使用自己的静态资源该怎么导入呢?我们看下一行代码;

    我们去找staticPathPattern发现第二种映射规则 : /** , 访问当前的项目任意资源,它会去找 resourceProperties 这个类,我们可以点进去看一下,

    @ConfigurationProperties(
        prefix = "spring.resources",
        ignoreUnknownFields = false
    )
    public class ResourceProperties {
        private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

    resourceProperties 可以设置和我们静态资源有关的参数,this.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;
        }

     

     @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;
                }
    
                private List<Resource> resolveFaviconLocations() {
                    String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.getResourceLocations(this.resourceProperties.getStaticLocations());
                    List<Resource> locations = new ArrayList(staticLocations.length + 1);
                    Stream var10000 = Arrays.stream(staticLocations);
                    ResourceLoader var10001 = this.resourceLoader;
                    this.resourceLoader.getClass();
                    var10000.map(var10001::getResource).forEach(locations::add);
                    locations.add(new ClassPathResource("/"));
                    return Collections.unmodifiableList(locations);
                }
            }


    模板引擎

    结论:只要需要使用thymeleaf,只需要导入对应的依赖就可以了(注意到入依赖的版本)。

    我们将html放在我们的templates目录下即可

  • 相关阅读:
    Python进阶: Decorator 装饰器你太美
    计算机网络自顶向下方法第3章-传输层 (Transport Layer).2
    Python进阶:值传递,引用传递?不存在的,是赋值传递
    Python进阶:对象复制与比较,分深浅,见真假
    Python基础:模块化来搭项目
    这说明一个问题
    我稍微思考了一下
    由LruCache和DiskLruCache提供三级缓存支持的ImageLoader
    回忆一个加塞方法
    三年六班的李子明同学,你妈拿了两本计算机基础在二号树上等你
  • 原文地址:https://www.cnblogs.com/book3/p/12219201.html
Copyright © 2011-2022 走看看