啊啊啊啊啊啊,崩溃!
刚刚写完了整片文章,切换HTML编辑修改了下段落缩进,居然全部文章不见了,而且无法撤销,哭~红薯老大看到了注意下啊!最近不是要对我的空间进行升级么,算我提的一个。
再来一遍吧还好时间有的是。
这两天看红薯的OSChina用到了Velocity框架,好奇心驱使我看了看,起初只知道下了个Velocity的jar包,半天不知道怎么用,后来才发现apache里有个velocity-tools的zip包,解压开里面有几个实例,下面我拣个比较简单的先交代一下。
可以看到simple这个实例很简单,从文件个数看就知道。
我们先来看web.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">
<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>
里面配置了velocity的Servlet,这个应该不用解释
下面是WEB-INF里的velocity配置文件tools.xml:
<?xml version="1.0"?>
<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="ToyTool" restrictTo="index*"/>
</toolbox>
<toolbox scope="session">
<tool key="map" class="java.util.HashMap"/>
</toolbox>
</tools>
data标签配置的明显是些数据,方便在模版页面中调用,下面的toolbox标签是配置的是指定scope存储空间的调用类,restrictTo属性配置了指定页面的调用权限。
在给出一个类似实体类的一个类:
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();
}
}
在tools.xml文件中有配置。
在看两个模版页面
index.jsp
<%@taglib prefix="velocity" uri="http://velocity.apache.org/velocity-view" %>
<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>
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>