2008年4月16日 20:45:55 Edited by DingDangXiaoMa
sitemesh 与velocity ,freemarker 结合。(参考资料 sitemesh-example.war)
准备:commmons-collections.jar, freemarker.jar sitemesh-2.3.jar velocity-dep-1.3.1.jar velocity-tools-view-1.1.jar
(1)sitemesh 与velocity
web.xml
<servlet>
<servlet-name>sitemesh-velocity</servlet-name>
<servlet-class>
com.opensymphony.module.sitemesh.velocity.VelocityDecoratorServlet
</servlet-class>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sitemesh-velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
decorators/velocity.vm
<html>
<head>
<title>Velocity Decorator - $title</title>
<link href="$base/decorators/main.css" rel="stylesheet" type="text/css">
$head
</head>
<body>
<div id="pageTitle">$title</div>
<hr/>
$body
<div id="footer">
<b>Disclaimer:</b> This site is an example site to demonstrate SiteMesh. It serves no other purpose.
</div>
</body>
</html>
与一次的代码是差不太多,只不过用$标记来代替以前的<decorator:title /> <decorator:body />
编写 WebRoot/velocity.html
<html>
<head>
<title>Velocity Test</title>
</head>
<body>
<p>This page is decorated by the VelocityDecoratorServlet.</p>
<p>Go <a href="index.html">back</a>.</p>
</body>
</html>
把velocity.html映射到velocity.vm上。
WEB-INF/decorators.xml添加如下映射.
<decorator name="velocity" page="velocity.vm">
<pattern>/velocity.html</pattern>
</decorator>
再运行程序:http://localhost/sitemesh/velocity.html就可看到映射后的样式了, sitemesh与velocity结合。
(2) sitemesh 与freemarker
与上例同步。
web.xml 加入如下servlet 映射:
<servlet>
<servlet-name>sitemesh-freemarker</servlet-name>
<servlet-class>
com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet
</servlet-class>
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
<init-param>
<param-name>default_encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sitemesh-freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
添加:decorators/freemarker.ftl
<html>
<head>
<title>Freemarker Decorator - ${title}</title>
<link href="${base}/decorators/main.css" rel="stylesheet" type="text/css">
${head}
</head>
<body>
<div id="pageTitle">${title}</div>
<hr/>
${body}
<div id="footer">
<b>Disclaimer:</b> This site is an example site to demonstrate SiteMesh. It serves no other purpose.
</div>
</body>
</html>
添加:webRoot/freemarker.html
<html>
<head>
<title>Freemarker Test</title>
</head>
<body>
<p>This page is decorated by the FreemarkerDecoratorServlet.</p>
<p>Go <a href="index.html">back</a>.</p>
</body>
</html>
添加两者之间的映射:WEB-INF/decorators.xml
<decorator name="freemarker" page="freemarker.ftl">
<pattern>/freemarker.html</pattern>
</decorator>
运行http://localhost/sitemesh/freemarker.html .即可看到结果。
今天就到这里,先这样吧。