筱进GG的gravatar头像
筱进GG2017-11-08 14:16:36
Spring Boot学习(四)之web开发渲染页面 -- Velocity

上篇编写了Spring Boot学习(四)之web开发渲染页面 -- Freemarker

接下来简单介绍一下Velocity整合,跟Freemarker是一样的,没有什么太大区别

下面开始看pol. xml引入的jar

需把

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

替换为

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

index.ftl改为index.vm

内容为:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
Velocity模板
<h1>${name}</h1>
</body>
</html>

启动项目:

访问http://localhost:8080/

结果显示为:

筱进GG

 在spring boot的application.properties属性文件中为velocity提供了一些常用的配置,如下:

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

就大功搞成了,这个跟freemarker差不多不做过多的介绍了!

源码链接:Spring Boot学习(四)之web开发渲染页面 -- Velocity


打赏

已有2人打赏

程序猿全敏的gravatar头像最代码官方的gravatar头像

分享到:

最近浏览
1158756616 LV77月13日
月亮星星星星星星
lw19900921 LV197月4日
太阳星星星星星星
dns10007月3日
暂无贡献等级
浙江螃蟹 LV37月2日
星星星星星星
水心木支 LV46月25日
月亮
coding喵 LV136月13日
月亮月亮月亮星星
itplayboy LV36月7日
星星星星星星
zp1321088 LV25月15日
星星星星
zhos0212 LV155月10日
月亮月亮月亮星星星星星星
liujiajin LV114月9日
月亮月亮星星星星星星
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友