github:https://github.com/Ccww-lx/Sp... 模塊:spring-boot-starter-base-web
html
Web
開發是開發中相當重要的一部分, Web
開發的核心內容主要包括內嵌Servlet
容器和Spring MVC
。更重要的是,Spring Boot
`爲web開發提供了快捷便利的方式進行開發,使用依賴jar:
spring-boot-starter-web,提供了嵌入式服務器
Tomcat以及
Spring MVC的依賴,且自動配置
web相關配置,可查看
org.springframework.boot.autoconfigure.web`。git
Web
相關的核心功能:github
Thymeleaf
模板引擎Web
相關配置Tomcat
配置Favicon
配置 Spring Boot
提供了大量模板引擎, 包含括FreeMarker
、Groovy
、 Thymeleaf
、 Velocity和Mustache
, Spring Boot
中推薦
使用Thymeleaf
做爲模板引擎, 由於Thymeleaf
提供了完美的Spring MVC
的支持。 web
在Spring Boot
的org.springframework.boot.autoconfigure.thymeleaf
包下實現自動配置,以下所示:spring
ThymeleafAutoConfiguration
源碼:tomcat
@Configuration @EnableConfigurationProperties(ThymeleafProperties.class) @ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class }) @AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class }) public class ThymeleafAutoConfiguration { //配置TemplateResolver @Configuration @ConditionalOnMissingBean(name = "defaultTemplateResolver") static class DefaultTemplateResolverConfiguration { ... } //配置TemplateEngine @Configuration protected static class ThymeleafDefaultConfiguration { ... } //配置SpringWebFluxTemplateEngine @Configuration @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnProperty(name = "spring.thymeleaf.enabled", matchIfMissing = true) static class ThymeleafWebMvcConfiguration { ... } //配置thymeleafViewResolver @Configuration @ConditionalOnWebApplication(type = Type.REACTIVE) @ConditionalOnProperty(name = "spring.thymeleaf.enabled", matchIfMissing = true) static class ThymeleafWebFluxConfiguration { ... } ... }
ThymeleafAutoConfiguration
自動加載Web
所需的TemplateResolver
、TemplateEngine
、SpringWebFluxTemplateEngine
以及thymeleafViewResolver
,並經過ThymeleafProperties
進行Thymeleaf
屬性配置。詳細細節查看官方源碼。服務器
ThymeleafProperties
源碼:app
//讀取application.properties配置文件的屬性 @ConfigurationProperties(prefix = "spring.thymeleaf") public class ThymeleafProperties { private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8; public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html"; /** *Web模板文件前綴路徑屬性,Spring boot默認路徑爲classpath:/templates/ */ private String prefix = DEFAULT_PREFIX; /** * Web模板文件後綴屬性,默認爲html */ private String suffix = DEFAULT_SUFFIX; /** * Web模板模式屬性,默認爲HTML */ private String mode = "HTML"; /** * Web模板文件編碼屬性,默認爲UTF_8 */ private Charset encoding = DEFAULT_ENCODING; .... }
能夠從ThymeleafProperties
中看出,Thymeleaf
的默認設置,以及能夠經過前綴爲spring.thymeleaf
屬性修改Thymeleaf
默認配置。ide
1).根據默認Thymeleaf
配置,在src/main/resources/
下,建立static
文件夾存放腳本樣式靜態文件以及templates
文件夾存放後綴爲html的頁面,以下所示:spring-boot
2)index.html頁面
<!DOCTYPE html> <!-- 導入xmlns: th=http://www.thymeleaf.org命名空間 --> <html xmlns:th="http://www.thymeleaf.org"> <html lang="en"> <head> <meta charset="UTF-8"> <title>首面詳細</title> </head> <body> <div class="user" align="center" width="400px" height="400px"> message:<span th:text="${user.message}"/><br/> 用戶名:<span th:text="${user.username}"/><br/> 密碼:<span th:text="${user.password}"/> </div> </body> </html>
3).controller
配置:
@Controller public class LoginController { @Autowired private LoginService loginService; /** * 將首頁設置爲登錄頁面login.html * @return */ @RequestMapping("/") public String startIndex() { return "login"; } /** * 登錄驗證 * @param username * @param password * @param model * @return */ @RequestMapping("/login") public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model) { UserDTO userDTO = loginService.login(username, password); model.addAttribute("user", userDTO); return "index"; } }
web
相關配置根據WebMvcAutoConfiguration
以及WebMvcProperties
理解Spring Boot
提供的自動配置原理。
ViewResolver
以及靜態資源Spring boot
自動配置ViewResolver
:
ContentNegotiatingViewResolver
(最高優先級Ordered.HIGHEST_PRECEDENCE
)BeanNameViewResolver
InternalResourceViewResolver
靜態資源:
addResourceHandlers
方法默認定義了/static
、 /public
、 /resources
和/METAINF/resources
文件夾下的靜態文件直接映射爲/**
addFormatters
方法會自動加載Converter
、GenericConverter
以及Formatter
的實現類、並註冊到Spring MVC中,所以自定義類型轉換器只需繼承其三個接口便可。
自定義Formatter
:
/** * 將格式爲 ccww:ccww88轉爲UserDTO * * @Auther: ccww * @Date: 2019/10/4 16:25 * @Description: */ public class StringToUserConverter implements Converter<String, UserDTO> { @Nullable public UserDTO convert(String s) { UserDTO userDTO = new UserDTO(); if (StringUtils.isEmpty(s)) return userDTO; String[] item = s.split(":"); userDTO.setUsername(item[0]); userDTO.setPassword(item[1]); return userDTO; } }
HttpMessageConverters
(HTTP request
(請求)和response
(響應)的轉換器)configureMessageConverters
方法自動配置HttpMessageConverters:
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { this.messageConvertersProvider.ifAvailable((customConverters) -> converters .addAll(customConverters.getConverters())); }
經過加載由HttpMessageConvertersAutoConfiguration
定義的HttpMessageConverters
,會自動註冊一系列HttpMessage Converter
類,好比Spring MVC
默認:
ByteArrayHttpMessageConverter
StringHttpMessageConverter
ResourceHttpMessageConverter
SourceHttpMessageConverter
AllEncompassingFormHttpMessageConverter
自定義HttpMessageConverters
,只須要在自定義的HttpMessageConverters
的Bean
註冊自定義HttpMessageConverter
便可。
以下:
註冊自定義的HttpMessageConverter
:
@Configuration public class CustomHttpMessageConverterConfig { @Bean public HttpMessageConverters converter(){ HttpMessageConverter<?> userJsonHttpMessageConverter=new UserJsonHttpMessageConverter(); return new HttpMessageConverters(userJsonHttpMessageConverter); } }
自定義HttpMessageConverter
:
public class UserJsonHttpMessageConverter extends AbstractHttpMessageConverter<UserDTO> { private static Charset DEFUALT_ENCODE=Charset.forName("UTF-8"); public UserJsonHttpMessageConverter(){ super(new MediaType("application", "xxx-ccww", DEFUALT_ENCODE)); } protected boolean supports(Class aClass) { return UserDTO.class == aClass; } protected UserDTO readInternal(Class aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException { String message = StreamUtils.copyToString(httpInputMessage.getBody(), DEFUALT_ENCODE); String[] messages = message.split("-"); UserDTO userDTO = new UserDTO(); userDTO.setUsername(messages[0]); userDTO.setMessage(messages[1]); return userDTO; } protected void writeInternal(UserDTO userDTO, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException { String out = "ccww: " + userDTO.getUsername() + "-" + userDTO.getMessage(); httpOutputMessage.getBody().write(out.getBytes()); } }
同理,能夠將Servlet、Filter以及Listener相對於的註冊便可。
自定義的MVC配置類上加@EnableWebMvc
將廢棄到Spring boot
默認配置,徹底由本身去控制MVC
配置,但一般是Springboot
默認配置+所需的額外MVC
配置,只須要配置類繼承WebMvcConfigurerAdapter
便可
Tomcat
配置可使用兩種方式進行Tomcat
配置屬性
application.properties
配置屬性便可,Tomcat
是以"server.tomcat
"爲前綴的特有配置屬性,通用的是以"server
"做爲前綴;WebServerFactoryCustomizer
接口自定義屬性配置類便可,同理其餘服務器實現對應的接口便可。application.properties
配置屬性:
#通用Servlet容器配置 server.port=8888 #tomcat容器配置 #配置Tomcat編碼, 默認爲UTF-8 server.tomcat.uri-encoding = UTF-8 # Tomcat是否開啓壓縮, 默認爲關閉off server.tomcat.compression=off
實現WebServerFactoryCustomizer
接口自定義:
/** * 配置tomcat屬性 * @Auther: ccww * @Date: 2019/10/5 23:22 * @Description: */ @Component public class CustomTomcatServletContainer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { public void customize(ConfigurableServletWebServerFactory configurableServletWebServerFactory) { ((TomcatServletWebServerFactory)configurableServletWebServerFactory).addConnectorCustomizers(new TomcatConnectorCustomizer() { public void customize(Connector connector) { Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler(); protocol.setMaxConnections(200); protocol.setMaxThreads(200); protocol.setSelectorTimeout(3000); protocol.setSessionTimeout(3000); protocol.setConnectionTimeout(3000); protocol.setPort(8888); } }); } }
替換spring boot
默認Servle
t容器tomcat
,直接在依賴中排除,並導入相應的Servlet
容器依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starterweb</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-startertomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starterjetty</artifactId> </dependency
Favicon
自定義Favicon
只須要則只需將本身的favicon.ico
( 文件名不能變更) 文件放置在類路徑根目錄、 類路徑META-INF/resources/
下、 類路徑resources/
下、 類路徑static/
下或類路徑public/
下。
最後可關注公衆號:【ccww筆記】 一塊兒學習,天天會分享乾貨,還有學習視頻領取!