利用Velocity Tools2.0 轻松构建Web工程

1、下载Velocity Tools2.0,给出下载链接:http://velocity.apache.org/download.cgi

2、新建web工程,将velocityTools2.0的jar包添加进工程的lib中。

3、修改web.xml文件,修改后的文件是这样的:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">  
  3.   
  4. <!--  
  5.  Licensed to the Apache Software Foundation (ASF) under one  
  6.  or more contributor license agreements.  See the NOTICE file  
  7.  distributed with this work for additional information  
  8.  regarding copyright ownership.  The ASF licenses this file  
  9.  to you under the Apache License, Version 2.0 (the  
  10.  "License"); you may not use this file except in compliance  
  11.  with the License.  You may obtain a copy of the License at  
  12.   
  13.    http://www.apache.org/licenses/LICENSE-2.0  
  14.   
  15.  Unless required by applicable law or agreed to in writing,  
  16.  software distributed under the License is distributed on an  
  17.  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  
  18.  KIND, either express or implied.  See the License for the  
  19.  specific language governing permissions and limitations  
  20.  under the License.  
  21. -->  
  22.   
  23. <web-app>  
  24.   <servlet>  
  25.     <servlet-name>velocity</servlet-name>  
  26.     <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class>  
  27.   </servlet>  
  28.   <servlet-mapping>  
  29.     <servlet-name>velocity</servlet-name>  
  30.     <url-pattern>*.vm</url-pattern>  
  31.   </servlet-mapping>  
  32.   <welcome-file-list>  
  33.     <welcome-file>index.vm</welcome-file>  
  34.   </welcome-file-list>  
  35. </web-app>  

 

4、新建一个java包com.stone.velocity。

5、新建一个javaBean ,类似这样的:

Java代码   收藏代码
  1. package com.stone.velocity;  
  2.   
  3. public class ToyTool  
  4. {  
  5.     private String message = "Hello from ToyTool!";  
  6.   
  7.     public String getMessage()  
  8.     {  
  9.         return message;  
  10.     }  
  11.   
  12.     public void setMessage(String m)  
  13.     {  
  14.         message = m;  
  15.     }  
  16.   
  17.     /** To test exception handling in templates. */  
  18.     public boolean whine() {  
  19.         throw new IllegalArgumentException();  
  20.     }  
  21.   
  22. }  

 

6、编写tools.xml。

Xml代码   收藏代码
  1. <?xml version="1.0"?>  
  2.   
  3. <!--  
  4.  Licensed to the Apache Software Foundation (ASF) under one  
  5.  or more contributor license agreements.  See the NOTICE file  
  6.  distributed with this work for additional information  
  7.  regarding copyright ownership.  The ASF licenses this file  
  8.  to you under the Apache License, Version 2.0 (the  
  9.  "License"); you may not use this file except in compliance  
  10.  with the License.  You may obtain a copy of the License at  
  11.   
  12.    http://www.apache.org/licenses/LICENSE-2.0  
  13.   
  14.  Unless required by applicable law or agreed to in writing,  
  15.  software distributed under the License is distributed on an  
  16.  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  
  17.  KIND, either express or implied.  See the License for the  
  18.  specific language governing permissions and limitations  
  19.  under the License.  
  20. -->  
  21.   
  22. <tools>  
  23.     <data type="boolean" key="xhtml" value="true"/>  
  24.     <data type="boolean" key="isSimple" value="true"/>  
  25.     <data type="number" key="version" value="2.0"/>  
  26.     <data key="foo">this is foo</data>  
  27.     <data key="bar">this is bar.</data>  
  28.     <toolbox scope="request">  
  29.         <tool key="toytool" class="com.stone.velocity.ToyTool" restrictTo="index*"/>  
  30.     </toolbox>  
  31.     <toolbox scope="session">  
  32.         <tool key="map" class="java.util.HashMap"/>  
  33.     </toolbox>  
  34. </tools>  

 

7、创建index.vm和index.jsp,注意这两个文件放在WEBROOT下,如果放在别的地方,注意修改web.xml中的相关设置。

8、index.vm

Java代码   收藏代码
  1. <html>  
  2. <body>  
  3.   
  4. I'm a velocity template processed using the VelocityViewServlet.  
  5.   
  6. #if( $XHTML )  
  7.   #set( $br = "<br />" )  
  8. #else  
  9.   #set( $br = "<br>" )  
  10. #end  
  11.   
  12. $br  
  13. $br  
  14.   
  15. Here we use a custom tool: $toytool.message  
  16.   
  17. $br  
  18. $br  
  19.   
  20. Lets count : #foreach($i in [1..5])$i #end  
  21.   
  22. $br  
  23. $br  
  24.   
  25. Let's play with a hashmap:$br  
  26. first add foo: $map.put("foo",$foo)$br  
  27. then add bar: $map.put("bar",$bar)$br  
  28. $br  
  29. and that gives us $map  
  30.   
  31. $br  
  32. $br  
  33.   
  34. Here we get the date from the DateTool:  $date.medium  
  35.   
  36. $br  
  37. $br  
  38.   
  39. #if( $isSimple )  
  40. This is simple#if( $XHTML ) xhtml#end app version ${version}.  
  41. #end  
  42.   
  43. $br  
  44. $br  
  45.   
  46. Click <a href="index.jsp">here</a> to see the VelocityViewTag handle the same VTL markup.  
  47.   
  48. </body>  
  49. </html>  

 

9、index.jsp.

Java代码   收藏代码
  1. <html>  
  2. <body>  
  3.   
  4. I'm a JSP file that uses the VelocityViewTag.  
  5.   
  6. <velocity:view>  
  7. #if( $XHTML )  
  8.   #set( $br = "<br />" )  
  9. #else  
  10.   #set( $br = "<br>" )  
  11. #end  
  12.   
  13. $br  
  14. $br  
  15.   
  16. Here we use a custom tool: $toytool.message  
  17.   
  18. $br  
  19. $br  
  20.   
  21. Lets count : #foreach($i in [1..5])$i #end  
  22.   
  23. $br  
  24. $br  
  25.   
  26. Let's play with a hashmap:$br  
  27. first add foo: $map.put("foo",$foo)$br  
  28. then add bar: $map.put("bar",$bar)$br  
  29. $br  
  30. and that gives us $map  
  31.   
  32. $br  
  33. $br  
  34.   
  35. Here we get the date from the DateTool:  $date.medium  
  36.   
  37. $br  
  38. $br  
  39.   
  40. #if( $isSimple )  
  41. This is simple#if( $XHTML ) xhtml#end app version ${version}.  
  42. #end  
  43.   
  44. $br  
  45. $br  
  46.   
  47. Click <a href="index.vm">here</a> to see this VTL markup as a normal template.  
  48.   
  49. </velocity:view>  
  50. </body>  
  51. </html>  

 

10、发布工程并访问,是不是很简单!这里介绍的知识简单的集成方式,还有很多的高级功能,可参考相关文档!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值