关键词:view technology、template、template engine、markup。内容较多,按需查用即可。
- 介绍
- Thymeleaf
- Groovy Markup Templates
- Velocity & FreeMarker
- JSP & JSTL
- Script templates
- XML Marshalling View(暂空)
- Tiles(暂空)
- XSLT(暂空)
- Document views (PDF/Excel)(暂空)
- JasperReports(暂空)
- Feed Views(暂空)
- JSON Mapping View(暂空)
- XML Mapping View (暂空)
Spring 4 官方文档学习(十一)Web MVC 框架之resolving views 解析视图 — 它覆盖了views如何耦合到MVC框架的基础。
Groovy Markup Template Engine 是Spring支持的另一个view技术。该模板引擎的主要目标是生成 类XML (XML, XHTML, HTML5,…)的标记,也可以被用于生成任意基于文本的内容。
嗯嗯,要求classpath中有 Groovy 2.3.1+。
Velocity 和 FreeMarker是模板语言,可被用作Spring MVC application中的view技术。它们是非常相似的,并且服务于相似的需要,因此本部分将二者放在一起。关于二者的语法和语义的区别,见FreeMarker站点。
自Spring Framework 4.3起,对Velocity的支持已经是deprecated的了,原因是Apache Velocity project已经有6年没有活动的维护了!我们推荐Spring的FreeMarker支持,或者Thymeleaf–其自身带有Spring支持。
FreeMarker
通过设置FreeMarkerConfigurer bean的properties,即可将FreeMarker的 Settings 和 SharedVariables 可以被直接传给FreeMarker的Configuration对象(由Spring管理)。freemarkerSettings property需要一个java.util.Properties对象;freemarkerVariables则需要一个java.util.Map!如下:
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape"/>
</map>
</property>
</bean><bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>
详见FreeMarker文档。
How to Reference and Use JSTL in your Web Application 提供了一个很有用的指导,诸如常见陷阱、如何避免它们。注意,自Spring 3.0 起,最小支持的servlet版本是 2.4 (JSP 2.0, JSTL 1.1),这样会降低混淆的范围。
Handlebars running on Nashorn
Nashorn Javascript engine is provided builtin with Java 8+. Using the latest update release available is highly recommended.
你还应该添加相关的依赖。例如,对JavaScript来说,你应该使用WebJars来添加Maven/Gradle依赖,从而让你的js库在classpath中可用。
Handlerbars需要在使用模板之前先编译,还需要一个polyfill 以便模拟某些浏览器设施在服务器侧脚本引擎不可用。– fuck en!!!
@Configuration
@EnableWebMvc
public class MustacheConfig extends WebMvcConfigurerAdapter { @Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.scriptTemplate();
} @Bean
public ScriptTemplateConfigurer configurer() {
ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
configurer.setEngineName("nashorn");
configurer.setScripts("polyfill.js", "handlebars.js", "render.js");
configurer.setRenderFunction("render");
configurer.setSharedEngine(false);
return configurer;
}
}
当使用非线程安全的脚本引擎时,模板库不是设计用于并发的– 如运行在Nashorn上面的Handlebars或React,需要将sharedEngine property设为false。这种情况下,必须使用Java 8u60+,原因见这里:this bug。
polyfill.js 只定义了Handlebars需要的window对象:
var window = {};
基本的 render.js 实现,会在使用模板之前先编译。一个生产就绪实现应该也能存储和复用缓存的模板/预编译的模板。这可以在脚本侧完成,同时进行任何需要的定制(如管理目标引擎配置)。
function render(template, model) {
var compiledTemplate = Handlebars.compile(template);
return compiledTemplate(model);
}
更多配置样例,见Spring脚本模板单元测试 (java, resources)。
7、XML Marshalling View
略
8、Tiles
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/view.html#view-tiles
略
8.1、依赖
8.2、如何集成Tiles
UrlBasedViewResolver
ResourceBundleViewResolver (可以混合多种view技术)
SimpleSpringPreparerFactory 和 SpringBeanPreparerFactory
9、XSLT
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/view.html#view-xslt
略
10、Document views (PDF/Excel)
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/view.html#view-document
略
10.1、简介
10.2、Configuration and setup
Document view definitions
Controller code
Subclassing for Excel views
Subclassing for PDF views
11、JasperReports
JasperReports 这是一个强大的开源的报告引擎,支持使用简单易懂的XML文件格式来设计报告。JasperReports 支持四种不同的格式:CSV、Excel、HTML和PDF。
11.1、依赖
11.2、配置
配置ViewResolver
配置Views
关于Report文件
使用JasperReportsMultiFormatView
11.3、填充ModelAndView
11.4、使用Sub-Reports
配置Sub-Reports文件
配置Sub-Report数据源
11.5、配置Exporter Parameters
12、Feed Views
13、JSON Mapping View
14、XML Mapping View
官方文档链接:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/view.html