SpringBoot Web开发
回顾:
自动装配
1. 创建应用,选择模块
SpringBoot到底帮我们配置了什么?我们能不能进行修改?能修改那些东西?能不能扩展?
- xxxAutoConfiguration->向容器中自动配置组件
- 在spring.factories文件中
- xxxProperties->自动配置类,装配配置文件中自定义的一些内容!
现在要解决的问题:
- 导入静态资源…怎么导入?
- 首页
- jsp页面…模板引擎Thymeleaf
- 装配和扩展SpringMVC
- 视图解析器
- 增删改查
- 拦截器
- 国际化【了解】
- 实现中英文切换
一、静态资源
以下源码路径:
- WebMvcAutoConfiguration【重点】—>addResourceHandlers(方法)—>WebProperties—>Resources(方法)
- 以上路径都可以用来存放静态资源webjars,resources,static,public,/**(Resources根目录下的)【会自动查询以上目录】
根据上面建立静态资源目录
优先级:
resources>static>public
访问:
- 在Public中创建一个1.js
- 访问http://localhost:8080/1.js即可
各个目录的用途:
- public:一般放一些公共的资源(比如大家都会去访问的js)
- static:里面放一些静态资源和首页(比如图片)
- resources:放一些upload的上传文件
总结**:
- 在springboot,我们可以使用以下方式处理静态资源
- webjars
- 访问路径:localhost/8080/webjars/下面的目录结构
- 比如:localhost:8080/webjars/jquery/3.6.0/jquery.js
- resources,static,public,/**(Resources根目录下的)
- 访问路径:localhost:8080/
- 优先级:resources>static(默认)>public
- 自定义静态资源(一旦使用了就会让原有的失效)——一般不使用
二、首页如何定制
- 注:在Resources/templates目录下的所有页面,只能通过Controller来进行跳转。
- 这个需要模板引擎的支持【thymeleaf】
三、模板引擎【thymeleaf】
- 模板引擎的作用就是我们来写一个页面模板
- 以前用jsp,现在用thymeleaf
注:是前后端分离过程中的中间产物
结论:只要使用thymeleaf,只需要导入对应的依赖就可以了,我们将html放在我们的template目录下即可
如何使用thymeleaf
- 导入约束,命名空间
- 所有的html元素都可以被thymeleaf替换接管
- th: 元素名
标准表达式语法
thymeleaf语法
- 文档第十条里面
- 常用:
- 遍历数据:
- 前端基本都用的是三元运算符来实现判断
四、装配和扩展SpringMVC【重点】
自定义视图解析器
重写页面跳转
总结:
- 在springboot中,有很多的xxx Configuration—>会帮助我们进行扩展配置,只要看到了这个东西,我们就要注意了!