Web page technology review (including FASTM)
Order
In the Java Web Application, the page build part is the most cumbersome part of the headache.
Other layers can be well structured, and only the structure of the page generated part is very disabled.
This article first introduces and compares various page generation techniques, and then specifically describes a PHP template to improve the PHP template to Java's open source project - FASTM.
I hope that FASTM can help the majority of Java Web programmers will be freed from cumbersome page development work.
2. Page Generation Technology Summary
The page referred to herein is a web page that can be displayed in the browser in XML, HTML, WML, and the like.
According to the location of the page processing logic, page generation technology can be divided into two categories:
(1) A class is that the page itself contains logic (IF, For, etc). For example, JSP Taglib, Velocity, XML XSLT (Processing Logic is in the XSL file), TapeStry, etc.
In this type of technology, since the page itself contains logic, the structure and reusability of page components are not very good, and "object", "class", "package", etc. are unable to use object-oriented features.
(2) Another class is that the page itself does not contain logic. For example, Echo, XMLC, DOM XPath, Echo, PHP, JDynamicte, FASTM, etc.
In this type of technology, due to the processing logic in Java Code, how much is the structure and reusability of the Java program, how good the structure and reusability of page components can be.
The following is a separately explained and compare these page generation techniques:
2.1. JSP Taglib
JSP Taglib is Sun's page generation technology specification.
advantage:
The authority, normative, many users, the third party provides a lot of Taglib support.
Moreover, JSP is essentially a servlet. The Java Code part is very powerful and flexible and can achieve the greatest flexibility in Java language.
Taglib can reach a certain level of page elements reuse. Taglib can also help the Java Code "contamination" section of the JSP page to some extent. J
Disadvantages:
The disadvantage of JSP is obvious. Java Code and HTML pages are mixed together, which is the top Java Code Pollution problem that makes the majority of programmers. This is not fully solved using Taglib.
Moreover, programmers and page editors cannot work on the same file. Each page editor has a html page, and the programmer must re-join Java Code and Taglib.
Once Java Code and Taglib are added, the HTML page cannot be displayed correctly in the page editor (Dream Weaver, Front Page). If the program does not run, you can't see the page structure of the JSP page, display style and content.
About efficiency, flexibility, structural, reusability, JSP Taglib are a two difficult option.
A large number of Taglibs in JSP can make JSP's page structure, more in line with XML format, and can reuse some page elements. But the code after Taglib is huge and messy, and the operating efficiency is very low, which seriously affects the response speed. Tablib is very unflexible, and things that can be completed are limited. The reusability of Tablib code itself is limited by the Tagsupport definition, not very good. Taglib's writing is not a pleasant thing. J If you use Java Code in JSP, then the structure of the page will be poor and difficult to manage. All the code is in the same file, which is a big HTML page, which is simply like a nightmare. From a left bracket "{" to find the corresponding right bracket "}" to become a painful thing. The HTML element in the middle of the Java Code cannot be reused. So, in addition to include file or jsp: include, JAVA Code in JSP does not have reusable.
2.2. Velocity
http://jakarta.apache.org/Velocity/
Velocity is a template processing tool. The Velocity Template is composed of some Velocity scripting languages or variable definitions in HTML.
advantage:
Velocity's script language (start) or variable definition (with $ markers) and HTML, WML, XML, etc. are not conflict.
Simple Velocity page (excluding branches and loop logic) can be displayed correctly in the page editor (Dream Weaver, Front Page).
Disadvantages:
Similarly, the page processing logic of the Velocity Template is mixed together with HTML elements. If the logical complex logic of the Velocity page (such as loop and judgment branch), then the Velocity page cannot be displayed correctly in the page editor.
In the event of a big HTML page, find the corresponding "#end" from a "#IF" is also a very painful thing. The HTML element in the middle of the Velocity script language cannot be reused. Therefore, the script code and HTML elements in the Velocity template do not have reusability (except for include file).
2.3. XML XSLT
http://cocoon.apache.org/
The Cocoon project uses XML XSLT methods. The Java program only needs to output XML data, and the Cocoon Framework calls the XSL file to convert XML data into files such as HTML, WML.
advantage:
The programmer has provisions, do not consider the page structure and display mode, just output XML data.
With the same XML data, as long as different XSL files are defined, you can easily have different display-free pages.
In terms of content and display style separation, XML XSLT this method is best.
Disadvantages:
slow. XSLT is slow.
Since there is no HTML file, you can't see the page structure, display style and content. Only after the program runs, after XSLT conversion,
XSL syntax is more difficult to master, because there is no "ready" editing tool, high learning costs, far more than HTML learning costs.
2.4. Tapestry
http://jakarta.apache.org/tapestry/
TapeStry extends the definition of HTML elements. TapeStry uses these extended HTML element properties to represent processing logic (cyclic, branch, etc.), component definitions, and variable definitions.
advantage:
The entire TapeStry page file is HTML element, which can be displayed in the page editor (Dream Weaver, Front Page). However, whether the display is correct, it is another matter, and the complexity of processing logic (cycle, branch, etc.) is associated. It is similar to Velocity. TapeStry's page component reuse is relatively high.
Disadvantages:
complex. Tapestry definitions and usage are complicated. Slow due to complexity.
I don't actually use Tapestry's specific experience, this will not continue to comment. J
2.5. XMLC
http://xmlc.enhydra.org/
XMLC compiles HTML, WML and other documents into a DOM structure of Java classes. The programmer does not need to take a point, just operate this DOM structure, you can dynamically generate a page.
XMLC generates the operation method of the corresponding node based on the ID definition of the HTML element.
advantage:
The page is pure HTML without any logic. Correctly displayed in the page editor (Dream Weaver, Front Page).
Java Code handles DOM structure, code structure, and reusability. The DOM node is HTML element, that is, page components, also have a good reusability, you can place a DOM node in any of any documents at any time.
Because it is static compile, there is no dynamic analysis process, and the speed of the document is very fast.
Disadvantages:
Each time you modify the HTML page, you must re-compile the HTML file into a Java file. Moreover, many HTMLs are not a good XML structure, and it is not possible to compile it to a DOM structure.
The DOM structure cannot be reused in a multi-threaded environment, which means that each page request needs to generate a separate DOM structure, which is relatively large. Moreover, each DOM structure is used, it is difficult to empty and reset it, it is difficult to provide the next request. When the HTML file is relatively large, the overhead of the memory is significantly increased.
XMLC is not flexible enough. For example, we know, many Java Scri
PT
Method is defined in XML annotation. such as:
script>
If Java Scri
PT
There is a need for a dynamically generated portion, and the Java SCRI of the comment section
PT
Not an XML structure, XMLC is difficult to handle this situation.
2.6. DOM XPath
The XMLC is described earlier to compile HTML static to a DOM structure.
We can also use a way to dynamically generate a DOM structure, then locate the DOM node with XPath and operate them.
Nekohtml (http://www.apache.org/~andyc/neko/doc/html/) is an HTML document parsing tool.
NEKOHTML uses apache Xerces (http://xml.apache.org/xerces2-j/index.html) Xerces Native Interface parsing the HTML document, can automatically make up and correct HTML elements that do not conform to XML literacy, and generate HTML DOM Document tree.
Apache XcerSe's XPatHapi, you can easily locate the node of the DOM structure with XPath. See NEKOHTML's use documentation for DOM XPATH.
advantage:
It has the advantage of the above XMLC method.
There is no static compilation process, dynamically generate the DOM structure, and timely reflect changes in the page file. Disadvantages:
The shortcomings of the above XMLC method are generally.
The dynamic generation of the DOM structure takes time. We can use this method to solve this problem, and put the first generated DOM structure as a standard template. We don't operate the standard template. When you request a DOM structure each time, we will copy a new DOM structure from this standard template depth (deep copy) and then use this new DOM structure. This way each HTML file only needs to be parsed once, becoming a standard template. The standard template is also updated each time the HTML file changes (this situation).
XPath's resolution and positioning speed is not very fast, almost every time you have to pass the entire document tree.
2.7. Echo
http://sourceforge.net/projects/echo
The template file is not required in the Echo project. The programmer writes code in Swing, and Echo helps you generate HTML results.
Advantages: Do not process the HTML section. Swing is a virtual component that can be reused.
Disadvantages: Echo shields HTML and automatically generates a lot of JavaSCRI
PT
And HTML code. For web programs, it is not flexible enough to maintain.
2.8. PHP
The design idea of the PHP template is very good, and the PHP template uses XML's Comment Note section to define dynamic structures, with {} to mark the variable portion that needs to be replaced.
advantage:
The page editor (DREAM WEAVER, Front Page does not display the XML Comment Comment section, and {} symbols and XML elements are not conflict. Therefore, the PHP template itself meets the HTML syntax, which can be displayed correctly in the page editor. Programmers and page editors can work on the same page file and what you get it.
Simple, easy to use, flexible (almost as flexible as the most flexible JSP Java Code). The specific use is followed. The main purpose of this article is to introduce the use of PHP templates and its Java Port. J
Disadvantages:
This technology has little shortcomings compared to other page technologies.
A place that needs to be mentioned is also Java SCRI
PT
The method is defined in the case in XML annotation. such as:
script>
Because PHP Template uses XML Comment to define dynamic structures, we can't be in Java SCRI
PT
The structure is defined. But we don't need to define dynamic structures in XML Comment, we can use the {} flag to require dynamic changes, it can achieve the same purpose.
2.9 JDYNAMITE
https://sourceforge.net/projects/jdynamite
From all aspects, I think PHP is the best page generation technology.
One of my colleagues introduced me the definition and use of PHP Template, I have a strong interest and generate the idea of porting PHP Template to Java. I first searched online and found the open source project of jdynamite (java dynamic template (https://sourceforge.net/projects/jdynamite). JDynamite can transplant the PHP template to the Java language.
JdyNamite's template definition and PHP Template is some of the difference, but as simple as. I read the JDynamite example code and immediately like this method. But I found that JdyNamite has a little short. Like the processing of the DOM structure, the Template section of JDynamite and the parameter setting portion are incorporated here. JDYNAMITE's template section cannot be used simultaneously in the multi-thread.
Each JdyNamite needs to read and parse the HTML file, generate a template, after setting the value, resulting. This template after handling can no longer be used directly (perhaps you can empty reuse).
If the HTML file is large, then, it takes a lot of time to read and resolve the HTML file.
So, I decided to realize a high-efficiency reusable PHP Template Java Port.
The project is called FASTM (the meaning of Fast Template.)
2.10. FASTM
http://sourceforge.net/projects/faSTM
http://sourceforge.net/projects/lightweb
FASTM template definition uses JDYNAMITE template definition, and I use JDynamite's example template file as a template file as a FASTM test example. FASTM can be said to be a Multiple Thread Port of JdyNamite.
In FASTM, I separate HTML's resolution result document (Template DOM) and ValueSet Dom.
Each HTML file only needs to be parsed once, generating a read-only template. Since read-only, of course, thread is safe, any number of threads can use this template simultaneously.
This template will only be combined with different variable settings, and different results can be generated.
The parsing process of the template is used in a way to read processing.
FASTM speed is faster than JSP Taglib. FASTM speed may be slower than pure JSP, or it may be faster. JSP (ie, servlet) Write the entire page to the RESPONSE, the network transmission is not high (of course, it is also possible to use HttpResponse Buffer). FASTM puts the entire page or a whole page, written to Response, and the network underlying protocol optimizes the block to achieve the best network transmission efficiency.
FASTM supports dynamically generated JavasCri
PT
. This is a place where PHP and JDynamite are invisible.
FASTM supports JavasCri
PT
Note definition. such as,
// begin Dynamic: Special_Code
... ..
// end Dynamic: Special_Code
This block will be marked as a dynamic block.
FASTM's page definition is completely defined by the PHP page of JDYNAMITE. My idea is PHP Template and JDynamite. My own original thing is the implementation of FASM, and how the method is used. FASTM's code is small, and the technology adopted is also very simple. There is no use of any third party software, only the JDK1.4 itself is used.
FASTM is another leap on the DOM & PHP idea - the separation of TMPLATE DOM and VALUESET DOM.
In the Java page generation technology I have known, I believe that FASTM is the best way from all aspects - fast, easy to use, flexible, powerful. I expect FASTM page generation methods to better solve the headache problem of page generation technology, which can be popular in programmers around the world.