Template and JSP technology __jsp

Source: Internet
Author: User
Tags html tags php template xslt java web

(This article is issued in the Java EMAG first phase)
First, origin and present situation:

The origins of template and JSPs also go back to the ancient days of web development, when people used CGI to develop web apps and write HTML tags in a CGI program.

After that the world began to move in a different direction: Sun offered a servlet solution similar to CGI, but both CGI and servlet faced the same problem: writing HTML tags in a program is not a sensible solution anyway. Sun Company introduced JSP technology in 1999. In another world, the Scriptlet page scripting technology, represented by PHP and ASP, began to be widely used.

But even so, the problem is not over, and the new problem arises: a mix of business and HTML tags, which not only creates confusion in the page structure but also makes the code itself difficult to maintain.

The MVC pattern, which originated in the late 70, was introduced and developed. MVC's three roles: model--all data and behavior that contain data and behavior other than the UI. View is the display of the model in the UI. Any change in information is handled by the third member of MVC-the controller.

In the following applications, there was a first leap in technology: Front-End display logic and back-end business logic separation, COM components or EJB or CORBA to handle business logic, ASP, JSP and PHP are used for front-end display. This is the Model 1 phase of Web Development (Page Controller mode).

But there are a lot of problems with this development model:

1. The page must write to the Scriptlet calling component to obtain the necessary data.

2. Processing display logic scriptlet code and HTML code mixed interleaved.

3. Difficulty in debugging. The JSP is compiled into a servlet, and the debug information on the page is not sufficient to locate the error.

All of this is because there are no separate views and controllers in Model 1. Completely separating the view and the controller becomes necessary. This is Model 2. It's the unresolved problem in model 1--separating the call to the component (business logic) and porting this part of the work to the controller. Now it seems perfect, but wait, the original controller from the page after the separation, the page required data how to obtain, who will handle the page display logic. Two methods: 1. Continue to use mechanisms such as asp,php or JSP, but because they are running in a Web environment, the data they want to display (the result of the backend logic) needs to be put through the controller into the request stream; 2. The use of new techniques-template technology, the use of independent template technology as a result of the separation of the web environment, the development of testing will bring considerable convenience. The data required for the page is passed into a pojo instead of the request object.

Template technology first began in the world of PHP, the emergence of phplib template and fasttemplate these two heroes. Soon template technology was introduced into the Java Web development world. At present, the more popular template technology is: Xstl,velocity,jdynamite,tapestry and so on. In addition, because JSP technology is the current standard, the equivalent of the system or using JSP to complete the page display logic part, in the Sun Company's Jstl, the various third-party organizations have launched their own taglib, a representative is struts tablib.

Second, the template technical analysis:

In essence, template technology is a placeholder dynamic substitution technology. A complete template technology requires four elements: 0. Template language, 1. Template file containing the template language, 2. A data object that has dynamic data, 3. Template engine. Here's a concrete discussion of these four elements. (In the course of the discussion, I only listed a few different characteristics of the technology, other techniques or a similar will not repeat)

1. Template language:

Template languages include: variable identities and expression statements. According to the different control power of expression, it can be divided into strong control force template language and weak control force template language. According to the compatibility between template language and HTML, it can be divided into compatibility template language and non-compatibility template language.

Template language to deal with three key points:

1. Scalar mark. There are many ways to insert a variable ID into HTML. One is to use HTML-like tags, and the other is to use special identifiers such as velocity or jdynamite; the third is to extend HTML tags, such as tapestry. In what way there are many considerations, a more common consideration is "WYSIWYG" requirements.

2. Condition control. This is a very difficult problem. A simple example is a logistics escort system, the number of items below a certain value to highlight. However, conditional control seems unavoidable for a particular complex display logic. When you put similar to <if condition= "$count <=" ><then><span class= "highlight" >count </span></then ></IF> introduced, just as we did in ASP and PHP, we will have to face the problem of scriptlet Embedded Web page again. I believe you and I do not think this is a good way to write. In fact, not all template technology uses conditional control, many existing applications such as PHP and I have seen a asp.net application, of course, and Java Jdynamite. There is no logic on the Web page, but the cost is to transfer the highlighted selection control to the programming code. You must make a choice. Maybe you're like me. You don't want to use conditional controls on Web pages, nor do you want to write HTML tags in your code, but this display logic is inescapable (if you don't want to be copied by your boss), one possible way is to use CSS to decide which CSS style to use in your programming code. In particular, CSS2 technology, its selector mechanism, can apply different styles depending on the HTML type and even the attributes of the element.

3. Iteration (Loop). Displaying a data form on a Web page is a very basic requirement that using a collection tag will be unavoidable, but fortunately, it is usually simple and sufficient. It is particularly worth mentioning that the PHP template technology and Jdynamite technology to use the HTML annotation tag is very simple to achieve it, but also maintain the "WYSIWYG" characteristics.

The following are some technical comparisons:

Velocity

Variable definition: with $ flag

Expression statement: Start with #

Strong control Language: variable assignment: #set $this = "Velocity"

External references: #include ($)

Condition control: #if ... #end

Non-compliant languages

Jdynamite

Variable definition: Wrapping with {}

Expression statement: Written in annotation format (<!--à)

Weak control language

Compatible languages

Xslt

Variable definitions: XML tags

Expression: XSL label

Strong Control Language: External reference: Import,include

Conditional control: If, choose...when...otherwise

Non-compliant languages

Tapestry

Developed in the form of component.

Variable definition (component definition): Add Jwcid to HTML tags

Expression statement: OGNL specification

Compatible languages

2. template file:

A template file refers to a text file that contains a template language.

Template files have different results because of their template language compatibility. Template files with HTML compatibility are just a resource file with good reusability and maintainability. For example, jdynamite template files can be reused in different projects, even with template files for PHP programs. The non-compliant template files, such as velocity, are substantially less reusable and maintainable because they are in fact a scripting program.

3. Data objects with Dynamic Data:

The template file contains static content, and the required dynamic data needs to be provided separately. Depending on how the data is provided, it can be divided into 3 different ways:

1. Map: Use Key/value to locate. This is the most common technique. The velocitycontext of velocity is the inclusion of the map object.

EXAMPLE.VM:

Hello from $name $project project.

Example.java:

Velocitycontext context = new Velocitycontext ();

Context.put ("name", "Velocity");

Context.put ("Project", "Jakarta");

2. DOM: Direct manipulation of DOM data objects, such as XSLT using XPath technology.

3. POJO: Direct use of reflection to obtain a Dto object, using the JavaBean mechanism to obtain data. such as Tapestry.

4. Template engine:

The work of the template engine is divided into three steps:

1. Get the template file and verify that the template language conforms to the specification.

For example velocity, determine #if have corresponding #end. In the XML+XSLT model, the XML file tag is intact and so on. After completing these tasks, the template engine typically resolves the template file into a node tree (a static content node that contains template files and a special node defined by the template engine).

2. Get the data object.

This data object is typically implemented through a program pass reference. The existing large number of frameworks are completed at the bottom of the program, the processing methods are different, and there are two technologies, namely push technology and pull technology. Push technology: Controller calls the Set method to inject dynamic data, the template engine is obtained through get method, typical representative: Struts; Pull technology: Template engine According to configuration information, find the model with view, call model get method to obtain data, Typical representative: Tapestry.

3. Merge template files (static content) and data Objects (dynamic content) and generate final pages.

The merging mechanism is generally as follows, the template engine traverses each node of the node tree and render the node, encounters the static content node to get the corresponding value from the data object, and executes its expression statement (if any) when encountering a special node.

The following detailed description:

Velocity

Template Template = velocity.gettemplate ("Test.wm");

Context context = new Velocitycontext ();

Context.put ("foo", "Bar");

Context.put ("Customer", New Customer ());

Template.merge (context, writer);

When the Velocity.gettemplate method is invoked, the Resourcemanger's corresponding method is invoked.

Resourcemanger first see if the template file is in cache, if not, get it, generate the resource object and invoke the process () method, determine whether the template is valid, and, if it is, generate a node tree in memory.

When Template.merge () is invoked, the node tree is traversed and the Render method of each node is invoked. For variables and object node in the template, the Execute () method is also invoked to obtain value from the context.

Note: Resourcemanger under Runtime/resource Package, node under Runtime/parser/node package

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.