@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface VelocityLayout {
/**
* The value of {@link Velocity} Layout's URL
*
* @return value of {@link Velocity} Layout's URL
*/
String value();
}
Velocity Spring Boot Project
Spring Boot Starter 支持模板引擎 velocity 以及 velocity tools,该Starter是官方Starter的补充。
发布日志
English Document
发布版本
依赖和兼容性
功能特性
模板页面设置
模板页面资源根路径
模板页面文件前缀
模板页面文件后缀
举例说明
根据以上配置和Java代码为例, 当前请求(URL:”/“)访问时,Spring MVC 会通过路径
classpath:/templates/velocity/default/index.vm来寻找Velocity资源页面。Spring Boot 外部化配置模式
配置项模式
例如,传统 Velocity Properties 配置
Spring Boot Velocity 配置
若需了解更多 Velocity 宏内容,请参考官方 Velocity 宏章节
多资源加载器(
ResourceLoader)Velocity 支持多资源加载器(
ResourceLoader)配置,每种资源加载器均为org.apache.velocity.runtime.resource.loader.ResourceLoader的实例,其 意图在于提供多种Velocity模板资源方式,以突破传统渲染引擎资源加载单一的局限。资源加载器(
ResourceLoader)类层次关系资源加载器(
ResourceLoader)类层次关系如下:org.apache.velocity.runtime.resource.loader.ResourceLoaderorg.apache.velocity.runtime.resource.loader.ClasspathResourceLoaderorg.apache.velocity.runtime.resource.loader.FileResourceLoaderorg.apache.velocity.runtime.resource.loader.JarResourceLoaderorg.apache.velocity.runtime.resource.loader.StringResourceLoaderorg.apache.velocity.runtime.resource.loader.URLResourceLoaderorg.apache.velocity.runtime.resource.loader.DataSourceResourceLoaderorg.apache.velocity.tools.view.WebappResourceLoader( Velocity Tools 提供 )org.springframework.ui.velocity.SpringResourceLoader( Spring Framework 提供 )Spring Boot 多资源加载器(
ResourceLoader)配置当读者了解资源加载器(
ResourceLoader)类层次关系后,还需回顾传统的Velocity Properties 配置方式。更多传统配置细节,请参考官方资源管理章节
Spring Boot 继承了 Spring Framework 中的特性,其中
org.springframework.ui.velocity.VelocityEngineFactory将org.springframework.ui.velocity.SpringResourceLoader作为 默认资源加载器(ResourceLoader)配置到org.apache.velocity.app.VelocityEngine,因此,在配置过程中,请不要重复配置。org.springframework.ui.velocity.VelocityEngineFactory初始化后,等价传统 Velocity Properties 配置方式:
等价 Spring Boot Properties 配置方式:
Velocity Layout 支持
官方默认自动装配 org.springframework.web.servlet.view.velocity.VelocityViewResolver , 当前 Starter 将自动装配 org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver, 代替VelocityViewResolver , 对Layout 支持。
Layout 配置项
为了与保持官方继承配置项前缀 “spring.velocity.”,因此其他Velocity配置项与官方保持一致,
例如:
布局页面资源位置URL
layoutUrl : layout VM页面的URL路径。
配置项
布局页面资源位置的渲染上下文名称
layoutKey : 布局页面资源位置渲染上下文名称
配置项
Java代码
MVC View 渲染HTML内容的渲染上下文名称
screenContentKey : MVC View 渲染HTML内容的渲染上下文名称
配置项
Layout Velocity 代码
@VelocityLayout支持velocity-spring-boot-starter 1.0.0.RELEASE 版本开始提供Annotation
com.alibaba.boot.velocity.annotation.VelocityLayout,该Annotation 与Spring Web MVC 中的@Controller配合使用,覆盖默认velocity layout 页面URL (即配置项spring.velocity.layout-url)。@VelocityLayout定义@VelocityLayout 可以定义在 @Controller 类或其处理方法上。
@VelocityLayout处理逻辑当 @Controller 处理方法中,将配置项
spring.velocity.layout-key(默认值:”layout”) 的值作为上下文名称,如果该上下文名称在渲染上下文中或者模型(例如:org.springframework.ui.Model) 已存在的话,@VelocityLayout 处理将被忽略。否则,当@VelocityLayout 标记在 @Controller 处理方法上时, 将@VelocityLayout#value()值作为Velocity Layout URL。
否则,当@VelocityLayout 标记在 @Controller 类上时,其所有的处理方法均继承 @VelocityLayout#value()值作为Velocity Layout URL。
否则,配置项
spring.velocity.layout-url中定义的值作为Velocity Layout URL。@VelocityLayout实例代码分析根据
@VelocityLayout处理逻辑,“/layout1” 的Velocity Layou URL 使用了 Model 中的 “/layout/layout.vm” (Java Code :
model.addAttribute(velocityLayoutProperties.getLayoutKey(), "/layout/layout.vm"))“/layout2” 的Velocity Layou URL 使用了 处理方法layout2上的 @VelocityLayout(“/layout/layout2.vm”)
“/layout3” 的Velocity Layou URL 使用了 VelocityLayoutController 上的 @VelocityLayout(“/layout/default.vm”)
Layout 激活
配置项
Velocity 渲染支持
独占 VelocityViewResolver 配置
Spring Boot 自动装配 Spring WebMVC 所提供的
org.springframework.web.servlet.view.ContentNegotiatingViewResolver组 件,org.springframework.web.servlet.view.ContentNegotiatingViewResolver的作用是协调org.springframework.web.servlet.ViewResolver列表,其中com.alibaba.boot.velocity.web.servlet.view.EmbeddedVelocityLayoutViewResolver(org.springframework.web.servlet.ViewResolver实例)。Velocity
org.springframework.web.servlet.ViewResolver层次关系org.springframework.web.servlet.ViewResolver( spring-webmvc )org.springframework.web.servlet.view.velocity.VelocityViewResolver( spring-webmvc )org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver( spring-webmvc )org.springframework.boot.web.servlet.view.velocity.EmbeddedVelocityViewResolver( spring-boot )com.alibaba.boot.velocity.web.servlet.view.EmbeddedVelocityLayoutViewResolver( velocity-spring-boot-starter )Spring WebMVC 页面渲染执行时,
org.springframework.web.servlet.view.ContentNegotiatingViewResolver将org.springframework.web.servlet.ViewResolver列表顺序(通过顺序添加或者实现org.springframework.core.Ordered接口来决定次序)地处理,当获取org.springframework.web.servlet.View后,再执行渲染。因此,
org.springframework.web.servlet.view.velocity.VelocityViewResolver或其派生类无法保障在org.springframework.web.servlet.view.ContentNegotiatingViewResolver中 最优先执行。由此,velocity-spring-boot-starter1.0.1 版本开始,支持单一 Velocity 渲染配置,配置项相当简单:当
web-support.exclusive-view-resolver配置为velocityViewResolver时,org.springframework.web.servlet.view.ContentNegotiatingViewResolver的org.springframework.web.servlet.ViewResolver列表成员中仅存在org.springframework.web.servlet.view.velocity.VelocityViewResolver或其 派生类实例。Velocity Tools 支持
Velocity Spring Boot Starter 支持 Tools 2.0 功能,通用功能文档,请参考: http://velocity.apache.org/tools/2.0/generic.html
Tools 配置项
Velocity Tools 配置是以XML文件为载体,XML文件的搜索路径:
首先,搜索 ServletContext 根目录,如果找不到的话,搜索ClassPath。
配置项如下:
Tools XML 配置
Tools Annotation 配置
从 1.0.1 版本开始,
velocity-spring-boot-starter新增Tools Annotation 配置 方式,以简化配置方式(相对于 XML 方式)实现 Tool
velocity-spring-boot-starter使用了 Velocity Tools 2.0 ,该版本使用 Annotation 的方式来申明 Tool 对象,即在实现类上标记org.apache.velocity.tools.config.DefaultKey,其中属性org.apache.velocity.tools.config.DefaultKey#value()来决定渲染上下文的变量 名称。例如:
Java Code
装配 Tool
当 Tool 实现后,仍可以使用传统的 XML 方式来装配,推荐使用类似于 Spring
@ComponentScan方式将指定 base packages 下的 Tool 实现装配到 Velocity 渲染 上下文中,配置如下:执行 Tool
当
com.alibaba.boot.velocity.tools.EchoTool实现和配置完成后,Velocity 页 面中可直接使用该 Tool 对象,例如:Velocity Code
日志输出
下游工程