標籤:技術 重寫 路徑 htm ipa ebs reg 通用 list
spring-booter-starter-web是spring-boot web發開的核心,自動設定資訊儲存在spring-boot-autoconfigure.jar 下面的web目錄裡面,包含了各種web自動設定,例如
ServerPropertiesAutoConfiguration ServerProperties 自動設定servlet容器
HttpEncodingAutoConfiguration 和 HttpEncodingProperties 用來自動設定http編碼
MultipartAutoConfiguration MultipartProperties 配置上傳檔案的屬性
WebMvnAutoConfiguration WebMvcProperties 配置spring mvc
1 Thymeleaf 模版引擎
spring-boot 可以使用jsp,但是內嵌的tomcat和jetty 不支援以jar的形式運行jsp,Undertow不支援jsp;同時spring-boot提供了很多模版引擎,包括常用的freemaker等,最推薦實用的是Thymeleaf,它提供了完美的springmvc支援。Thymeleaf是一個java類庫,是一個xml/xhtml/html5的模版引擎,可以視為mvc的web應用的view層,完全可以替代jsp。
2 整合spring mvc
在springmvc中,當對springmvc控制的資源發起請求時,這些請求會被springmvc的DispatherServlet處理,分析出HanderMapping中合適的映射,然後取得handlerAdapter 處理該handler,處理完了返回一個ModelAndView對象,最後把這個對象渲染成view返回給使用者。渲染的過程是靠ViewResolver和View進行的,ModelAndView中不包含真正的試圖,只返回一個邏輯試圖名稱,ViewResolver就會根據邏輯視圖名稱解析出真正的view對象,最後view對象進行視圖渲染返回給瀏覽器。項目中ViewResolver常用於配置路徑首碼和尾碼。 例如freemaker的viewresolver
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="text/html; charset=UTF-8"/>
<property name="cache" value="true"/>
<property name="prefix" value="WEB-INF/pages/"/>
<property name="suffix" value=".ftl"/>
</bean>
在spring-boot中,Thymeleaf定義了ThymeleafView(預設view)和 ThymeleafViewResolver以及通用的SpringTemplateEngine(引擎裡放置TemplateResolver),還提供了TemplateResolver用來設定通用的模版引擎(比如設定路徑前尾碼不用區分哪種模版)。
可以不用手動設定,自動注入autoconfigure.thymeleaf中對Thymeleaf進行了自動設定,包括了上面的TemplateResolver ThymeleafViewResolver SpringTemplateEngine ,在對應的properties檔案中,可以看到各種預設屬性配置
代碼中可以根據參數設定如下 ,action中使用model跟spring mvc一樣。
spring.thymeleaf.prefix=classpath:/pages/
spring.thymeleaf.suffix=.html
(1) springboot 會自動設定springmvc的resolver,WebMvcAutoConfiguration 裡面有自動設定。
1)ContentNegotiatinViewResolver 一個特殊的resolver,不自己處理View,而是交給其他resolver處理,具有最高優先順序,原理是拿到並遍曆所有resolver,根據contenttype選擇需要的resolver,resolver能處理什麼楊的contentType,可以設定在 spring.thymeleaf.content-type=text/html,預設都是這種頁面類型。如果多個resolver都能處理同一個contenttype,那麼選取優先順序最高的那一個。
2)BeanNameViewResolver 根據名稱選擇使用的resolver。action中返回一個字串,這個字串對應產生resolver bean的方法名,如。在action中,返回“jsonView”
3)InternalResourceViewResolver spring mvc最常用的resolver,通常用來設定前尾碼等。但是在spring-boot裡有坑。因為這個resolver的跳轉(forward dispatcher)是基於webinf的,而spring-boot的根目錄是classpath,所以這個resolver跳轉不到配置的classpath頁面。如果實在想用,需要加上web-inf,mvn設定成打war包,路徑寫在webinf下,使用外部tomcat容器啟動才行。 非常強烈推薦使用thymeleaf自己的resolver。這也是spring-boot不推薦使用jsp的原因,必須打成war包,而其他模版都是jar war都可。
(2)如果spring mvc 內建的配置不符合要求,可以通過一個配置類(@Configuration) 加上@EnableWebMvc 註解 來完全控制自己的mvc配置。如果只想拓展springmvc的額外配置,可以定義配置類繼承WebMvcConfigurerAdapter ,無需使用@EnableWebMvc,例如重寫 addViewControllers,用於不經過controller直接跳轉頁面,springboot 配置‘/‘ 映射到‘index.html‘,自己配置的跳轉也同時有效,推薦這種寫法。
(3)註冊servlet,filter,listener
spring boot提供兩種方法配置這些,一種是在類前面加 @WebServlet @WebListener @WebFilter (都要加配置路徑),然後配置一個scan,@Import(ServletComponentScanRegistrar.class),可以配置要掃描的包 ,最後在入口處加上@ServletComponentScan,即可掃描配置的內容。
另一種推薦配置,是@component 實現filter,然後在一個配置類裡 @Bean 註冊一個FilterRegistrationBean,這個bean裡配置名字,url路徑等。非常重要的一點就是,filter的執行順序和配置類@bean的從上到下配置順序一致,類似xml裡從上到下那種順序。
(4) 配置tomcat
用代碼可以配置servlet資訊,如果要配置通用的servlet,可以註冊一個實現EmbeddedServletContannerCustomizer介面的bean ;如果想要單獨配置tomcat ,jetty,undertow,就定一個bean 返回 TomcatEmbeddedServletContannerFactory,JettyEmbeddedServletContannerFactory,UndetowEmbeddedServletContannerFactory,裡面可以配置連接埠,session時間等。但是更方便的是直接在外部檔案裡配置server.port等,注意如果同時配置,檔案裡的配置優先順序會高於代碼優先順序。如果像替換tomcat,那麼在spring-boot-starter-web裡面排除掉spring-boot-starter-tomcat依賴,加上spring-boot-starter-jetty依賴即可。
spirng boot web配置開發