Java基础、中级、高级、架构面试资料

spring-boot整合velocity的配置大全

JAVA herman 7694浏览 0评论
公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:xttblog2,发送下载链接帮助你免费下载!
本博客日IP超过2000,PV 3000 左右,急需赞助商。
极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:xttblog2,之前的微信号好友位已满,备注:返现
受密码保护的文章请关注“业余草”公众号,回复关键字“0”获得密码
所有面试题(java、前端、数据库、springboot等)一网打尽,请关注文末小程序
视频教程免费领
腾讯云】1核2G5M轻量应用服务器50元首年,高性价比,助您轻松上云

上一篇文章中提到了Spring-boot 提供了很多模板引擎,其中就包括 velocity 。本文将介绍 velocity 和 spring-boot 的整合使用。

pom 中引入 spring-boot-starter-velocity 配置

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>

接下来在src/main/resources 目录中新建application.properties文件,该文件中可以详细的配置 velocity 的相关属性。

# VELOCITY TEMPLATES (VelocityAutoConfiguration)
# Set whether HttpServletRequest attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.velocity.allow-request-override=false
# Set whether HttpSession attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.velocity.allow-session-override=false
# Enable template caching.
spring.velocity.cache=true
# Template encoding.
spring.velocity.charset=UTF-8
# Check that the templates location exists.
spring.velocity.check-template-location=true
# Content-Type value.
spring.velocity.content-type=text/html
# Name of the DateTool helper object to expose in the Velocity context of the view.
spring.velocity.date-tool-attribute=
# Enable MVC view resolution for this technology.
spring.velocity.enabled=true
# Set whether all request attributes should be added to the model prior to merging with the template.
spring.velocity.expose-request-attributes=false
# Set whether all HttpSession attributes should be added to the model prior to merging with the template.
spring.velocity.expose-session-attributes=false
# Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext".
spring.velocity.expose-spring-macro-helpers=true
# Name of the NumberTool helper object to expose in the Velocity context of the view.
spring.velocity.number-tool-attribute=
# Prefer file system access for template loading. File system access enables hot detection of template changes.
spring.velocity.prefer-file-system-access=true
# Prefix that gets prepended to view names when building a URL.
spring.velocity.prefix=
# Additional velocity properties.
spring.velocity.properties.*=
# Name of the RequestContext attribute for all views.
spring.velocity.request-context-attribute=
# Template path.
spring.velocity.resource-loader-path=classpath:/templates/
# Suffix that gets appended to view names when building a URL.
spring.velocity.suffix=.vm
# Velocity Toolbox config location. For instance `/WEB-INF/toolbox.xml`
spring.velocity.toolbox-config-location=
# White list of view names that can be resolved.
spring.velocity.view-names=

最终整个项目的目录是这样的。

src
├─main
│  ├─java
│  │  └─com
│  │      └─xttblog
│  │          │  Application.java
│  │          │
│  │          └─controller
│  │                  HomeController.java
│  │
│  └─resources
│      │  application.properties
│      │
│      └─templates
│              index.vm
│
└─test
    ├─java
    └─resources

业余草公众号

最后,欢迎关注我的个人微信公众号:业余草(yyucao)!可加作者微信号:xttblog2。备注:“1”,添加博主微信拉你进微信群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作也可添加作者微信进行联系!

本文原文出处:业余草: » spring-boot整合velocity的配置大全