天天看點

SiteMesh結合Freemarker或velocity使用

Freemarker 入門示例 http://cuisuqiang.iteye.com/blog/2031768

FreeMarker 整合Struts2示例 http://cuisuqiang.iteye.com/blog/2031860

SiteMesh入門示例 http://cuisuqiang.iteye.com/blog/2066166 

那麼如何将Freemarker與SiteMesh結合起來使用,這在官方示例中已經有了相關例子。

檢視官方示例中decorators.xml檔案,裡面有一段是關于Freemarker使用的

<decorator name="freemarker" page="freemarker.ftl">
	<pattern>/freemarker.html</pattern>
</decorator>      

也就是說請求freemarker.html時會攔截并處理,并發freemarker.ftl作為預設頁面處理

看一下freemarker.ftl的内容,因為在JSP中是使用标簽來擷取Head或者Body的,是以在這個頁面主要是看看如何擷取這些元素。

<html>
	<head>
		<title>Freemarker Decorator - ${title}</title>
		<link href="${base}/decorators/main.css" target="_blank" rel="external nofollow"  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>
           

可以看到,直接通過$來擷取幾個元素,${title}、 ${head}、${base}、${title}、${body}

velocity的結合是一樣的,看看關于他的配置

<decorator name="velocity" page="velocity.vm">
	<pattern>/velocity.html</pattern>
</decorator>
           

看看velocity.vm頁面内容

<html>
	<head>
		<title>Velocity Decorator - $title</title>
		<link href="$base/decorators/main.css" target="_blank" rel="external nofollow"  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>
           

不多解釋了。

請您到ITEYE網站看 java小強 原創,謝謝!

http://cuisuqiang.iteye.com/!

自建部落格位址:http://www.javacui.com/ ,内容與ITEYE同步!