Spring Web MVC 視圖技術java
實例化 -> Bean Class -> Bean Objectweb
初始化前 -> Bean before/pre init()spring
org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization
初始化 -> init()緩存
org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
初始化後bash
org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization()
銷燬jsp
org.springframework.beans.factory.DisposableBean#destroy()
@ModelAttrtibute
prefix + view-name + suffix工具
classpath:/templates/thymeleaf/index.dota2
複製代碼
view.setUrl(getPrefix() + viewName + getSuffix())post
Class<?> viewClass = this.getViewClass();
Assert.state(viewClass != null, "No view class");
AbstractUrlBasedView view = (AbstractUrlBasedView)BeanUtils.instantiateClass(viewClass);
view.setUrl(this.getPrefix() + viewName + this.getSuffix());
String contentType = this.getContentType();
if (contentType != null) {
view.setContentType(contentType);
}
view.setRequestContextAttribute(this.getRequestContextAttribute());
view.setAttributesMap(this.getAttributesMap());
Boolean exposePathVariables = this.getExposePathVariables();
if (exposePathVariables != null) {
view.setExposePathVariables(exposePathVariables);
}
Boolean exposeContextBeansAsAttributes = this.getExposeContextBeansAsAttributes();
if (exposeContextBeansAsAttributes != null) {
view.setExposeContextBeansAsAttributes(exposeContextBeansAsAttributes);
}
String[] exposedContextBeanNames = this.getExposedContextBeanNames();
if (exposedContextBeanNames != null) {
view.setExposedContextBeanNames(exposedContextBeanNames);
}
複製代碼
默認 Cache = true學習
Cache = false -> 設置緩存時間ui
Spring MVC 核心總控制器 DispatcherServlet
C :DispatcherServlet
M
Spring MVC(部分)
@ModelAttribute
模板引擎(一般)
通用的方式
JSP 內置( built-in )九大變量
Servlet Scope 上下文(Spring @Scope
)
WebApplicationContext#SCOPE_REQUEST
WebApplicationContext#SCOPE_SESSION
WebApplicationContext#SCOPE_APPLICATION
JSP 內置變量( JSP = Servlet )
Thymeleaf 內置變量
StandardExpressionObjectFactory
-> 構建 IExpressionContext
V:
視圖對象
Servlet
RequestDispatcher#forward
RequestDispatcher#include
HttpServletResponse#sendRedirect
Spring MVC
View
InternalResourceView
RedirectView
Struts
Action
ForwardAction
RedirectAction
視圖處理對象
Spring MVC
*.do -> DispatcherServlet
-> Controller
-> View
-> ViewResolver
-> View#render
-> HTML -> HttpServletResponse
Thymeleaf
ViewResolver
-> ThymeleafViewResolver
View
-> ThymeleafView
ClassPathResource
)
TemplateResolution
IEngineContext
ProcessorTemplateHandler
org.thymeleaf.TemplateEngine#process(org.thymeleaf.TemplateSpec, org.thymeleaf.context.IContext, java.io.Writer)
org.thymeleaf.engine.TemplateManager#parseAndProcess
JSP
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
複製代碼
ViewResolver
->InternalResourceViewResolver
View
-> JstlView
RequestDispatcher
Struts
ActionServlet
-> Action -> ForwardAction -> RequestDispatcher -> JSP(Servlet) -> HTML -> HttpServletResponse
假設須要瞭解的技術是 thymeleaf -> thymeleaf Properties -> ThymeleafProperties
第一步:找到 @ConfigurationProperties
,確認前綴
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
}
複製代碼
好比:「spring.thymeleaf」
第二步:進一步確認,是否字段和屬性名一一對應
spring.thymeleaf.cache
spring.thymeleaf.mode=HTML
複製代碼
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
...
private boolean cache = true;
...
private String mode = "HTML";
...
}
複製代碼
MessageSource
+ Properties = MessageSourceProperties
配置項前綴 - spring.messages
DispatcherServlet#doDispatch
-> 攔截請求
Controller
-> 執行業務,而且控制視圖
DispatcherServlet#resolveViewName
-> 視圖解析
DispatcherServlet#render
-> 視圖渲染
Locale
Accept-Language: en,zh;q=0.9,zh-TW;q=0.8
LocaleContextHolder
DispatcherServlet
FrameworkServlet#initContextHolders
ThreadLocal
MessageSource
MessageSourceAutoConfiguration
MessageSourceProperties
Thymeleaf
JSP 爲何 Spring 拋棄
Java EE 和 Spring 競爭關係的
Spring 想獨立門戶
@Controller
、@RequestParam
JSP -> JSP 模板 -> 翻譯 Servlet Java 源文件 -> 編譯 Servlet Class -> Servlet 加載 -> Servlet 執行(字節碼執行)
Thymeleaf -> Thymeleaf 模板 -> 解釋執行模板表達式(動態運行時)