SpringBoot_5(静态资源)
发布日期:2021-04-30 21:02:39 浏览次数:106 分类:精选文章

本文共 3413 字,大约阅读时间需要 11 分钟。

Spring Boot???????????????

1. Spring Boot????????

?Spring Boot?????????css?js?html??????????????????Web?????Spring Boot??JAR??????????????webapp?????????????????????

1.1 ???????????

Spring Boot????????????????????

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/

?????????????????

  • [src/main/resources/META-INF/resources/]
  • [src/main/resources/resources/]
  • [src/main/resources/static/]
  • [src/main/resources/public/]

1.2 ?????????

???????????????????application.properties???

spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/wangxing/

???Spring Boot????????????????????????????????

2. ?????????

Spring Boot??????Tomcat????????????????????????????????

2.1 ???????Thymeleaf

Thymeleaf?????????????????Spring Boot????????????classpath:/templates/????????.html???

2.2 Thymeleaf????

Thymeleaf??????????????????

  • ?????${}?${...}
  • ?????th:if?th:unless
  • ?????th:each
  • ?????th:replace
  • ?????th:attr

???????????Thymeleaf??????????

2.3 ??????

?????????.html??????templates???????????

    ??????1    

????

2.4 ??????

Spring Boot?????????????

  • Map<String, Object>?????????????
  • Model?????????????
  • ?????Controller??????????

    @Controllerpublic class HelloController {    @RequestMapping("/testHello1")    public String testHello1(Map
    map) { map.put("id", 1001); map.put("name", "??"); return "test1"; }}

    3. ???????????????

    3.1 ????????

    ?????????????????????????????

    src/??? main/?   ??? resources/?   ?   ??? META-INF/?   ?   ?   ??? resources/?   ?   ??? resources/?   ?   ??? static/?   ?   ??? public/?   ?       ??? index.html?   ??? static/?       ??? images/?           ??? background.png??? static/    ??? css        ??? style.css

    3.2 ????????

    ?????????????????????????

    http://localhost:8080/css/style.css

    ????webjars?????????

    3.3 ??WebMvcAutoConfiguration

    WebMvcAutoConfiguration?????????????????????????

    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)        .setUseLastModified(this.resourceProperties.getCache().isUseLastModified()));}

    3.4 ?????????

    ?application.properties?????????????

    spring.web.resources.static-locations=classpath:/test/,classpath:/hello/

    ???Spring Boot?????test/?hello/???????????

    4. ????????????

    4.1 ??????

    ????????Thymeleaf????????????

        ????    

    4.2 controller?????

    ??Controller???????????????????

    @Controllerpublic class IndexController {    @RequestMapping("/")    public String index(Model model) {        model.addAttribute("title", "????");        model.addAttribute("content", "?????????????");        return "index";    }}

    4.3 ????????

    ?SpringBootApplication????????????????bean???????

    @SpringBootApplication@ComponentScan("com.example.springboot")public class SpringbootstatictestApplication {    public static void main(String[] args) {        SpringApplication.run(SpringbootstatictestApplication.class, args);    }}

    5. ????

  • ????????????????????????????????
  • ??????????.html???????????
  • ?????????application.properties??????????
  • ???????????????????????????
  • ??????????????Spring Boot?????????????????????????????????

    上一篇:第一范式,第二范式,第三范式,BCNF范式理解
    下一篇:Hive3.0.0自定义UDF函数获取每月结合国家法定节假日工作日数实现

    发表评论

    最新留言

    关注你微信了!
    [***.104.42.241]2026年05月25日 03时17分45秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章