SpringBoot整合Velocity(十二)
  vXFRsFYydcMw 2023年11月02日 77 0


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

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

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

SpringBoot 整合 Velocity

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

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

SpringBoot整合Velocity(十二)_jar

一. 一 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​

SpringBoot整合Velocity(十二)_spring_02

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

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

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

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


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
vXFRsFYydcMw
最新推荐 更多