首页 技术 正文
技术 2022年11月14日
0 收藏 317 点赞 4,986 浏览 4766 个字

关键词:view technology、template、template engine、markup。内容较多,按需查用即可。

  1. 介绍
  2. Thymeleaf
  3. Groovy Markup Templates
    1. 配置
    2. 例子
  4. Velocity & FreeMarker
    1. 依赖
    2. Context配置 — 上下文配置
    3. 创建模板
    4. 高级配置
      1. velocity.properties
      2. FreeMarker
    5. 绑定支持和form处理
      1. bind marcos — 绑定宏
      2. simple binding — 简单绑定
      3. form input generation marcos — 生成表单输入的宏
      4. Input Fields
      5. Selection Fields
      6. HTML escaping and XHTML compliance — HTML转义和XHTML兼容
  5. JSP & JSTL
    1. View resolvers — 视图解析器
    2. ’Plain-old’ JSPs vs JSTL
    3. 额外的标签促进开发
    4. 使用Spring的form标签库
      1. Configuration
      2. The form tag
      3. The input tag
      4. The checkbox tag
      5. The checkboxes tag
      6. The radiobutton tag
      7. The radiobuttons tag
      8. The password tag
      9. The select tag
      10. The option tag
      11. The options tag
      12. The textarea tag
      13. The hidden tag
      14. The errors tag
      15. HTTP Method Conversion
      16. HTML5 Tags — H5 标签
  6. Script templates
    1. 依赖
    2. 如何集成基于脚本的模板
  7. XML Marshalling View(暂空)
  8. Tiles(暂空)
  9. XSLT(暂空)
  10. Document views (PDF/Excel)(暂空)
  11. JasperReports(暂空)
  12. Feed Views(暂空)
  13. JSON Mapping View(暂空)
  14. XML Mapping View (暂空)

Spring 4 官方文档学习(十一)Web MVC 框架之resolving views 解析视图 — 它覆盖了views如何耦合到MVC框架的基础。

Thymeleaf+Spring

Groovy Markup Template Engine 是Spring支持的另一个view技术。该模板引擎的主要目标是生成 类XML (XML, XHTML, HTML5,…)的标记,也可以被用于生成任意基于文本的内容。

嗯嗯,要求classpath中有 Groovy 2.3.1+。

VelocityFreeMarker是模板语言,可被用作Spring MVC application中的view技术。它们是非常相似的,并且服务于相似的需要,因此本部分将二者放在一起。关于二者的语法和语义的区别,见FreeMarker站点。

自Spring Framework 4.3起,对Velocity的支持已经是deprecated的了,原因是Apache Velocity project已经有6年没有活动的维护了!我们推荐Spring的FreeMarker支持,或者Thymeleaf–其自身带有Spring支持。

API documentation

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

  • Mustache running on Nashorn
  • React running on Nashorn
  • EJS running on Nashorn
  • ERB running on JRuby
  • String templates running on Jython
  • Nashorn Javascript engine is provided builtin with Java 8+. Using the latest update release available is highly recommended.

  • Rhino Javascript engine is provided builtin with Java 6 and Java 7. Please notice that using Rhino is not recommended since it does not support running most template engines.
  • JRuby dependency should be added in order to get Ruby support.
  • Jython dependency should be added in order to get Python support.
  • 你还应该添加相关的依赖。例如,对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

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/view.html#view-xml-marshalling

    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

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/view.html#view-jasper-reports

    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

    相关推荐
    python开发_常用的python模块及安装方法
    adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
    日期:2022-11-24 点赞:878 阅读:8,941
    Educational Codeforces Round 11 C. Hard Process 二分
    C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
    日期:2022-11-24 点赞:807 阅读:5,465
    下载Ubuntn 17.04 内核源代码
    zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
    日期:2022-11-24 点赞:569 阅读:6,281
    可用Active Desktop Calendar V7.86 注册码序列号
    可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
    日期:2022-11-24 点赞:733 阅读:6,094
    Android调用系统相机、自定义相机、处理大图片
    Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
    日期:2022-11-24 点赞:512 阅读:7,728
    Struts的使用
    一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
    日期:2022-11-24 点赞:671 阅读:4,765