SpringBoot整合Velocity(十二)

简介: SpringBoot整合Velocity(十二)

SpringBoot 整合 Velocity


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


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


bf867fe8bef1b13b3cab376831b94638.png


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


18201f627e2401b1e7c9caf3036c0bfe.png


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


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


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


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


相关文章
|
Java 测试技术 Spring
SpringBoot 项目中使用velocity模板(转载)
(不要使用这种模板了,spring boot最新版已经不支持了。使用FreeMarker吧:http://blog.csdn.net/clementad/article/details/51942629) 简单几步,在spring boot中使用velocity模板生成文本:   1、引入依赖 [objc] view plain copy            org.
2989 0
|
缓存 Java
SpringBoot velocity 模板配置绝对路径的资源路径
velocity 配置模板路径是class path 下面相对的。 如果我们再boot 生产环境下,对应模板路径在class path 下那么将一并打包到jar 中。这样的情况我们就没有办法随时修改模板文件。这样对于一个产品维护是相当不方便的。那么就需要配置到一个jar 包的绝对路径中。这样我们可以随时修改,并且可以随时生效。 1.配置boot application.p
4044 0
|
2月前
|
JavaScript Java 关系型数据库
基于springboot的项目管理系统
本文探讨项目管理系统在现代企业中的应用与实现,分析其研究背景、意义及现状,阐述基于SSM、Java、MySQL和Vue等技术构建系统的关键方法,展现其在提升管理效率、协同水平与风险管控方面的价值。
|
2月前
|
搜索推荐 JavaScript Java
基于springboot的儿童家长教育能力提升学习系统
本系统聚焦儿童家长教育能力提升,针对家庭教育中理念混乱、时间不足、个性化服务缺失等问题,构建科学、系统、个性化的在线学习平台。融合Spring Boot、Vue等先进技术,整合优质教育资源,提供高效便捷的学习路径,助力家长掌握科学育儿方法,促进儿童全面健康发展,推动家庭和谐与社会进步。
|
2月前
|
JavaScript Java 关系型数据库
基于springboot的古树名木保护管理系统
本研究针对古树保护面临的严峻挑战,构建基于Java、Vue、MySQL与Spring Boot技术的信息化管理系统,实现古树资源的动态监测、数据管理与科学保护,推动生态、文化与经济可持续发展。
|
2月前
|
监控 安全 JavaScript
2025基于springboot的校车预定全流程管理系统
针对传统校车管理效率低、信息不透明等问题,本研究设计并实现了一套校车预定全流程管理系统。系统采用Spring Boot、Java、Vue和MySQL等技术,实现校车信息管理、在线预定、实时监控等功能,提升学校管理效率,保障学生出行安全,推动教育信息化发展。
|
2月前
|
人工智能 Java 关系型数据库
基于springboot的画品交流系统
本项目构建基于Java+Vue+SpringBoot+MySQL的画品交流系统,旨在解决传统艺术交易信息不透明、流通受限等问题,融合区块链与AI技术,实现画品展示、交易、鉴赏与社交一体化,推动艺术数字化转型与文化传播。
|
2月前
|
JavaScript Java 关系型数据库
基于springboot的高校运动会系统
本系统基于Spring Boot、Vue与MySQL,实现高校运动会报名、赛程安排及成绩管理的全流程信息化,提升组织效率,杜绝信息错漏与冒名顶替,推动体育赛事智能化发展。
|
2月前
|
JavaScript 安全 Java
基于springboot的大学生兼职系统
本课题针对大学生兼职信息不对称、权益难保障等问题,研究基于Spring Boot、Vue、MySQL等技术的兼职系统,旨在构建安全、高效、功能完善的平台,提升大学生就业竞争力与兼职质量。