上一篇文章中提到了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
: » spring-boot整合velocity的配置大全
原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/251489.html