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

精品资料网官方网站河北网络推广服务网

精品资料网官方网站,河北网络推广服务网,荣耀手机正品官网查询,wordpress 3.8.3下载目录 前言实现代码 前言 提取变量用正则也能做#xff0c;但我看另一个项目用freemarker提取了模板的变量#xff0c;就想着Velocity是不是也能做#xff0c;搜了一圈没搜到#xff0c;于是去debug了Velocity源码#xff0c;发现变量最终会存在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 SetString extractVariables(String templateStr) {// 实现从模板字符串中提取变量名的逻辑SetString 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};SetString extractedVariables extractVariables(templateData);log.info(Extracted Variables: extractedVariables);} }运行效果 本身项目是Spring Boot其他程序依赖 dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion4.5.7/version /dependency!-- velocity代码生成使用模板 -- dependencygroupIdorg.apache.velocity/groupIdartifactIdvelocity-engine-core/artifactIdversion2.3/version /dependency
http://www.w-s-a.com/news/513208/

相关文章:

  • 吴江区建设用地申报网站包装设计模板设计素材
  • 快速建站的公司wordpress 元数据定义
  • 网站seo分析工具网站标题用空格 逗号影响seo
  • 基金项目实验室信息网站建设wordpress文章新窗口打开
  • php网站开发就业前景做网站推荐源创网络
  • wordpress 8211西安网站优化维护
  • 泰安招聘网站有哪些wordpress 回复提醒
  • 网站服务器不稳定怎么打开网页企业营销策划心得体会
  • 自己做视频网站会不会追究版权企业商城网站建设方案
  • 烟台网站制作计划网站做seo的好处
  • 网站首页轮播官方网站下载拼多多
  • 罗庄区建设局网站自己做网站推广产品
  • 优秀flash网站欣赏苏州吴中区建设局网站
  • 网站添加wordpress博客网上商城购物系统论文
  • 上海市建设安全协会网站王夑晟企业网站建设需要做些什么
  • 网站app 开发辽宁建设工程信息网官网新网站是哪个
  • 厦门建设企业网站建设wordpress添加形式
  • 建立什么网站可以赚钱室内设计效果图qq群
  • 厦门网站设计大概多少钱服务公司沈傲芳
  • 如何起手做网站项目百度站长app
  • dede 购物网站wordpress主题超限
  • 做啊录音网站WordPress音乐悬浮插件
  • 安徽建设厅证书查询网网站网站策划与运营课程认知
  • wordpress证书关闭重庆seo优化效果好
  • 直播网站建设模板网站活动怎么做的
  • 医院网站建设网站网站开发工资高嘛
  • 个人网站备案内容写什么做网站是不是涉及很多语言职
  • 怎么用手机做一个网站门户网站建设工作的自查报告
  • 网站搭建怎么收费浙江建设集团网站
  • 建网站怎么赚钱免费引流软件下载