Spring Boot web开发

自动装配
Spring Boot到底帮我们配置了什么?我们能不能修改?能修改什么东西?能不能扩展

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

要解决的问题:

  • 导入静态资源
  • 首页
  • jsp,模板引擎Thymeleaf
  • 装配扩展spring mvc
  • 增删改、查
  • 拦截器
  • 国际化

静态资源

总结:

  1. 在Spring Boot中可以使用以下方式处理静态资源
  • webjars ==>localhost:8080/webjars/...
  • public,static,/**,resources ==>localhost:8080/
  1. 优先级:rosources>static(默认)>public

首页与图标定制

<index.html>/<favicon.ico>放在static目录下

模板引擎

只要需要使用thymeleaf, 只需要导入对应的依赖就可以了.将HTML放在templates目录下即可

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

Thymeleaf 的默认规则,它会把你返回的字符串加上前后缀去查找模板文件。前缀是 “classpath:/templates/”,后缀是 “.html”,所以你返回 “test”,它就会去 “templates/test.html” 找对应的页面。

//在templates目录下的所有页面,只能通过Controller跳转
//这个需要模板引擎的支持!thymeleaf
@Controller
public class IndexController {
    @GetMapping("/test")
    public String test(Model model){
        model.addAttribute("msg","hello,spring");
        return "test";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--所有的HTML元素都可以被thymeleaf接管:“th:元素名”-->
<div th:text="${msg}"></div>

</body>
</html>

thymeleaf语法

  • Variable Expressions:${...}普通变量
  • Selection Variable Expressions:*
  • Message Expressions:#{...}国际化消息
  • Link URL Expressions:@
  • Fragment Expressions:~{...}片段表达式
posted @ 2026-03-01 18:08  Dominus  阅读(0)  评论(0)    收藏  举报