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 模板 -> 解释执行模板表达式(动态运行时)