本文共 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?????????????????????????????????
发表评论
最新留言
关于作者