Velocity Tools 2.0 을 이용 하여 웹 프로젝트 를 쉽게 구축 합 니 다.
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. 공 사 를 발표 하고 방문 하 는 것 은 간단 하지 않 습 니까?여기 서 소개 하 는 지식 이 간단 한 집적 방식, 그리고 많은 고급 기능 이 있 으 니 관련 문 서 를 참고 할 수 있 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Fortinet FortiWeb Web Application Firewall Policy BypassFrom: Geffrey Velasquez Date: Wed, 2 May 2012 20:33:23 -0500...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.