예 : 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>
"게터 방법"이 핵심어였습니다. 고맙습니다! – Sebastian