2012-02-22 5 views
0

im CQ5で速度テンプレートを使用する。インストールされた速度scriptengineはあらかじめ定義されたCQオブジェクトを識別します。私は、速度スクリプトエンジンにユーザー定義のJavaオブジェクトを渡す方法を知りたいです。 私はこれと似たような試みた: http://groovy.codehaus.org/JSR+223+Scripting+with+Groovyユーザ定義のJavaオブジェクトを速度scriptengineに渡す

をそれはあなただけのcontext.put("name_of_parameter", yourOBject);のようなオブジェクトパラメータを渡すためにVelocityContextを使用する必要がwork..Kindlyは、この状況を解決するために私を助けて事前

答えて

4

感謝をdoesntの 私の例ではtest.temalate$person.addressは、personオブジェクトの平均呼び出しアドレス取得メソッドです。

例:

以下Person.java パブリッククラスPerson { プライベート文字列名として試してみてください。 プライベート文字列アドレス。

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> 
+0

「ゲッターメソッド」がキーワードです。ありがとうございました! – Sebastian

関連する問題