source:
http://www.linuxforum.net/forum/printthread.php?cat=387301&type=post.com=java/prin
If you have a understanding of Apache Cocoon or to be involved, you may be better to use Java in CoCoon.
Realization of specific logic has doubtful. This article will show you how to use XSP (EXTENSIBLE SERVER PAGE)
And Action. There is also an example and design principles.
You may have heard some sound from Apache about Cocoon. Now, after three years of development,
Cocoon has gradually converted from a simple implementation XSL (Extensible Stylesheet Language)
Servlet grows into a full web application framework.
CoCoon is developed with Java, which is generally used as a servlet container like Tomcat as a servlet.
In this article, we will introduce two ways to use Java to implement commercial logic in Cocoon-based applications.
First, let's take a look at Cocoon.
CoCoon officially defined is an XML publishing engine, we can understand that cocoon is used to generate, convert, and process
And the framework of output data. It can also be understood that Cocoon is a different processing from a variety of data sources to apply different processing, the most
The machine after the data is output in the desired format.
We can also define Cocoon as a data stream machine. That is to say, when you use Cocoon, you define the path of the data.
Trail or flow to generate a page of web applications.
Below is some of COCOON's main basic principles:
1. Cocoon handles all the data as the SAX (Simple API for XML) event, any non-XML data must
Transfer into an XML description.
2, components that generate SAX events are responsible for processing input data
3. The serializer is responsible for processing output data to output data to the client (browser, file, etc.).
4. Developer combination generators, serializer, and other components constitute a pipe. All pipes are in a map called site map
Definition in the file.
5, match the pipe through the URI (Uniform Resource Identifier), but the URI is detached from the physical resource.
Point 5, you need to explain:
For traditional Web Server, the URI is generally mapped to physical resources.
For example, this URI
http://localhost/index.html will map to INDEX.HTML in Apache Server
HTML file.
In Cocoon, URIS and physical resources may be without any absolute relationship. You can help the URI you can
Help users browse your site better. Finally, you can organize your files to make it easy to manage and maintain.
In order to better understand the Cocoon's processing model, you can see a simple pipe.
The following example defines a page called INDEX.html. This pipe is located in the SiteMap.xmap site map:
map: match>
This pipe has three steps:
The first is a generator component FileGenerator reads data from the XML file "content / mainfile.xml".
(FileGenerator actually has already defined in the map in advance, you can reference all in .cocoon through the "Type" property
Pipe components are referenced by their Type properties. )
The conversion is then performed, and the converter Traxtransformer applies the XSL StyleSheet to the introduced data.
Finally, the sequencer HTMLSerializer writes the data to the client's browser.
You may be confused, what is in connection with Java development?
We divide Cocoon's application into three parts:
1, data collection layer DATA Collection (Generation)
2, data processing and conversion layer Data Processing & Transforming
3. Output layer Data Output (Serialization)
Then, Java development is very important in Cocoon's processing conversion layer. Cocoon's conversion and processing layer are cocoon
The core of the application, through this layer of processing, logical applications, logical applications, you can get the desired output.
In Cocoon, you can have the following four ways to implement logic:
1. Use the Transformer component: They convert the incoming data according to the rules you give. Classic example
It is traxtransformer.
2. Select different components to make correct processing through different requests, session, and uri.
3, use an existing or you implement the Action.
4. Use XSP that mixed Java code and content.
This article introduces the last two methods: XSP and Action. The development of XSP and Action is in the servlet context.
Specifically, two components (actually all components) have access to Request, Response, Session, and
Context object. In some ways, a lot of logic you want to achieve will interact with these objects.
XSP
XSP is the innovation of the Cocoon project. You can compare it with JSP because they are mixed logic and content and JSP
Taglib and XSP logicsheet are also very similar.
XSP is located at the starting point of the pipe, in fact it is converted into a generator (Generator) to give the rest of the components in the pipe.
Data.
Let us look at this called Sample1.xsp simple example:
XML Version = "1.0"?>
Date now = new date ();
String msg = "boo!";
xsp: logic>
This is an xsp. You can see how We it Contain Both Logic
(Inside the
Above, We created a date Object whose value is
OH, WE Also Had a Special Message for you:
content>
xsp: Page>
First pay attention to the root mark of this document is
This tag defines the XSP's Language (can be Java or JavaScript) and the Namespace for the logical order for use.
Then we define the
These
Finally, our own content, starting from the user's own follow-up, in the above example is
We can get the variables defined in front with
Remember, an XSP is actually a generator generator. CoCoon converts it to the Java source file and compile it.
(If you want to see the Java source file converted into XSP, go to your servlet container's work path. For example,
If you use Tomcat 4.0.4, the path is below:
$ Catalina_Home / Work / Standalone / localhost / cocoon / cocoon-files / org / apache / cocoon / www.)
XML data generated after XSP execution is passed to the remaining components of the pipe.
Look at this pipeline example:
map: pipeline>
Here, we use a specified generator ServerPagesGenerator to handle our simple XSP. Back to customers
Non-modified XML.
TEAPERARATION The specifically {1} variable reference is used: it instead of the value indicated by the wildcard starting at the beginning of the pipe. That is to say,
If we open Sample1.xsp in our web app in your browser, the value of {1} is Sample1.
Remember, just like most Cocoon components, XSP accesses the Request, Response, Session, and Context objects. These ones
Objects are actually HTTPSERVLETREQUEST, HTTPSERVLETRESPONSE, HTTPSESSION, and
HTTPSERVLETCONTEXT package, Cocoon official version provides a large number of methods for accessing these objects.
XSP is especially useful when reading data from the database.
Database data naturally organizes rows and columns, so database data is easy to convert to XML. However, JDBC
(Java Database Connectivity) There is no connection to XML conversion.
XSP allows us to be easy when reading data, thank you ESQL logic. ESQL logic in addition to hidden
JDBC code also allows rows and columns to be placed in a specific tag. At the same time, ESQL logic can also perform nested query
And execute the update command.
Below, we will give an example of an XSP application: If we want to store some Cocoon's resources (name and URL) to the database.
First, we define the data sheet where the resource is stored, then when the user searches by keyword, we use XSP to find the corresponding
The line is displayed to the user.
Subsequently, we build a form to add new columns.
The definitions and insertions of the table are shown below. The database we used here is mysql, if you are using other
Database, pay attention to do the corresponding changes. In this example, you must have a configuration database connection pool.
The surface structure is as follows:
Use test;
Create Table Resources
Resourceurl Varchar (255) Not Null,
ResourceName VARCHAR (64) Not NULL
);
Insert some resource data:
Insert Into Resources Values
('http://xml.apache.org/cocoon', 'Cocoon Home Page');
Insert Into Resources Values
('http://www.galatea.com/flashguides/cocoon-tips-2.xml', 'Cocoon 2.0 Tips and Tricks';
After the table is built and COCOON is also correct, we can write the following XSP example:
XML Version = "1.0"?>
XMLns: Xsp = "http://apache.org/xsp" XMLns: esql = "http://apache.org/cocoon/sql/v2"> String Keyword = Request.GetParameter ("Value"); xsp: logic>
Select * from resources
WHERE ResourceName Like '%
esql: query>
resource>
esql: row-results>
resources>
esql: results>
esql: Execute-Query>
ESQL: Connection>
content>
xsp: Page>
Note Namespace (Namespace) declared in the
You must declare its namespace. You can find logical orders in Web-INF / COCOON.XCONF under the Cocoon WebApp path. The declaration of the XSP namespace is to indicate that this is an XSP logic.
In fact, all XSPs are at least to implement XSP logic orders. The logical order before XSP is converted into a Java source file
(I actually only XSL files) will be switched to Java code first. Therefore, all of the examples in the above example
The ESQL tag will be converted to the JDBC code we are familiar with. But not all tags can become JDBC code,
Note the
connection pool. The connection pool used in the above program is called "resources", of course you can use the definition you like.
Note that we use the
Sign. This way we can easily write a style sheet to convert XML into other browsers to understand. we do not have
Define any labels for the list of tables, by using
The tag defined by the column name.
Now, let me pay attention to the SQL query statement in the example. As you can see, this SQL is dynamically generated. As a user
After submitting data from gets or posts to this XSP, at the top of XSP, we assume the value of the Request parameter to
The keyword variable and then constitute a SQL statement according to Keyword.
Since this example is simple, let's turn it into a point, join the Email function, you can provide the email address after the user,
Send the query results to the user.
XSP examples are as follows:
XML Version = "1.0"?>
XMLns: Xsp = "http://apache.org/xsp" XMLns: ESQL = "http://apache.org/cocoon/sql/v2" XMLns: sendmail = "http://apache.org/cocoon/sendmail/1.0" XMLns: Xsp-request = "http://apache.org/xsp/request/2.0" > String keyword = String emailddr = String emailbody = ""; xsp: logic>
Select * from resources where resourceename like
'%
EmailBody =
EmailBody = ",
xsp: logic>
resource>
esql: row-results>
resources>
esql: results>
esql: Execute-Query>
ESQL: Connection>
IF (EmailAddr! = null) {
sendmail: send-mail>
}
xsp: logic>
content>
xsp: Page>
Several labels from Sendmail logic let us have the ability to send email. In this example, we will query results
Each row is added to assign the EmailBody variable as the body of the mail. When the user provides an email address via the request parameter,
We can send email. Of course, this requires you set a good SMTP server and from address in advance.
Cocoon knows the label in the Sendmail namespace based on Sendmail logic, because this namespace already
It has declared in the
The XSP-Request logic single provides packages for the Request common method. Although directly accessed the Request object directly in XSP and
There is no functionality that uses XSP-Request logic orders, but theoretically use Logicsheet labels
The Java code is more beautiful.
Before running this example, you must first set the sendmail logic in the COCOON.XCONF file, Cocoon's configuration
The files are all under the web-inf directory of the Web Application. Open the cocoon.xconf file with your familiar editor,
Find
Value = "resource: //org/apache/cocoon/components/language/markup/xsp/java/sendmail.xsl" /> builtin-logicsheet> This definition will http: //apache.org.cocoon/sendmail/1.0 namespace and already included in Cocoon Jar Sendmail.xsl style sheet is united. To use the features of the Sendmail logic, Cocoon must have mail.jar and activation.jar. Two JARs. If you use Server is Tomcat4.x, then they are located in $ Catalina_Home / Common / Lib. Actions The Action function is very powerful, you can put it in any place it in the pipe. Action can be considered a small self-contained machine, It gets some input data, do some processing, and then returns the HashMap object. Unnenrators in Cocoon, Transformers, Serializers Components, Action does not do anything about the actual XML content, mainly in the pipeline Implement some logic. Learning Action includes some understanding of the pipeline parameters, and sometimes the components of the pipe must communicate data. Of course, XML content The SAX event passes; however, what we said is the value required for the function of the pipeline assembly itself. There are two types of pipeline parameters: input and output. The Input parameter is one or more of the back of the component declaration. These two components can provide an accessible Output variable for their back components. These Output parameters are placed in the HashMap object, can be referenced by the KEY name (such as {1}). All pipes have at least a HashMap provided by Matcher from the beginning of the pipe. We use this in the pipeline HashMap object, use {1} to get the value of Key 1 in HashMap. CoCoon itself contains some built-in Action. One of them is to rely on the database to identify the user's action. when we To protect some of the pages in Cocoon, you can store the user ID and password when you are allowed to access users. Curry, then use DatabaseAuthenticationAction to do login confirmation. This DatabaseAuthenticationAction requires us to provide an XML description file to explain which table you want to use and Some columns. Here is an example of this description file: XML Version = "1.0" encoding = "UTF-8"?>