Velocity Tools 2.0 을 이용 하여 웹 프로젝트 를 쉽게 구축 합 니 다.

1. Velocity Tools 2.0 을 다운로드 하여 다운로드 링크 를 드 립 니 다.http://velocity.apache.org/download.cgi
2. 새 웹 프로젝트 는 velocity Tools 2.0 의 jar 패 키 지 를 프로젝트 의 lib 에 추가 합 니 다.
3. 웹. xml 파일 을 수정 합 니 다. 수 정 된 파일 은 다음 과 같 습 니 다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->

<web-app>
  <servlet>
    <servlet-name>velocity</servlet-name>
    <servlet-class>org.apache.velocity.tools.view.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>

4. 자바 패키지 com. stone. velocity 를 새로 만 듭 니 다.
5. 자바 빈 을 새로 만 듭 니 다.
package com.stone.velocity;

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();
    }

}

6. tools. xml 를 작성 합 니 다.
<?xml version="1.0"?>

<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->

<tools>
    <data type="boolean" key="xhtml" value="true"/>
    <data type="boolean" key="isSimple" value="true"/>
    <data type="number" key="version" value="2.0"/>
    <data key="foo">this is foo</data>
    <data key="bar">this is bar.</data>
    <toolbox scope="request">
        <tool key="toytool" class="com.stone.velocity.ToyTool" restrictTo="index*"/>
    </toolbox>
    <toolbox scope="session">
        <tool key="map" class="java.util.HashMap"/>
    </toolbox>
</tools>

7. index. vm 과 index. jsp 를 만 듭 니 다. 이 두 파일 을 WEBROOT 아래 에 두 고 다른 곳 에 두 면 웹. xml 의 관련 설정 을 수정 하 는 것 에 주의 하 십시오.
8、index.vm
<html>
<body>

I'm a velocity template processed using the VelocityViewServlet.

#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

$br
$br

Click <a href="index.jsp">here</a> to see the VelocityViewTag handle the same VTL markup.

</body>
</html>

9、index.jsp.
<html>
<body>

I'm a JSP file that uses the VelocityViewTag.

<velocity:view>
#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

$br
$br

Click <a href="index.vm">here</a> to see this VTL markup as a normal template.

</velocity:view>
</body>
</html>

10. 공 사 를 발표 하고 방문 하 는 것 은 간단 하지 않 습 니까?여기 서 소개 하 는 지식 이 간단 한 집적 방식, 그리고 많은 고급 기능 이 있 으 니 관련 문 서 를 참고 할 수 있 습 니 다!

좋은 웹페이지 즐겨찾기