달력

4

« 2024/4 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
2008. 1. 31. 15:23

Velocity 따라하기 - 1번 예제 - 그거/Tech2008. 1. 31. 15:23


1. velocity-1.5.zip 파일을 다운로드 받아서 압축을 해제한다.
2. velocity-1.5.jar, commons-collections-3.1.jar, commons-lang-2.1.jar 파일을 classpath에 등록한다.
3. example.vm 파일을 생성하여 template을 작성한다.

   #set( ${this} = "Velocity")

   ${this} is great!
  
   #foreach( ${name} in ${list} )
       ${name} is great!
   #end
  
   #set( ${condition} = true)
  
   #if (${condition})
       The condition is true!
   #else
       The condition is false!
   #end


4. Example.java 파일을 생성하여 위에서 작성한 template 파일을 읽어서 처리한다.

   // Velocity 의 환경 설정
   // velocity.properties 파일에는 runtime.log = velocity_example.log 이런 내용이 들어간다.
   Velocity.init("velocity.properties");
  
   // VelocityContext를 만들어서 template 파일에서 사용할 변수를 설정한다.
   VelocityContext context = new VelocityContext();
   // 아래의 "list" 는 template 파일에서 ${list} 로 사용된다.
   context.put("list", XXX);
  
   // template 파일을 지정하여 Template class를 생성한다.
   Template template = Velocity.getTemplate(templageFile);
  
   // rendering 된 template이 출력될 output을 지정한다.
   BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(System.out) );
   template.merge(context, writer);
  
   // rendering 된 template을 출력한다.
   writer.flush();
   writer.close();


5. Example.java 컴파일 하고 실행한다.

$ javac -classpath .;..\..\velocity-1.5.jar Example.java
$ java -classpath .;..\..\velocity-1.5.jar; \
                          ..\..\lib\commons-collections-3.1.jar; \
                          ..\..\lib\commons-lang-2.1.jar \
                          Example example.vm

2008. 1. 31 오후 2:24:49 org.apache.velocity.runtime.log.JdkLogChute log
정보: FileResourceLoader : adding path '.'


Velocity is great!

    ArrayList element 1 is great!
    ArrayList element 2 is great!
    ArrayList element 3 is great!
    ArrayList element 4 is great!


    The condition is true!







:
Posted by 뽀기