Spring Boot学习(四)之web开发渲染页面 -- Velocity

来源:互联网 发布:sketchup看图软件 编辑:程序博客网 时间:2024/06/05 07:20

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

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

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

需把

1<dependencies> 
2    <dependency> 
3        <groupId>org.springframework.boot</groupId> 
4        <artifactId>spring-boot-starter-freemarker</artifactId> 
5    </dependency> 
6  </dependencies>

替换为

1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-velocity</artifactId>
4</dependency>

index.ftl改为index.vm

内容为:

01<!DOCTYPE html>
02<html>
03<head lang="en">
04    <meta charset="UTF-8" />
05    <title></title>
06</head>
07<body>
08Velocity模板
09<h1>${name}</h1>
10</body>
11</html>

启动项目:

访问http://localhost:8080/

结果显示为:

筱进GG

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

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

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


欢迎大家一起交流学习SpringBoot,java等领域的技术,交流群 : 587674051 博客的源码也在里面


原创粉丝点击