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

企业网站类型爱站之家

企业网站类型,爱站之家,花店网站建设目的,夸克搜索目录 前言实现代码 前言 提取变量用正则也能做,但我看另一个项目用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.dtcms.com/wzjs/279634.html

相关文章:

  • 中华人民共住房和城乡建设部网站搜索优化引擎
  • 广州做网站公司排名公司软文怎么写
  • 太原网站制作哪家不错百度识图扫一扫入口
  • 网站开发平台工具百度推广seo怎么学
  • 集团网站建设定制网站建设品牌传播策划方案
  • 马关网站建设互联网营销平台
  • 贵阳做网站方舟网络站长之家查询的网址
  • 开家给别人做网站公司一周热点新闻
  • 封面设计网站上海seo网站推广
  • php做数据网站快速提高关键词排名的软件
  • app外包接活内蒙古seo优化
  • 漳州专业网站建设公司广州百度推广电话
  • 免费的网站如何建设sem网络营销
  • wordpress外贸网站建设西安网站seo
  • 做门户网站啥意思上海今日头条新闻
  • 简历模板免费网站怎么建立企业网站免费的
  • 网站报价系统南京seo推广
  • 贾汪网站建设个人发布信息免费推广平台
  • 南昌网站排名优化费用做推广公司
  • wordpress acf如何seo搜索引擎优化
  • 可以做设计的网站有哪些温州seo网站建设
  • 机关网站建设需求文档关键词挖掘工具网站
  • 企业网站程序源码福建企业seo推广
  • 钢化网手机系统优化
  • 网站系统制作福州网站排名推广
  • 公司建设网站需要什么百度服务商平台
  • 甘肃省建设工程网站seo百科
  • 徐州中小企业网站制作宣传推广方式
  • 成都网站建设设计公司推广员是做什么的
  • 深圳做网站的大公司郑州抖音推广