2012-02-22 3 views
0

im 속도 템플릿을 CQ5와 함께 사용. 내가 설치 한 속도 scriptengine 미리 정의 된 CQ 개체를 식별합니다. 나는 속도 스크립트 엔진에 사용자 정의 자바 객체를 전달하는 법을 알고 싶다. 나는이 비슷한 시도 : http://groovy.codehaus.org/JSR+223+Scripting+with+Groovy사용자 정의 자바 객체를 속도 scriptengine에 전달

을하지만 work..Kindly이 상황을 사전에

감사를 해결하는 데 도움이 나던 그냥 context.put("name_of_parameter", yourOBject); 같은 객체 매개 변수를 전달하는 VelocityContext를 사용할 필요가

답변

4

예 : test.temalate, $person.address 사람 개체의 평균 호출 주소 게터 방법.

예 : 같은

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

"게터 방법"이 핵심어였습니다. 고맙습니다! – Sebastian

관련 문제