這是一個簡單的 XSP 程式示例,涵蓋了網站地圖配置、XSP 語法等核心內容。本示例以 Cocoon 2.0.3 為基礎,請確保其已正確安裝。
c:\tomcat4.1)$TOMCAT_HOME/webapps/cocoon在 $COCOON_HOME 目錄下建立 happy-new-year 目錄。
建立 happy-new-year.xsp,內容如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsp:page language="Java" xmlns:xsp="https://apache.org/xsp">
<xsp:logic>
String myString = "Happy New Year!";
</xsp:logic>
<page>
<xsp:expr>myString</xsp:expr>
</page>
</xsp:page>
建立 happy-new-year.xsl,內容如下:
<?xml version='1.0' encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="https://www.w3.org/1999/XSL/Transform" version='1.0'>
<xsl:template match="/">
<html>
<head>
<title>Happy New Year</title>
</head>
<body>
<img src="https://www.QQread.com/chome_static/newyear.jpg"/>
<br/>
<font color="#0000CC">
<b><xsl:value-of select="."/></b>
</font>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
$COCOON_HOME 目錄下的 sitemap.xmapsitemap.xmap,在最底部的 </map:pipeline> 標籤上面加入:<map:match pattern="samples/happy-new-year.xsp">
<map:generate type="serverpages" src="samples/happy-new-year/happy-new-year.xsp"/>
<map:transform src="samples/happy-new-year/happy-new-year.xsl"/>
<map:serialize type="html"/>
</map:match>
打開瀏覽器,輸入 https://localhost:8080/cocoon/samples/happy-new-year.xsp,應該能看到正確結果。也可訪問 線上演示。
若出現問題,請仔細核對上述步驟,特別注意標籤書寫是否正確、格式是否正確。例如 <map:serialize /> 是空標籤,注意有斜槓 "/";而有內容的標籤如 <map:match>...</map:match> 由開始標籤和結束標籤組成。
若仍有問題,可查看 Cocoon 日誌,預設日誌檔位於 $TOMCAT_HOME/webapps/cocoon/WEB-INF/logs/ 下。