将用户定义的java对象传递给velocity脚本引擎

发布于 2025-01-08 03:56:39 字数 272 浏览 4 评论 0原文

我正在使用带有 CQ5 的速度模板。我安装的速度脚本引擎可以识别预定义的 CQ 对象。我想知道如何将用户定义的java对象传递给速度脚本引擎。 我尝试了类似的东西: http://groovy.codehaus.org/JSR+223+Scripting+with+Groovy

但它不起作用..请帮我解决这个情况

提前致谢

im using velocity template with CQ5. the velocity scriptengine i ve installed identifies pre-defined CQ objects. i would like to know how to pass user defined java objects to the velocity script engine.
I tried something similar to this:
http://groovy.codehaus.org/JSR+223+Scripting+with+Groovy

but it doesnt work..Kindly help me to solve this situation

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉溺在你眼里的海 2025-01-15 03:56:40

您只需要使用 VelocityContext 来传递对象参数,例如 context.put("name_of_parameter", yourOBject);
在我的示例中 test.temalate$person.address 表示调用 person 对象的地址 getter 方法。

示例:尝试如下

Person.java
公共类人{
私有字符串名称;
私有字符串地址;

    public Person(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }
}

Test.java

import java.io.StringWriter;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;


public class Test {
    public static void main(String[] args) {
        VelocityEngine ve = new VelocityEngine();
        ve.init();
        Template template = ve.getTemplate("test.template");
        VelocityContext context = new VelocityContext();
        context.put("person", new Person("Jhon", "London"));
        StringWriter writer = new StringWriter();
        template.merge(context, writer);
        System.out.println(writer.toString());
    }
}

test.template

<table>
    <tr>
        <td>Name</td>
        <td>$person.name</td>
    </tr>
    <tr>
        <td>Address</td>
        <td>$person.address</td>
    </tr>
</table>

您将得到如下输出。

<table>
    <tr>
        <td>Name</td>
        <td>Jhon</td>
    </tr>
    <tr>
        <td>Address</td>
        <td>London</td>
    </tr>
</table>

You just need to use VelocityContext to pass object parameter like context.put("name_of_parameter", yourOBject);
In my example test.temalate , $person.address mean call address getter method of person object.

Example : Try as below

Person.java
public class Person {
private String name;
private String address;

    public Person(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }
}

Test.java

import java.io.StringWriter;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;


public class Test {
    public static void main(String[] args) {
        VelocityEngine ve = new VelocityEngine();
        ve.init();
        Template template = ve.getTemplate("test.template");
        VelocityContext context = new VelocityContext();
        context.put("person", new Person("Jhon", "London"));
        StringWriter writer = new StringWriter();
        template.merge(context, writer);
        System.out.println(writer.toString());
    }
}

test.template

<table>
    <tr>
        <td>Name</td>
        <td>$person.name</td>
    </tr>
    <tr>
        <td>Address</td>
        <td>$person.address</td>
    </tr>
</table>

You will get output as below.

<table>
    <tr>
        <td>Name</td>
        <td>Jhon</td>
    </tr>
    <tr>
        <td>Address</td>
        <td>London</td>
    </tr>
</table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文