【问题标题】:How to create velocity template for existing html?如何为现有的 html 创建速度模板?
【发布时间】:2023-04-09 00:25:01
【问题描述】:

我是 Apache Velocity 的新手。我想知道如何更新现有的 html 模板以将其转换为 Velocity 模板。当这个模板准备好时,它会被插入到数据库中,在运行时我们需要调用 Java 对象来替换变量。

假设我有一个这样的 jsp 文件:

<div style="float: left; margin: 0px auto; width: 36%; margin-top:18px;">
    <div class="btnAdd">
        <input type="button" id="savePopUp"
            class="save_drafts" class="save_drafts" value="Add >>"
        onclick="transferSelectedItems($('#lbFieldSelectInput'),$('#ldFieldSelectOutput'));">
    </div>
    <div class="btnAdd">
        <input type="button" id="savePopUp"
            class="save_drafts" class="save_drafts" value="Add All >>"
        onclick="transferAllItems($('#lbFieldSelectInput'),$('#ldFieldSelectOutput'));">
    </div>
    <div class="btnRemove">
        <input type="button" id="resetPopUp" class="save_drafts"
            value="<< Remove"
        onclick="transferSelectedItems($('#ldFieldSelectOutput'),$('#lbFieldSelectInput'));">
    </div>
    <div class="btnRemove">
        <input type="button" id="resetPopUp" class="save_drafts"
            value="<< Remove All"
        onclick="transferAllItems($('#ldFieldSelectOutput'),$('#lbFieldSelectInput'));">
    </div>
</div>  

如何插入变量以将其转换为 Velocity 模板?

【问题讨论】:

标签: java html velocity


【解决方案1】:

参考我的previous reply,要将其转换为 Velocity 模板,您必须清楚自己想要做什么。这是第 1 步。

然后您必须决定要以速度渲染哪个特定部分。在您的代码中,我想最好的部分是值。你可以支持价值观 您正在渲染到本地数据库(或文件),检索它们并将值放入上下文中。示例:-

String value1, value2, value3;
//Read the values as per required.
VelocityContext context = new VelocityContext();
context.put("value1", value1);
//...

在你的速度模板文件中:

    <div class="btnRemove">
        <input type="button" id="resetPopUp" class="save_drafts"
            value="$value1"
        onclick="transferSelectedItems($('#ldFieldSelectOutput'),$('#lbFieldSelectInput'));">
    </div>

有关更多信息和指导,请参阅 Velocity 文档。

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2021-04-13
    • 2013-04-14
    • 2019-11-13
    相关资源
    最近更新 更多