230122_50_SpringBoot入门

  • SpringBoot Web开发

jar:webapp!

自动装配

1.创建应用,选择模块

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

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

要解决的问题:

  • 导入静态资源....

  • 首页

  • jsp,模块引擎Thymeleaf

  • 增删改查

  • 拦截器

  • 国际化!

  • 静态资源

  • 新建一个controller

package com.bill.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/22 - 01 - 22 - 20:57
 * @Description: com.bill.controller
 * @version: 1.0
 */

@RestController
public class ControllerHello {

    @RequestMapping("/hello")
    public String hello(){
        return "hello,springboot";
    }
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
   if (!this.resourceProperties.isAddMappings()) {
      logger.debug("Default resource handling disabled");
      return;
   }
   addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
   addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
      registration.addResourceLocations(this.resourceProperties.getStaticLocations());
      if (this.servletContext != null) {
         ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
         registration.addResourceLocations(resource);
      }
   });
}
  • 什么是webjars:WebJars是将客户端(浏览器)网络资源库(例如jQuery或者Bootstrap)打包成j的JAR文件
  • 以jquery为例,导入依赖
<dependency>
   <groupId>org.webjars</groupId>
   <artifactId>jquery</artifactId>
   <version>3.6.3</version>
</dependency>

目录结构相同:

  • 加载webjars下所有静态资源:
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");

测试结果:可以正常访问到该路径下的资源jquery.js

  • 其他可以加载静态资源的路径
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
      "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
  • 新建资源目录

  • 测试结果:

  • 优先级:resources > static > public

posted @   billwen  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示