内容简介:prefix + view-name + suffixview.setUrl(getPrefix() + viewName + getSuffix())默认 Cache = true
prefix + view-name + suffix
classpath:/templates/thymeleaf/index.dota2 复制代码
view.setUrl(getPrefix() + viewName + getSuffix())
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 -> 设置缓存时间
Spring MVC 模板渲染逻辑
Spring MVC 核心总控制器 DispatcherServlet
-
C :
DispatcherServlet -
M
-
Spring MVC(部分)
@ModelAttribute
-
模板引擎(通常)
-
通用的方式
-
模板上下文
- 内建/内嵌自己 工具 变量
-
模板上下文
-
JSP 内置( built-in )九大变量
-
Servlet Scope 上下文(Spring
@Scope)-
PageContext(page 变量)
- 关注当前页面
-
A forward B
- A 变量只能 A 页面使用,不能共享给 B
- A t 和 B t 可以采用同名变量,同时使用
-
Servlet Request(请求上下文) -
WebApplicationContext#SCOPE_REQUEST-
关注当前请求
-
A forward B
- A 请求变量可以用于 B 页面
-
A forward B
-
关注当前请求
-
Servlet Session(会话上下文) -
WebApplicationContext#SCOPE_SESSION-
关注当前会话
-
A forward / redirect B
- A 请求变量可以用于 B 页面
-
A forward / redirect B
-
关注当前会话
-
Servlet ServletContext(应用上下文) -
WebApplicationContext#SCOPE_APPLICATION-
关注当前应用
- 用户 A 和 用户 B 会话可以共享
-
关注当前应用
-
PageContext(page 变量)
-
JSP 内置变量( JSP = Servlet )
- out(Writer = ServletResponse#getWriter())
- exception ( Throwable)
- config( ServletConfig )
- page ( Jsp Servlet 对象)
- response(ServletResponse)
-
Thymeleaf 内置变量
StandardExpressionObjectFactory-> 构建IExpressionContext- 上下文(模型)
- ####strings
- #numbers
- ...
-
-
-
V:
-
视图对象
-
Servlet
RequestDispatcher#forward RequestDispatcher#include HttpServletResponse#sendRedirect
-
Spring MVC
-
View-
forward:
-
InternalResourceView
-
-
redirect:
-
RedirectView
-
-
forward:
-
-
Struts
-
ActionForwardAction RedirectAction
-
-
-
视图处理对象
-
Spring MVC
-
*.do ->
DispatcherServlet->Controller->View->ViewResolver->View#render-> HTML ->HttpServletResponse-
Thymeleaf
-
ViewResolver->ThymeleafViewResolver -
View->ThymeleafView -
通过模板名称解析模板资源(
ClassPathResource)-
TemplateResolution
-
-
读取资源,并且渲染内容 HTML
IEngineContext ProcessorTemplateHandler
- HTML 内容输出到 Response
-
源码路径
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-
Foward ->
RequestDispatcher
-
Foward ->
-
-
-
-
Struts
-
*.do ->
ActionServlet-> Action -> ForwardAction -> RequestDispatcher -> JSP(Servlet) -> HTML ->HttpServletResponse
-
*.do ->
-
-
学习技巧
学会配置代码
假设需要了解的技术是 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
Spring MVC
-
Locale
-
Servlet
-
ServletRequest#getLocale()
-
Accept-Language: en,zh;q=0.9,zh-TW;q=0.8
-
-
ServletRequest#getLocale()
-
LocaleContextHolder-
来自于
-
DispatcherServlet-
FrameworkServlet#initContextHolders
-
-
-
存储是
ThreadLocal
-
来自于
-
Servlet
Spring Boot
-
MessageSource-
MessageSourceAutoConfiguration-
MessageSourceProperties- message.properties
- message_en.properties
- message_zh.properties
- message_zh_CN.properties
- message_zh_TW.properties
-
-
-
Thymeleaf
- #key => messageSource.get
问答环节
JSP 为什么 Spring 抛弃
-
Java EE 和 Spring 竞争关系的
-
Spring 想独立门户
-
WebFlux
- Servlet 3.1
- Reactor +Netty
-
@Controller、@RequestParam- Spring Web MVC
- Spring WebFlux
-
不再看到 Servlet API
- ServletRequest
- ServletResponse
-
WebFlux
JSP -> JSP 模板 -> 翻译 Servlet Java 源文件 -> 编译 Servlet Class -> Servlet 加载 -> Servlet 执行(字节码执行)
Thymeleaf -> Thymeleaf 模板 -> 解释执行模板表达式(动态运行时)
以上所述就是小编给大家介绍的《SpringBoot学习系列-Spring Web MVC 视图技术》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 系统学习iOS动画之一:视图动画
- 系统学习iOS动画之四:视图控制器的转场动画
- iOS小技巧·把子视图控制器的视图添加到父视图控制器
- CouchDB 视图简介及增量更新视图的方法
- c# – 将数据从部分视图传递到其父视图
- Django 基于函数的视图与基于类的视图
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
超级连接者:破解新互联时代的成功密码
伊桑•祖克曼(ETHAN ZUCKERMAN) / 林玮、张晨 / 浙江人民出版社 / 2018-8-1 / CNY 72.90
● 我们生活在一个互联互通的世界,我们需要辩证地看待某些事件,发现隐藏在背后的真相。着眼当下,看清彼此之间的联系,而非凭空幻想未来世界联系之紧密。数字世界主义要求我们承担起责任,让隐藏的联系变成现实。 ● 我们对世界的看法是局限的、不完整的、带有偏见的。如果我们想要改变从这个广阔的世界所获取的信息,我们需要做出结构性的改变。 ● 建立联系是一种新的力量。无论是在国家层面、企业层面还是个......一起来看看 《超级连接者:破解新互联时代的成功密码》 这本书的介绍吧!