当前位置: 首页 > news >正文

企业宣传网站制作在线建站网页制作网站建设平台

企业宣传网站制作,在线建站网页制作网站建设平台,外国做爰网站,有关做聚合物电池公司的网站目录 前言实现代码 前言 提取变量用正则也能做,但我看另一个项目用freemarker提取了模板的变量,就想着Velocity是不是也能做,搜了一圈没搜到,于是去debug了Velocity源码,发现变量最终会存在Node数组里。 实现代码 …

目录

  • 前言
  • 实现代码

前言

  • 提取变量用正则也能做,但我看另一个项目用freemarker提取了模板的变量,就想着Velocity是不是也能做,搜了一圈没搜到,于是去debug了Velocity源码,发现变量最终会存在Node数组里。
    在这里插入图片描述

实现代码

  • 我只要能获取到Node [] children数组对象的数据就可以了。
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.engine.velocity.VelocityEngine;
import cn.hutool.extra.template.engine.velocity.VelocityTemplate;
import com.zbkj.common.constants.Constants;
import org.apache.velocity.runtime.parser.node.ASTIdentifier;
import org.apache.velocity.runtime.parser.node.ASTReference;
import org.apache.velocity.runtime.parser.node.ASTprocess;
import org.apache.velocity.runtime.parser.node.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;/*** @author zzq* @description Velocity工具类* @date 6/12/2025 14:12:44*/
public class VelocityUtil {private static final Logger log = LoggerFactory.getLogger(VelocityUtil.class);private final static String RAW_TEMPLATE_FIELD = "rawTemplate";/*** 从Velocity模板字符串中提取变量名** @param templateStr 模板字符串* @return 变量名列表* @Date 2025-06-12*/public static Set<String> extractVariables(String templateStr) {// 实现从模板字符串中提取变量名的逻辑Set<String> variables = new HashSet<>(Constants.NUM_TWENTY);VelocityEngine engine = new VelocityEngine();Template template = engine.getTemplate(templateStr);Field rawTemplateField = ReflectionUtils.findField(VelocityTemplate.class, RAW_TEMPLATE_FIELD);ReflectionUtils.makeAccessible(rawTemplateField);org.apache.velocity.Template rawTemplate = (org.apache.velocity.Template) ReflectionUtils.getField(rawTemplateField, template);ASTprocess rawTemplateData = (ASTprocess) rawTemplate.getData();if (!ObjectUtils.isEmpty(rawTemplateData)) {int nodeLength = rawTemplateData.jjtGetNumChildren();for (int i = 0; i < nodeLength; i++) {StringBuilder sb = new StringBuilder();// 第一级节点Node child = rawTemplateData.jjtGetChild(i);if (child instanceof ASTReference) {ASTReference reference = (ASTReference) child;String referenceText = reference.getRootString();sb.append(referenceText);
//                    log.info("Reference: " + referenceText);int nodeGrandLen = reference.jjtGetNumChildren();// 子孙节点for (int j = 0; j < nodeGrandLen; j++) {Node grandChild = reference.jjtGetChild(j);if (grandChild instanceof ASTIdentifier) {ASTIdentifier identifier = (ASTIdentifier) grandChild;sb.append(StrUtil.DOT + identifier.getIdentifier());
//                            log.info("Identifier: " + identifier.getIdentifier());}}variables.add(sb.toString());}}}return variables;}public static void main(String[] args) {// 示例数据String templateData = "你是${name} xxx ${java.lang.String.XTEST} ${java.lang} ${STRING}";Set<String> extractedVariables = extractVariables(templateData);log.info("Extracted Variables: " + extractedVariables);}
}
  • 运行效果
    在这里插入图片描述

  • 本身项目是Spring Boot,其他程序依赖

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>4.5.7</version>
</dependency><!-- velocity代码生成使用模板 -->
<dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>2.3</version>
</dependency>
http://www.yidumall.com/news/83617.html

相关文章:

  • 公司logo怎么注册手机优化
  • 南昌做网站电话百度广告费一般多少钱
  • 做网站技术服务合同网络项目怎么推广
  • 专门做纪录片的网站友谊平台
  • 用子域名可以做网站吗深圳网站建设微信开发
  • 制作网站素材网络营销的概念及特点
  • 孔夫子旧书网网站谁做的重大军事新闻
  • 新公司做网站怎么做百度关键词点击工具
  • 永久免费网站虚拟主机十大seo公司
  • 南方数据企业网站管理系统9东莞网站建设优化
  • 任县网站制作sem账户托管外包
  • 网站建设微信软文厦门最快seo
  • 关键词网站建设推广网络推广的话术怎么说
  • 重庆市教育考试院门户网站链接购买
  • 新疆建设工程信息网站天津最新消息今天
  • wordpress+仿站步骤seo优化一般包括哪些内容
  • 免注册个人网站制作推广如何做网上引流
  • crm管理系统的优缺点上海外贸网站seo
  • wordpress 无法选择数据库福州seo
  • wordpress apcseo快速排名利器
  • 做壁纸网站好谷歌seo技巧
  • 网站建设合约seo教程有什么
  • 做网站多少分辨率好免费python在线网站
  • wordpress注册显示密码错误天津seo优化
  • 流量多网站服务推广软文
  • 网站注册免费百度公司网站推广怎么做
  • vps网站目录是灰色的百度文库网页版
  • 如何在微信公众号添加wordpressseo关键词挖掘
  • 网站根目录是哪个文件夹2022百度seo优化工具
  • 长安网站建设详细教程企业网站建站模板