SpringBoot Web开发
- 自动装配:springboot到底帮我们配置了什么,我们能不能进行修改,能修改哪些东西,能不能扩展?
- xxxAutoConfiguration:向容器中自动配置组件
- xxxProperties:自动配置类,装配配置文件中自定义的一些内容
- 要解决的问题:
静态资源
- 在springboot中,我们可以使用以下方式处理静态资源
- webjars:
localhost:8080/webjars/xxx
- classpath目录下的pubilc,static,/**,resource:
localhost:8080/xxx
- webjars:
首页如何定制
- 新建一个index.html,放在静态资源目录下的任一目录即可
- 在静态资源目录下的任一目录图标放favicon.ico
- 关闭springboot默认图标
spring.mvc.favicon.enabled=false
- 关闭springboot默认图标
thymeleaf模板引擎
-
[taɪmliːf],百里香叶
-
导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--或者--> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency>
-
需要在html文件中导入命名空间约束
<html lang="en" xmlns:th="http://www.thymeleaf.org">
-
或者idea安装thymeleaf插件一键导入
-
测试
model.addAttribute("msg","hello,springboot");
<body> <!--所有的html元素都可以被thymeleaf替换接管:th:元素名--> <div th:text="${msg}"></div> </body>
thymeleaf基本语法
-
测试
-
model.addAttribute("msg","<h1>hello,springboot</h1>");
-
<!--不解析html和解析html--> <div th:text="${msg}"></div> <div th:utext="${msg}"></div>
-
结果:
-
model.addAttribute("msgs", Arrays.asList("123","abc","hhh"));
-
<div th:each="msg:${msgs}" th:text="${msg}"></div> <!--或者--> <div th:each="msg:${msgs}">[[${msg}]]</div>
-
结果:
-
-
URL的使用
-
绝对网址,绝对URL用于创建到其他服务器的链接,需要指定协议名称
http
或者https
,如:<a th:href="@{https://www.baidu.com}">百度</a>
-
上下文相关URL,即与项目根相关联的URL,这里
@{/}
就是项目根目录,如:<a th:href="@{/blog/search}">跳转</a>
-
服务器相关URL,它与上下文路径很相似,它主要用于一个Tomcat下运行有多个项目的情况。比如我们当前项目为
app
,如果tomcat还运行着一个otherApp
,我们就可以通过该方法访问otherApp
的请求路径-
<!-- 在页面这样写 --> <a th:href="@{~/otherApp/blog/search}">跳转</a> <!-- Thymeleaf解析之后是这样的 --> <a href="/otherApp/blog/search">跳转</a>
-
-
有时候我们需要页面带参数传递到后端,则可以使用下面这个方法
-
<!-- 在页面这样写 --> <a th:href="@{/blog/search(id=3,blogName='Java')}" >跳转</a> <!-- Thymeleaf解析之后是这样的 --> <a href="/blog/search?id=3&blogName=Java" >跳转</a> <!-- 还可以实现restful风格的效果 --> <a th:href="/blog/{id}/search(id=3&blogName=Java)">跳转</a> <!-- Thymeleaf解析之后是这样的 --> <a href="/blog/3/search?blogName=java">跳转</a>
-
-
SpringBoot使用fastJSON
-
导入依赖
-
导入bean
@Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 1.定义一个converters转换消息的对象 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); // 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 3.在converter中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); // 4.将converter赋值给HttpMessageConverter HttpMessageConverter<?> converter = fastConverter; // 5.返回HttpMessageConverters对象 return new HttpMessageConverters(converter); }
MVC装配扩展
-
自定义视图解析器
@Configuration public class MyMvcConfig implements WebMvcConfigurer { //ViewResolver实现了视图解析器接口的类,我们就可以把它看做视图解析器 @Bean public ViewResolver myViewResolver(){ return new MyViewResolver(); } //自定义了一个自己的视图解析器MyViewResolver public static class MyViewResolver implements VieResolver { @Override public View resolveViewName(String viewName, Locale locale) throws Exception { return null; } } }
-
自定义视图跳转
@Configuration public class MyMvcConfig implements WebMvcConfigurer { //视图跳转 @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/test").setViewName("test"); } }
-
自定义配置日期格式化
#自定义配置日期格式化 spring: mvc: format: date-time: yyyy-mm-dd
-
为什么接管MVC的时候不能加@EnableWebMvc
- 首先,@EnableWebMvc表示完全自己控制mvc配置,也就是说所有配置自己重写,所有默认配置都没了
- @EnableWebMvc:这个注解导入了一个类
DelegatingWebMvcConfiguration
DelegatingWebMvcConfiguration
作用:从容器中获取所有的webmvcconfigDelegatingWebMvcConfiguration
继承了WebMvcConfigurationSupport
- 一旦加了这个注解容器中就会放入这个类,而一旦拥有这个类
- 在
WebMvcAutoConfigration
类中有@ConditionalonMissingBean(WebMvcConfigurationsupport.class)
- 就会导致WebMvc的自动配置失效
-
总结
- SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(如果用户自己配置@bean),如果有就用用户配置的,如果没有就用自动配置的,如果有些组件可以存在多个,比如我们的视图解析器,就将用户配置的和自己默认的组合起来
- 说白了就是重写方法增加组件和修改默认配置
自定义Starter
-
命名归约:
- 官方命名:
- 前缀:spring-boot-starter-xxx
- 比如:spring-boot-starter-web
- 自定义命名:
- xxx-spring-boot-starter
- 比如:mybatis-spring-boot-starter
- 官方命名:
-
一个完整的Spring Boot Starter可能包含以下组件:
- autoconfigure模块:包含自动配置的代码,一般会放在spring-boot-autoconfigure-2.4.5.jar目录里
- starter模块:提供对autoconfigure模块的依赖,以及一些其它的依赖
- 事实上,starter是一个空jar。它唯一的目的是提供这个库所必须的依赖。你的starter必须直接或间接引用核心的Spring Boot starter(spring-boot-starter)
-
编写启动器
-
在IDEA中新建一个空项目 spring-boot-starter-diy
-
新建一个普通Maven模块:study-spring-boot-starter
-
新建一个Springboot模块:
-
在starter中导入autoconfigure的依赖
<!-- 启动器 --> <dependencies> <!-- 引入自动配置模块 --> <dependency> <groupId>com.study</groupId> <artifactId>study-spring-boot-starter-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
-
建议在autoconfigure模块中包含下列依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure-processor</artifactId> <optional>true</optional> </dependency>
-
starter项目pom看情况加入
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
-
-
将autoconfigure项目下多余的文件都删掉,Pom中只留下一个starter,这是所有的启动器基本配置
-
编写一个自己的服务和配置类
public class HelloService { HelloProperties helloProperties; public HelloProperties getHelloProperties() { return helloProperties; } public void setHelloProperties(HelloProperties helloProperties) { this.helloProperties = helloProperties; } public String sayHello(String name) { return helloProperties.getPrefix() + name + helloProperties.getSuffix(); } }
@ConfigurationProperties(prefix = "study.hello") public class HelloProperties { private String prefix; private String suffix; public String getPrefix() { return prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } }
-
编写我们的自动配置类并注入bean,测试
@Configuration @ConditionalOnWebApplication //web应用生效 @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration { private HelloProperties helloProperties; @Autowired public void setHelloProperties(HelloProperties helloProperties) { this.helloProperties = helloProperties; } @Bean public HelloService helloService(){ HelloService service = new HelloService(); service.setHelloProperties(helloProperties); return service; } }
-
在resources编写一个自己的META-INF\spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.study.HelloServiceAutoConfiguration
-
编写完成后,可以安装到maven仓库中
- 两个项目都安装
- 两个项目都安装
-
新建一个测试的SpringBoot项目,导入自己写的启动器
<dependency> <groupId>com.study</groupId> <artifactId>study-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
-
编写一个HelloController进行测试我们自己的写的接口
@RestController public class HelloController { private HelloService helloService; @Autowired public void setHelloService(HelloService helloService) { this.helloService = helloService; } @RequestMapping("/hello") public String hello(){ return helloService.sayHello("666"); } }
-
编写配置文件 application.properties
study.hello.prefix="cbc" study.hello.suffix="666"
-
查看结果
-