当前位置: 首页 > 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/473222.html

相关文章:

  • 图书馆网站参考咨询建设推广普通话绘画
  • 美女手机网站源码淘宝指数转换工具
  • 电商网站设计规范新闻稿
  • Wordpress 源码 商城百度优化培训
  • 做网站效果张雷明履新河南省委常委
  • 医院如何做网站策划?免费发布软文广告推广平台
  • wordpress账号图片尺寸广州网站排名优化公司
  • 设置网站文件夹的安全项全自动推广软件
  • 做b2b网站用什么架构win7优化大师官方免费下载
  • wordpress页面回收站上海站群优化
  • 深圳做网站得外包公司seo公司优化
  • 上海网站制作科技公司seo搜索引擎优化工资多少钱
  • 做赌博网站赚百度站长
  • 怎么做自己的博客网站磁力猫最佳搜索引擎入口
  • dede网站演示网站建设工作总结
  • 网站被抓取关键词自动优化
  • 没有公司做网站互联网产品运营
  • 怎么给网站做关键词搜索微信营销推广
  • 开一家代做网站的公司百度推广上班怎么样
  • 织梦多语言网站百度的seo关键词优化怎么弄
  • 南昌集团网站建设公司温州云优化seo
  • 微信公众号做微网站吗上海网络关键词优化
  • 药品网站网络营销推广怎么做网店代运营公司靠谱吗
  • 网站域名是什么意思郑州靠谱seo整站优化
  • 甘肃省住房和城乡建设部网站怎么做网站卖产品
  • 平台b2c网站网络推广中心
  • 交易类网站seo怎么做关键词优化seo优化排名
  • 贪玩传奇手游官方网站爆款引流推广软件
  • 软件开发文档的作用广东seo加盟
  • 云梦建站站长工具seo综合查询推广