Jakarta Tomcat5.0+Apache Cocoon2.1.3开发初步指南二--开发篇(1)

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Jakarta Tomcat5.0+Apache Cocoon2.1.3开发初步指南二--开发篇 (1)

Author:Junnef Jin

Date: 2003/11/25

E_mail:junnef21@sohu.com

开发一个简单的程序:

下面我们来开发一个简单的程序,这个例子参考了Cocoon帮助文档中的一个例子,如果你已经按照Jakarta Tomcat5.0+Apache Cocoon2.1.3开发初步指南一--安装篇做了,那么通过下面的链接可以看到英文原例:

http://localhost:8080/cocoon/docs/howto/howto-html-pdf-publishing.html

下面一步一步来创建这个程序。

9、创建程序目录

在部署到Tomcat中的那个cocoon文件夹下,新建一个目录,命名为html-pdf。

10、创建XML文档

在html-pdf文件夹下创建如下两个文件:

pageOne.xml

<?xml version="1.0" encoding="iso-8859-1"?>

<page>

<title>This is the pageOne.xml example</title>

<s1 title="Section one">

<p>This is the text of section one</p>

</s1>

</page>

pageTwo.xml

<?xml version="1.0" encoding="iso-8859-1"?>

<page>

<title>This is the pageTwo.xml example</title>

<s1 title="Yes, it works">

<p>Now you're hopefully seeing pageTwo in HTML or PDF</p>

</s1>

</page>

11、为HTML文件创建XSLT转换器

在html-pdf文件夹下创建

doc2html.xsl

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<!-- generate HTML skeleton on root element -->

<xsl:template match="/">

<html>

<head>

<title><xsl:apply-templates select="page/title"/></title>

</head>

<body>

<xsl:apply-templates/>

</body>

</html>

</xsl:template>

<!-- story is used later by the Meerkat example -->

<xsl:template match="p|story">

<p><xsl:apply-templates/></p>

</xsl:template>

<!-- convert sections to HTML headings -->

<xsl:template match="s1">

<h1><xsl:apply-templates select="@title"/></h1>

<xsl:apply-templates/>

</xsl:template>

</xsl:stylesheet>

12、创建站点地图文件

这个文件是cocoon2中的两个重要配置文件之一,且必须命名为sitemap.xmap,放到该模块的根目录下,这里,放到html-pdf下,文件内容如下:

<?xml version="1.0" encoding="iso-8859-1"?>

<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">

<!-- define the Cocoon processing pipelines -->

<map:pipelines>

<map:pipeline>

<!-- respond to *.html requests with

our docs processed by doc2html.xsl -->

<map:match pattern="*.html">

<map:generate src="{1}.xml"/>

<map:transform src="doc2html.xsl"/>

<map:serialize type="html"/>

</map:match>

<!-- later, respond to *.pdf requests with

our docs processed by doc2pdf.xsl -->

<map:match pattern="*.pdf">

<map:generate src="{1}.xml"/>

<map:transform src="doc2pdf.xsl"/>

<map:serialize type="fo2pdf"/>

</map:match>

</map:pipeline>

</map:pipelines>

</map:sitemap>

13、现在可以测试我们程序的HTML版面了

在IE地址栏中输入下面的链接,将会看到如图所示的页面

http://localhost:8080/cocoon/html-pdf/pageOne.html

http://localhost:8080/cocoon/html-pdf/pageTwo.html

高兴吧,刚开始而已,下面我们会有更多的收获。

待续。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航