velocity 응용

앞에서 http://freesea.iteye.com/admin/blogs/652707 초학 velocity 에서 velocity 를 간단하게 알 아 보 았 을 뿐 입 니 다. velocity 의 실제 응용 에 대해 더 배 워 야 합 니 다. 오늘 은 시간 이 좀 있 습 니 다. 테스트 식 은 간단 한 응용 을 했 습 니 다. 깊이 이해 하지 못 해서 쉽게 잊 혀 집 니 다. 여기 서 잊 어 버 리 겠 습 니 다.
 
 
오늘 은 주로 velocity - tools - view 패 키 지 를 사용 하여 보 기 를 표시 하고 제어 합 니 다. 이 패 키 지 는 velocity 의 사용 을 간소화 하 는 데 큰 역할 을 합 니 다.사용 하기 전에 당연히 홈 페이지 에 올 라 가 보 세 요. 다운로드 주소: http://velocity.apache.org/download.cgi#tools
 
압축 을 풀 고 나 서 안에 examples 디 렉 터 리 가 있 는 것 을 발 견 했 습 니 다. simple. war 를 tomcat 의 webapps 디 렉 터 리 에 버 리 고 진행 하 는 것 은 간단 해 보이 지만 많은 의문 이 있 습 니 다. 더 알 아 보기 위해 자신 도 한 번 해 보 았 습 니 다. 정말 많은 이익 을 얻 었 습 니 다.
 
웹 xml
     simple 프로젝트 에서 웹. xml 설정 을 보 았 습 니 다.
 
<web-app>
  <servlet>
    <servlet-name>velocity</servlet-name>
    <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>velocity</servlet-name>
    <url-pattern>*.vm</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.vm</welcome-file>
  </welcome-file-list>
</web-app>

 
 정상 적 인 프로젝트 와 별 차이 가 없 는 것 같 습 니 다. 주로 표현 층 을 vm 의 템 플 릿 파일 로 바 꾸 었 을 뿐 입 니 다.
 
2. ToyTool. java
 
public class ToyTool
{
    private String message = "Hello from ToyTool!";

	public String getMessage()
	{
        return message;
	}

    public void setMessage(String m)
    {
        message = m;
    }

    /** To test exception handling in templates. */
    public boolean whine() {
        throw new IllegalArgumentException();
    }

}

 
 
index. vm
I'm a velocity template.
#if( $XHTML )
  #set( $br = "<br />" )
#else
  #set( $br = "<br>" )
#end

$br
$br

Here we use a custom tool: $toytool.message

$br
$br

Lets count : #foreach($i in [1..5])$i #end

$br
$br

Let's play with a hashmap:$br
first add foo: $map.put("foo",$foo)$br
then add bar: $map.put("bar",$bar)$br
$br
and that gives us $map

$br
$br

Here we get the date from the DateTool:  $date.medium

$br
$br

#if( $isSimple )
This is simple#if( $XHTML ) xhtml#end app version ${version}.
#end

 4. toolbox. xml
 
<toolbox>
  <xhtml>true</xhtml>
  <tool>
     <key>toytool</key>
     <scope>request</scope>
     <request-path>index.vm</request-path>
     <class>ToyTool</class>
  </tool>
  <data type="number">
    <key>version</key>
    <value>1.1</value>
  </data>
  <data type="boolean">
    <key>isSimple</key>
    <value>true</value>
  </data>
  <data type="string">
    <key>foo</key>
    <value>this is foo.</value>
  </data>
  <data type="string">
    <key>bar</key>
    <value>this is bar.</value>
  </data>
  <tool>
    <key>map</key>
    <scope>session</scope>
    <class>java.util.HashMap</class>
  </tool>
  <tool>
    <key>date</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.DateTool</class>
  </tool>
</toolbox>
 
 
이 몇 개의 서 류 를 보 았 는데 초학 이기 때문에 많은 의문 이 있 습 니 다.
1. 왜 velocity. properties 파일 을 사용 하지 않 으 면 정확하게 사용 할 수 있 습 니까? (velocity 초학 http://freesea.iteye.com/admin/blogs/652707 시 필요 합 니 다)
 
2. index. vm 에 $toytool. message 가 있 습 니 다. Toytool 류 의 속성 을 직접 꺼 낼 수 있 지만 설정 에 작업 정보 가 없습니다. 이 종 류 를 어디서 사용 하 는 지 알려 주세요.
 
3. toolbox. xml 에서 정 의 된 tool 과 date 는 무슨 뜻 일 까요? 이 물건 들 을 템 플 릿 에 출력 할 수 있 는 방법 이 무엇 인지 모 르 겠 습 니 다.
 
이 문제 들 을 가지 고 줄곧 똑똑히 알 고 싶 었 다.
 
우선 웹. xml 의 입 구 를 열 면 일반적인 상황 은 index. vm 페이지 로 들 어 갑 니 다.
 
 
  <welcome-file-list>
    <welcome-file>index.vm</welcome-file>
  </welcome-file-list>
 
 
모든 vm 파일 은 Velocity ViewServlet 클래스 를 사용 합 니 다.
 
<servlet>
    <servlet-name>velocity</servlet-name>
    <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>velocity</servlet-name>
    <url-pattern>*.vm</url-pattern>
  </servlet-mapping>
 
 
Velocity ViewServlet 이 무엇 을 실 행 했 는 지 알 아 보기 위해 velocity - tools - 1.4 - src. zip 에서 내 려 와 연 구 를 한 결과 이런 유형 이 우리 에 게 기본 적 으로 실현 되 었 음 을 발견 했다.
많은 일, 예 를 들 면:
 
velocity. properties 를 설정 하지 않 은 경우 기본적으로 사용 합 니 다.
 
/**
     * Default Runtime properties.
     */
    public static final String DEFAULT_TOOLS_PROPERTIES =
        "/org/apache/velocity/tools/view/servlet/velocity.properties";

 
 WEB - INF / velocity. properties 설정 이 필요 없 었 습 니 다. 기본적으로 velocity - tools 패키지 의 velocity 속성 파일 을 사 용 했 습 니 다.
 
# default to servletlogger, which logs to the servlet engines log
runtime.log.logsystem.class = org.apache.velocity.tools.view.servlet.ServletLogger

# by default, load resources with webapp resource loader
resource.loader = webapp
webapp.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader
 
 
 
기본 설정 을 사용 할 수 있 지만 많은 문제 가 발생 했 습 니 다. 예 를 들 어 vm 파일 에서 중국 어 를 사용 할 때 출력 에 오류 가 발생 했 습 니 다. org. apache. velocity. tools. view. servlet. Velocity ViewServle 류 가 기본적으로 사용 하 는 출력 인 코딩 은 ISO - 8859 - 1 입 니 다.
 
    /** The HTTP content type context key. */
    public static final String CONTENT_TYPE = "default.contentType";

    /** The default content type for the response */
    public static final String DEFAULT_CONTENT_TYPE = "text/html";

    /** Default encoding for the output stream */
    public static final String DEFAULT_OUTPUT_ENCODING = "ISO-8859-1";

 
 그래서 자신 이 정의 한 velocity. properties 파일 을 사용 합 니 다. 그러면 기본 가방 에 있 는 파일 을 사용 하지 않 습 니 다. 저 희 는 출력 인 코딩 등 정 보 를 스스로 정의 할 수 있 습 니 다. 예 를 들 어:
 
runtime.log = velocity_example.log
#Velocity.properties     
#        WEB-INF/classes    Velocity     ,          
#resource.loader=class 
#class.resource.loader.class=org.apache.Velocity.runtime.resource.loader.ClasspathResourceLoader 
#                  ,       
resource.loader=file 
file.resource.loader.path=D:\Workspaces\MyEclipse 8.5\velocity\WebRoot
#       velocity      
file.resource.loader.cache=false 
#             ,       
input.encoding=utf-8 
#             ,       
output.encoding=utf-8 

 
드디어 제 첫 번 째 의문 을 해 결 했 습 니 다. Velocity ViewServlet 은 이것 을 처리 하 는 것 외 에 toolbox. xml 를 처리 할 수 있 기 때문에 vm 에서 $toytool. message 를 직접 사용 할 수 있 는 이 유 는 toolbox. xml 에서 정의 한 도 구 를 문맥 환경 에 연결 시 켰 기 때문에 vm 에서 인용 할 수 있 습 니 다. 구체 적 으로 코드 세그먼트 를 표시 하지 않 습 니 다. 이것 을 제외 하고...tools 패 키 지 는 유용 한 도구 류 도 많이 정 했 습 니 다. 필요 할 때 toolbox. xml 에서 정의 하고 vm 에서 참조 할 수 있 습 니 다.
 

좋은 웹페이지 즐겨찾기