Original jc2cn
Apache cocoon and xsp
Bigmouse@peoplemail.com.cn)
==================================
I. Introduction
Apache cocoon is a pure Java Web content publishing framework that allows content (pure data), logic, and representation to be placed in different XML files, respectively, then, XSL is used to combine and display them. The advantage of this structure is that the performance has nothing to do with the content. For example, the content of a book stored in an XML file can be determined according to different expressions, convert XSL to HTML, PDF, or even WML (of course, WAP is not suitable for reading books now... (AH) and other forms, and because of XML performance independence, it is used as a standard transmission and exchange data format in B2B (Enterprise-enterprise) transmission interaction (such as soap) in business activities and distributed programs is widely used.
Xsp is the abbreviation of extensible server pages. It implements business logic by adding a Java program to the XML file. Because the data in the XML file is represented by XSL, therefore, xsp separates content from display. Think about how ASP, PHP, and JSP are familiar to you? They work by inserting the logic code into the HTML page, that is, the content is not separated from the display. This is easy to cause trouble. If you have used these technologies to develop a website, you will be able to understand it. You must first wait for the artist to prepare the HTML page for you, then you embed your code (if you have been doing it for a long time, simply Ctrl + C and CTRL + V ). In the future, the artist wants to change something, but he needs to grab it first and ask, "Dude, is it okay if I move this part ?!". What's worse is that the website needs to be revised?
In the construction of a website using cocoon, the staff can be divided into three types: XML file writers, xsp writers, and XSL writers. The creator of an XML file mainly writes the XML file format, DTD or schema, which is equivalent to editing the content of a common website. The xsp writer is responsible for adding Java logic code to the XML file to dynamically control the content, which is equivalent to the ASP, PHP, and JSP programming writers of websites. The XSL creator is responsible for compiling the XSL file that shows the page, that is, the common site artist, but this is higher than the general artist, because XSL is composed of templates, it is an empty shelf that calls XML-related elements one by one without content. These templates also have fixed syntaxes. You can see the details later.
2. Install cocoon
Although the cocoon web program can be debugged in the jbuilder6 development environment, you may not use jbuilder6. So I will introduce the installation and configuration process in Windows2000 Professional (take the directory on my machine as an example ):
1. JDK
I am using JBuilder's built-in jdk1.3.1. The installation directory is C:/jbuilder6/jdk1.3.1 /. If you do not have a JDK installer yet, please download one from the http://java.sun.com.
Install it directly.
2. Apache HTTP Server
I use phptriad for Windows's Apache 1.3.12 and the installation directory is C:/Apache /. If you do not have an Apache installer, download one from the http://www.apache.org.
Install C:/Apache/CONF/httpd directly. the port entry in the conf file (about 211st rows). If you have other HTTP servers such as IIS, modify this port value and do not conflict with IIS. Set the port value to 8000.
3. Tomcat
I am using the built-in Tomcat 3.2.3 JBuilder, the installation directory is C:/jbuilder6/jakarta-tomcat-3.2.3 /. If you do not have a tomcat installer yet, please download one from the http://jakarta.apache.org.
Download apachemodulejserv. dll (or the website above) and copy the file to the C:/Apache/modules/directory.
C:/jbuilder6/jakarta-tomcat-3.2.3/CONF/tomcat. in the conf file, set "loadmodule jserv_module modules/apachemodulejserv. DLL "before" # "remove (about 8th rows), and add" # "(about 13th rows) before" loadmodule jserv_module libexec/mod_jserv.so ).
Modify the C:/Apache/CONF/httpd. conf file and add "include C:/jbuilder6/jakarta-tomcat-3.2.3/CONF/tomcat. conf" at the end of the file"
Right-click "my computer"-> "advanced"-> "environment variables" and choose "New" from "system variables". The following two environment variables are displayed: "variable name" is "tomcat_home", "variable value" is "C:/jbuilder6/jakarta-tomcat-3.2.3"; "variable name" is "java_home", "variable value" is "C: /jbuilder6/jdk1.3.1 ".
4. Cocoon
I use the built-in cocoon1.8 in JBuilder and the installation directory is C:/jbuilder6/cocoon /. If you do not have a cocoon installer, go to the http://xml.apache.org to download one.
Copy all *. Jar files under the C:/jbuilder6/cocoon/lib/directory to the C:/jbuilder6/jakarta-tomcat-3.2.3/lib/directory.
Copy the cocoon. jar file under the C:/jbuilder6/cocoon/bin/directory to the C:/jbuilder6/jakarta-tomcat-3.2.3/lib/directory.
Note: The tomcat version I use can automatically check C:/jbuilder6/jakarta-tomcat-3.2.3/lib/directory *. JAR file, and then add it to $ classpath. If you find that your Tomcat does not support the automatic check function, you need to manually send it to Tomcat. add those copied in bat *. JAR file.
Create a cocoon subdirectory under C:/jbuilder6/jakarta-tomcat-3.2.3/webapps/and then create a jakarta-tomcat-3.2.3 subdirectory under the C:/jbuilder6/WEB-INF/webapps/cocoon/directory.
Copy the C:/jbuilder6/cocoon/CONF/cocoon. properties file to C:/jbuilder6/jakarta-tomcat-3.2.3/webapps/cocoon/WEB-INF.
Copy the C:/jbuilder6/cocoon/src/WEB-INF/Web. xml file to C:/jbuilder6/jakarta-tomcat-3.2.3/webapps/cococoon/WEB-INF.
Modify C:/jbuilder6/jakarta-tomcat-3.2.3/webapps/cocoon/WEB-INF/Web. xml file, change CONF/cococoon. properties to WEB-INF/cocoon. Properties
Modify the C:/Apache/CONF/httpd. conf file and add:
Alias/cocoon C:/jbuilder6/jakarta-tomcat-3.2.3/webapps/cocoon
<Directory "C:/jbuilder6/jakarta-tomcat-3.2.3/webapps/cocoon">
Options indexes followsymlinks
</Directory>
ApJServMount/cocoon
<Location/cocoon/WEB-INF/>
AllowOverride none
Deny from all
</Location>
Modify the C:/jbuilder6/jakarta-tomcat-3.2.3/CONF/server. xml file:
<Context Path = "/cocoon" docbase = "webapps/cocoon" DEBUG = "0" reloadable = "true">
</Context>
Restart Tomcat and Apache to make the settings take effect.
Access: http: // localhost: 8080/cocoon. XML in IE
You can see some cocoon parameters.
Iii. Use Java to write logic labels of xsp
Let's take a look at an xsp example. This is a simple counter:
---------- Counter. xml ----------
<? XML version = "1.0" encoding = "gb2312"?>
<? Cocoon-process type = "xsp"?>
<? Cocoon-process type = "XSLT"?>
<? XML-stylesheet href = "counter. XSL" type = "text/XSL"?>
<Xsp: Page Language = "Java" xmlns: xsp = "http://www.apache.org/1999/XSP/Core">
<Xsp: logic>
Private Static int ncounter = 0;
Private synchronized int getcounter ()
{
Return ncounter ++;
}
</Xsp: logic>
<Counter>
<P>
Access: <xsp: expr> getcounter () </xsp: expr> (times)
</P>
</Counter>
</Xsp: Page>
---------- Counter. XSL ----------
<? XML version = "1.0"?>
<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: template match = "counter">
<XSL: Copy>
<XSL: Apply-templates/>
</XSL: Copy>
</XSL: Template>
</XSL: stylesheet>
The following describes the meaning of each statement in the counter. xml file:
<? XML version = "1.0" encoding = "gb2312"?>
I don't need to say that, do I? XML files must contain a part. xsp is an XML file and cannot be missing. The gb2312 character set is used to display Chinese characters in the code.
<? Cocoon-process type = "xsp"?>
This Processing Instruction instructs cocoon to use xsp to process the XML file.
<? Cocoon-process type = "XSLT"?>
<? XML-stylesheet href = "counter. XSL" type = "text/XSL"?>
This Processing Instruction instructs cocoon to use the counter. XSL file to convert and display the XML file. XSLT is not required for the XML file.
<Xsp: Page Language = "Java" xmlns: xsp = "http://www.apache.org/1999/XSP/Core">
Each xsp file must contain xsp: page, which is the root element of xsp. You must also specify the namespace. The language attribute is not required. It specifies the language used for processing the logic. The default value is the Java language. Note: xsp: Page must contain the root element of an XML file. The above Code contains the root element counter.
<Xsp: logic> </xsp: logic>
This tag contains the logic part of xsp. Xsp introduces many common Java classes by default (For details, refer to the instructions below), so you can directly write Java code, instead of importing the required packages and classes. However, if you want to introduce a class, you can embed xsp: include in xsp: structure. For example:
<Xsp: Structure>
<Xsp: Include> JAVA. util. Vector </xsp: Include>
</Xsp: Structure>
This is equivalent to import java. util. Vector
<Xsp: expr> getcounter () </xsp: expr>
This label is used to evaluate the expression value and then display it. This is automatically used for type conversion. It stores this value as the text value of a node in the output XML file. In addition, sometimes xsp: expr needs to be embedded in xsp: logic to make the content output by xsp: expr as a node. At this time, you cannot directly put xsp: expr into xsp: in logic, xsp: expr is put into xsp: context, and then embedded into xsp: logic, as shown below:
<Xsp: logic>
<TD>
For (INT I = 0; I <parametervalues. length; I ++)
{
<Xsp: content>
<Xsp: expr> parametervalues [I] </xsp: expr>
</Xsp: content>
<Br/>
}
</TD>
</Xsp: logic>
Note: "<" must be converted to <to avoid XML parsing errors. This is usually difficult to write programs, Because you often use ">", "<" and other symbols that need to be converted. You can put the content to be converted to <! [CDATA]> In the tag, XML will not process the content, so the above Code can be written as follows:
<Xsp: logic>
<TD>
<! [CDATA [For (INT I = 0; I <parametervalues. length; I ++)]>
{
<Xsp: content>
<Xsp: expr> parametervalues [I] </xsp: expr>
</Xsp: content>
<Br/>
}
</TD>
</Xsp: logic>
Note that a non-empty element cannot be truncated by xsp: logic. For example, if the above Code <TD> is written outside of xsp: logic, it is incorrect:
<! -- The TD element is not empty and is truncated by xsp: logic -->
<TD>
<Xsp: logic>
<! [CDATA [For (INT I = 0; I <parametervalues. length; I ++)]>
{
<Xsp: content>
<Xsp: expr> parametervalues [I] </xsp: expr>
</Xsp: content>
<Br/>
}
</Xsp: logic>
</TD>
This is just a very simple application. Cocoon comes with many examples. We suggest you take a look. The above uses Java code to implement dynamic content in logic labels. Maybe you are not familiar with the Java language, which does not mean you cannot use xsp. Because xsp also provides the tag library function, you can use the tag library to create xsp pages even if you do not understand programming languages. About the use of the tag Library and other xsp problems!
--------- Appendix -------------
The following Java classes are automatically introduced:
Java. Io .*;
Java. util .*;
Org. W3C. Dom .*;
Org. xml. Sax .*;
Javax. servlet .*;
Javax. servlet. http .*;
Org. Apache. Cocoon. parser .*;
Org. Apache. Cocoon. Producer .*;
Org. Apache. Cocoon. Framework .*;
Org. Apache. Cocoon. processor. xsp .*;