velocity模板引擎本身提供了基本的语法层面的解析,在实际的使用中还有很多可以扩展的工具来简化开发中遇到的一些问题,比如数据的格式化、页面相关的路径处理等。
开发环境
- spring
- spring boot
- velocity
- spring boot mvc
- mybatis
使用指南
<?xml version="1.0" encoding="UTF-8"?>
<tools>
<data type="string" key="version" value="2.0"/>
<toolbox scope="request">
<tool class="org.apache.velocity.tools.view.CookieTool"/>
<tool class="org.apache.velocity.tools.view.ImportTool"/>
<tool class="org.apache.velocity.tools.view.IncludeTool"/>
<tool class="org.apache.velocity.tools.view.LinkTool"/>
<tool class="org.apache.velocity.tools.view.PagerTool"/>
<tool class="org.apache.velocity.tools.view.ParameterTool"/>
<tool class="org.apache.velocity.tools.view.ViewContextTool"/>
<tool class="org.apache.velocity.tools.generic.ContextTool"/>
<tool class="org.apache.velocity.tools.generic.LoopTool"/>
<tool class="org.apache.velocity.tools.generic.RenderTool"/>
</toolbox>
<toolbox scope="session" createSession="false">
<tool class="org.apache.velocity.tools.view.BrowserTool"/>
</toolbox>
<toolbox scope="application">
<tool class="org.apache.velocity.tools.generic.AlternatorTool"/>
<tool class="org.apache.velocity.tools.generic.ClassTool"/>
<tool class="org.apache.velocity.tools.generic.ComparisonDateTool"/>
<tool class="org.apache.velocity.tools.generic.ConversionTool"/>
<tool class="org.apache.velocity.tools.generic.DisplayTool"/>
<tool class="org.apache.velocity.tools.generic.EscapeTool"/>
<tool class="org.apache.velocity.tools.generic.FieldTool"/>
<tool class="org.apache.velocity.tools.generic.MathTool"/>
<tool class="org.apache.velocity.tools.generic.NumberTool"/>
<tool class="org.apache.velocity.tools.generic.ResourceTool"/>
<tool class="org.apache.velocity.tools.generic.SortTool"/>
<tool class="org.apache.velocity.tools.generic.XmlTool"/>
<tool class="org.apache.velocity.tools.generic.ListTool"/>
<tool class="org.apache.velocity.tools.generic.DateTool"/>
</toolbox>
</tools>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 添加配置
基本的使用中将配置好的属性放到/WEB-INF目录下,并在application.properties中添加对应的配置:spring.velocity.toolbox-config-location=WEB-INF/toolbox.xml - 列表内容
- 添加配置类
import org.springframework.boot.autoconfigure.velocity.VelocityProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.view.velocity.VelocityViewResolver
@Configuration
public class VelocityConfig {
@Bean(name = "velocityViewResolver")
public VelocityViewResolver velocityViewResolver(VelocityProperties properties) {
VelocityViewResolver viewResolver = new VelocityViewResolver()
viewResolver.setViewClass(VelocityLayoutToolboxView.class)
properties.applyToViewResolver(viewResolver)
return viewResolver
}
}
类2:
public class VelocityLayoutToolboxView extends VelocityToolboxView {
private VelocityLayoutToolboxView() {
}
@Override
protected Context createVelocityContext(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
ViewToolContext ctx = new ViewToolContext(this.getVelocityEngine(), request, response, this.getServletContext());
if (this.getToolboxConfigLocation() != null) {
XmlFactoryConfiguration factory = new XmlFactoryConfiguration();
factory. read(ResourceUtils.getURL(getToolboxConfigLocation()).openStream());
ToolboxFactory toolboxFactory = factory.createFactory();
toolboxFactory.configure(factory);
Collection<ToolboxConfiguration> toolboxes = factory.getToolboxes();
for (ToolboxConfiguration tc : toolboxes) {
ctx.addToolbox(toolboxFactory.createToolbox(tc.getScope()));
}
}
if (model != null && !model.isEmpty()) {
ctx.putAll(model);
}
return ctx;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
配置完成后,页面就可以使用工具类了。
常用工具类说明
https://velocity.apache.org/tools/devel/
https://github.com/apache/velocity-tools
https://velocity.apache.org/tools/releases/2.0/summary.html
配置属性:autlAlternate (),boolean 类型,默认是true
方法和属性:
A:#set( $alt = $alternator.auto(value1,value2,...) );创建一个自动的交替符
B:#set( $alt = $alternator.make(value1,value2,...) );创建一个默认的交替符
C:#set( $alt = $alternator.manual(value1,value2,...) );创建一个手动的交替符
4、ContextTool:取当前页面的值
工具使用超链接格式;这个工具有点不寻常的在每一个方法,参数将返回一个新实例的工具的一个副本的方法之一是呼吁,有额外的改变指定的方法调用。
官方参考文档:
http://velocity.apache.org/tools/devel/view/LinkTool.html
工具访问resourcebundle的和格式化消息,大多数方法都返回一个新对象,大多与原始相同的方法,允许您建立参数优雅和简单,而不是试图记住如何使用方法与许多参数,必须在一个特定的顺序:所以,你可以访问资源的关键“你好。谁”“otherStuff”包与一个消息这样的论点:美元text.hello.whoever.bundle(otherStuff).insert(世界)而不是这样的:text.get美元(“你好。谁”、“otherStuff“零美元,”世界”)
18、ImportTool:通用text-importing视图的工具模板
19、IncludeTool:工具允许透明的内容协商的方式模仿Apache httpd MultiViews方法;读取默认语言的ViewToolContext org.apache.velocity.tools.view.i18n。defaultLanguage;请注意,它并不实际的# include或#为你解析,但仅仅是帮助包括内容协商
23:ViewContextTool:扩展的通用ContextTool包括从HttpServletRequest键和值,HttpSession和ServletContext属性