SpringBoot Web开发
回顾:
自动装配
1. 创建应用,选择模块
SpringBoot到底帮我们配置了什么?我们能不能进行修改?能修改那些东西?能不能扩展?
- xxxAutoConfiguration->向容器中自动配置组件
- 在spring.factories文件中
- xxxProperties->自动配置类,装配配置文件中自定义的一些内容!
现在要解决的问题:
- 导入静态资源…怎么导入?
- 首页
- jsp页面…模板引擎Thymeleaf
- 装配和扩展SpringMVC
- 视图解析器
- 增删改查
- 拦截器
- 国际化【了解】
- 实现中英文切换
一、静态资源
以下源码路径:
- WebMvcAutoConfiguration【重点】—>addResourceHandlers(方法)—>WebProperties—>Resources(方法)
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
- 以上路径都可以用来存放静态资源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
- 自定义静态资源(一旦使用了就会让原有的失效)——一般不使用
spring:
mvc:
static-path-pattern: /myUrl
二、首页如何定制
- 注:在Resources/templates目录下的所有页面,只能通过Controller来进行跳转。
- 这个需要模板引擎的支持【thymeleaf】
三、模板引擎【thymeleaf】
- 模板引擎的作用就是我们来写一个页面模板
- 以前用jsp,现在用thymeleaf
注:是前后端分离过程中的中间产物
结论:只要使用thymeleaf,只需要导入对应的依赖就可以了,我们将html放在我们的template目录下即可
<!--thymeleaf-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
如何使用thymeleaf
- 导入约束,命名空间
xmlns:th="http://www.thymeleaf.org">
- 所有的html元素都可以被thymeleaf替换接管
- th: 元素名
标准表达式语法
Simple expressions:(表达式语法)
Variable Expressions: ${...}:获取变量值;OGNL;
1)、获取对象的属性、调用方法
2)、使用内置的基本对象:#18
#ctx : the context object.
#vars: the context variables.
#locale : the context locale.
#request : (only in Web Contexts) the HttpServletRequest object.
#response : (only in Web Contexts) the HttpServletResponse object.
#session : (only in Web Contexts) the HttpSession object.
#servletContext : (only in Web Contexts) the ServletContext object.
3)、内置的一些工具对象:
#execInfo : information about the template being processed.
#uris : methods for escaping parts of URLs/URIs
#conversions : methods for executing the configured conversion service (if any).
#dates : methods for java.util.Date objects: formatting, component extraction, etc.
#calendars : analogous to #dates , but for java.util.Calendar objects.
#numbers : methods for formatting numeric objects.
#strings : methods for String objects: contains, startsWith, prepending/appending, etc.
#objects : methods for objects in general.
#bools : methods for boolean evaluation.
#arrays : methods for arrays.
#lists : methods for lists.
#sets : methods for sets.
#maps : methods for maps.
#aggregates : methods for creating aggregates on arrays or collections.
==================================================================================
Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
Message Expressions: #{...}:获取国际化内容
Link URL Expressions: @{...}:定义URL;
Fragment Expressions: ~{...}:片段引用表达式
Literals(字面量)
Text literals: 'one text' , 'Another one!' ,…
Number literals: 0 , 34 , 3.0 , 12.3 ,…
Boolean literals: true , false
Null literal: null
Literal tokens: one , sometext , main ,…
Text operations:(文本操作)
String concatenation: +
Literal substitutions: |The name is ${name}|
Arithmetic operations:(数学运算)
Binary operators: + , - , * , / , %
Minus sign (unary operator): -
Boolean operations:(布尔运算)
Binary operators: and , or
Boolean negation (unary operator): ! , not
Comparisons and equality:(比较运算)
Comparators: > , < , >= , <= ( gt , lt , ge , le )
Equality operators: == , != ( eq , ne )
Conditional operators:条件运算(三元运算符)
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
Special tokens:
No-Operation: _
thymeleaf语法
- 文档第十条里面
- 常用:
- 遍历数据:
@RequestMapping("/test")
public String test(Model model) {
model.addAttribute("users", Arrays.asList("kami", "dog"));
return "test";
}
<!--遍历出来的每个元素,展现到文本上-->
<!--第一种写法——推荐使用-->
<h3 th:each="users : ${users}" th:text="${users}"></h3>
<!--第二种写法-->
<!--<h3 th:each="users : ${users}">[[${users}]]</h3>-->
- 前端基本都用的是三元运算符来实现判断
四、装配和扩展SpringMVC【重点】
自定义视图解析器
//如果你想自定义一些定制化的功能,只要写这个组件,然后将它交给springboot,springboot就会自动装配
//全面扩展 springMVC
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//ViewResolver 实现了视图解析器接口的类,我们就可以把它看做是视图解析器
@Bean
public ViewResolver MyViewResolver(){
return new MyViewResolver();
}
//自定义了一个自己的试图解析器MyViewResolver(静态内部类)
public static class MyViewResolver implements ViewResolver{
@Override
public View resolveViewName(String s, Locale locale) throws Exception {
return null;
}
}
}
重写页面跳转
//如果我们要扩展springMVC,官方建议我们这样做
@Configuration
//如果我们要扩展springMVC,不能加下面这个注解
//@EnableWebMvc //这玩意就是导入了一个DelegatingWebMvcConfiguration.class类(从容器中获取所有的webMvcConfig)
public class MyMvcConfig implements WebMvcConfigurer {
//视图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/kami").setViewName("test");
}
}
总结:
- 在springboot中,有很多的xxx Configuration—>会帮助我们进行扩展配置,只要看到了这个东西,我们就要注意了!