SpringBoot整合Velocity(十二)

二八佳人体似酥,腰间仗剑斩愚夫。虽然不见人头落,暗里教君骨髓枯。

上一章简单介绍了SpringBoot整合FreeMarker(十一),如果没有看过,请观看上一章

学习整合之前,可以看一下老蝴蝶写的关于Velocity应用的相关文章: Velocity的使用

SpringBoot 整合 Velocity

按照 SpringBoot 整合 BootStrap 的Maven方式创建相应的项目。

将模板 vm 文件放置在 resources/templates 目录下

image-20210805195552436

一. 一 pom.xml 添加相应的依赖

 <!--引入 spring-boot-starter-velocity的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-velocity</artifactId>
        </dependency>
        <!--添加一个webjar jquery-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!--引入bootstrap-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.4.1</version>
        </dependency>

注意, Velocity 整合时,只能使用低版本的SpringBoot,不能使用 2.0+ 版本。

老蝴蝶这儿使用 1.4.2.RELEASE 版本

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

一.二 application.yml 配置文件中配置 Velocity 的相关信息

server:
  port: 8081
  servlet-path: /Velocity
# 配置velocity
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true
    username: root
    password: abc123
    type: com.alibaba.druid.pool.DruidDataSource
  velocity :
    cache: false
    charset: UTF-8
    check-template-location: false
    content-type: text/html
    enabled: true
    prefix: /templates/
    suffix: .vm


一.三 配置资源映射

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    /**
     * 配置静态的资源信息
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        //映射 static 目录
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        //放置其他 业务页面资源
        registry.addResourceHandler("/**").addResourceLocations("classpath:/templates/");
    }
}

一.四 静态资源 index.vm

放置在 resources/templates 目录下

用的是 webjars 的 bootstrap 的样式。

<html>
<head>
    <title>Welcome ${web} </title>
    <link rel="StyleSheet" href="webjars/bootstrap/3.4.1/css/bootstrap.css" type="text/css">
</head>
<body class="container">
<h1>Welcome ${user}!</h1>
<p>Girl:
    <a href="${info.url}">${info.name}</a>!
<table class="table table-hover">
    <th>
    <td>id编号</td>
    <td>名称</td>
    <td>年龄</td>
    <td>性别</td>
    <td>描述</td>
    </th>
    #if(${userList})
        #foreach($u in $userList)

<tr>
    <td></td>
    <td>${u.id}</td>
    <td>${u.name}</td>
    <td>${u.age}</td>
<td>#if(${u.sex}==1)男 #else 女 #end </td>
    <td>${u.description}</td>
</tr>
 #end
## -- 如果为空的话, #else 表示为空的意义
#else
<tr>
    没有数据
</tr>
#end
</table>
<script type="text/javascript" src="webjars/jquery/3.5.1/jquery.js"></script>
<script type="text/javascript" src="webjars/bootstrap/3.4.1/js/bootstrap.js"></script>
</body>
</html>

一.五 InfoController 类

老蝴蝶这儿只列举一个简单的查询的方法

@Controller
public class InfoController {
    @RequestMapping("/index")
    public String info(Model model){
        model.addAttribute("web","FreeMarker展示信息");
        model.addAttribute("user","两个蝴蝶飞");
        Map<String,Object> info=new HashMap<>();
        info.put("url","www.yueshushu.top");
        info.put("name","周小欢");
        model.addAttribute("info",info);
        model.addAttribute("userList",getUserList());
        return "index";
    }
    // 不采用数据库查询的方法
    private List<User> getUserList() {
        List<User> userList=new ArrayList<>();
        for(int i=1;i<=10;i++){
            User user=new User();
            user.setId(i);
            user.setName("蝴蝶"+i);
            user.setAge(i*3+1);
            user.setSex(i%2);
            user.setDescription("一个简单的描述");
            userList.add(user);
        }
        return userList;
    }
}

一.六 输入网址,进行访问

http://localhost:8081/Velocity/index

image-20210805200138051

SpringBoot整合 Velocity 模板,整合成功。

本章节的代码放置在 github 上:

https://github.com/yuejianli/springboot/tree/develop/Springboot_Velocity

谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 Spring Boot 整合 Velocity 并使用自定义标签,可以按照以下步骤进行: 1. 导入相关依赖 在 `pom.xml` 文件添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-velocity</artifactId> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-tools</artifactId> <version>2.0</version> </dependency> ``` 2. 配置 Velocity 在 `application.properties` 文件添加以下配置: ```properties spring.velocity.resource-loader-path=classpath:/templates/ spring.velocity.toolbox-config-location=velocity.toolbox.xml ``` 其,`resource-loader-path` 配置 Velocity 模板所在的目录,`toolbox-config-location` 配置 Velocity 工具箱的配置文件路径。 3. 定义自定义标签 在 `velocity.toolbox.xml` 文件定义自定义标签,例如: ```xml <tools> <toolbox scope="application"> <tool key="myTag" class="com.example.MyTag"/> </toolbox> </tools> ``` 其,`key` 是标签名,`class` 是标签类的完整路径。 4. 实现自定义标签类 在项目创建 `MyTag` 类,并实现 `org.apache.velocity.tools.generic.SafeConfig` 接口和 `org.apache.velocity.tools.generic.Tool` 接口,例如: ```java public class MyTag implements SafeConfig, Tool { private String name; @Override public void configure(Map<String, Object> map) { this.name = (String) map.get("name"); } public String execute() { return "Hello, " + name + "!"; } } ``` 其,`configure` 方法用于获取配置信息,`execute` 方法用于执行标签的逻辑。 5. 在模板使用自定义标签 在模板使用自定义标签,例如: ``` <myTag name="world"/> ``` 这样就可以在模板使用自定义标签了。 以上就是 Spring Boot 整合 Velocity 并使用自定义标签的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两个蝴蝶飞

你的鼓励,是老蝴蝶更努力写作的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值