Introduction to the difference between JSP and Freemarker and volicity

Source: Internet
Author: User
Tags define local time zones

Freemarker

Freemarker is a template engine: a generic tool that is based on templates and data to be changed, and used to generate output text (HTML pages, e-mail, configuration files, source code, etc.). It is not intended for end-users, but a Java class library, a component that programmers can embed into the products they develop.

Freemarker is free, based on the Apache License 2.0 release. The template is written as Freemarker template Language (FTL), which is a simple, dedicated language. You need to prepare the data to be displayed in a real programming language, such as database queries and business operations, after which the template displays the data that has been prepared. In the template, it is used primarily to show the data, but outside of the template, notice what data to show.

Introduction JSP with the Freemarker and volicity difference:

In the Java field, there are three main performance layer technologies: JSP, Freemarker, Velocity.
JSP is the most familiar technology of everyone
Advantages:
1. Powerful function, can write Java code
2. Support JSP tag (JSP tag)
3. Support Expression language (EL)
4, the official standard, a wide range of users, a wealth of third-party JSP Tag Library
5, good performance. JSP compiled into class file execution, good performance
Disadvantages:
JSP has no obvious shortcomings, it is not to pick a bone that is, because you can write Java code, such as improper use easily destroy the MVC structure.


Velocity is a template language that appears earlier to replace JSP
Advantages:
1. Cannot write Java code, can achieve strict MVC separation
2, good performance, is said to be better than JSP performance
3, using the expression language, it is said that the expression of JSP language is learning velocity
Disadvantages:
1. Not the official standard
2, user groups and third-party tag libraries do not have more JSP.
3, the JSP tag support is not good enough
Freemarker
Advantages:
1. Cannot write Java code, can achieve strict MVC separation
2, the performance is very good
3. Good support for JSP tags
4, built in a lot of common functions, easy to use
5, macro definition (like JSP tags) very convenient
6. Using an expression language
Disadvantages:
1. Not the official standard
2, user groups and third-party tag libraries do not have more JSP
Reasons to choose Freemarker:
1, performance. Velocity should be the best, followed by the JSP, and the average page Freemarker performance is the worst (albeit just a few milliseconds to more than 10 milliseconds gap). But on pages with complex pages (with a lot of judgment and date amounts formatted), Freemarker performance is better than using tag and El jsp.
2, macro definition is more convenient than JSP tag
3, built-in a lot of common functions. such as HTML filtering, date amount formatting and so on, very convenient to use
4. Support JSP Tag
5. Strict MVC separation can be achieved

Another one:

Velocity is simpler and more lightweight than freemarker, but its function is not freemarker so powerful.

For most applications, using Freemarker is easier than velocity, because velocity also has to write some custom toolbox classes and write some more generic template code over and over and over and over again, thus losing more valuable time at the start of development. In addition, the use of tool classes and workarounds does not seem to be a very effective approach in the template engine. At the same time, Velocity's approach makes it much easier to interact with Java objects in the velocity template, which violates the simple principle, although you can also move the code into the controller implementation. Of course, if you use freemarker like velocity, then freemarker can be as simple as velocity.

Velocity an advantage over Freemarker is that it has a wide range of third-party support and a very large user community, and you can get a lot of help from this community, and the opposite freemarker is much worse. Of course, there are more and more third-party software starting to support Freemarker.

Here are a list of features that Freemarker can do, and Velocity does not (and look at):

1. Support for dates and numbers
You can perform operations and comparisons on any number of types, including arbitrary precision types, not just integers.
You can compare and display (format) Date/time values.

2. Internationalization
You can format the digital area, a variety of built-in and custom number format patterns.
You can format date regions and time zones, a variety of built-in and custom date format patterns.
Identifiers (variable names) can contain non-English letters like accented letters, Arabic letters, kanji, etc.

3. Cyclic processing
You can exit the loop
You can access the internal loop of the outside loop mechanism of the control variable
You can tell if you are currently at the end of the loop.

4. Array processing at the stencil level
You can use the syntax of [i] to access array elements, including original and non-primitive exponents
Can get to the length of the array

5. Macro definition
Macro calls can be passed by location or by name
The macro parameter can be set to a default value, and if the parameter is not specified when the macro is called, the default value is used instead
by < @myMacro >body</@myMacro > can support the nesting of macros
You can call a macro directly by using the "name of the macro" in the text expression
Macros allow you to first use the redefined
Macros can define local variables (the new version of Velocity is also implemented by the #local directive, although the official documentation has not yet been introduced)

6. Namespaces
You can use multiple namespaces for variables. When you create a "macro library" it is very useful because you can prevent name collisions with libraries that request specific variables or with other macro variables.

7. Built-in methods for manipulating strings, lists, and maps that are not related to the Java language

8. Can prompt spelling errors and other errors in the template
When accessing a non-existent variable, Freemarker will error when executing the template, through configuration, you can specify whether Freemarker will stop execution when encountering such an error, or ignore the error, while freemarker logs the problem;
If you enter the name of the error directive, Freemarker throws an exception.

9. More advanced text Output tools
You can enclose a block of the template in a set oftags that'll cause it to apply HTML escaping or XML escaping (or any othe Rtransformation you can express as a freemarker expression for this matter) Onall interpolations (${foo}) in the block.
Freemarker has transforms, which is blocks oftemplate that when rendered, go through a transforming filter. Built-intransforms include whitespace compressor, HTML and XML Escaper. Best of all,you can implement your own transformers as well (i.e. if you generate Javasource code, can write a Java co De Pretty-printer transform and insert itinto the template). Naturally, transforms can be nested.
You can explicitly flush the output writer witha built-in flush-directive.
You can stop the rendering with a built-instop-directive.

10. Text Processing
Java-enabled special character processing, such as \b, \ t, \ n, \f, \ r, \ ", \ ', \ \, and Unicode \xxxxx
In addition to the usual strings, numbers, and Boolean constants you can define lists and map text as well as internal templates

11. Advanced Space Cleanup
Freemarker will remove some extra spaces, jumps, line breaks, and so on, eliminating some of the annoying, obvious extra spaces
Freemarker also provides instructions to delete more than a space

12. Integration with other technologies
Provides a library of JSP tags to embed freemarker templates in JSPs
Can work directly with Python objects

13. More powerful XML conversion capabilities

14. Advanced Template meta-Program
You can snap to any part of the output template background variable
You can arbitrarily interpret the range variable as if it were a template definition


Velocity is a template language that appears earlier to replace JSP
Advantages:
1. Cannot write Java code, can achieve strict MVC separation
2, good performance, is said to be better than JSP performance
3, using the expression language, it is said that the expression of JSP language is learning velocity
Disadvantages:
1. Not the official standard
2, user groups and third-party tag libraries do not have more JSP.
3, the JSP tag support is not good enough

Freemarker
Advantages:
1. Cannot write Java code, can achieve strict MVC separation
2, the performance is very good
3. Good support for JSP tags
4, built in a lot of common functions, easy to use
5, macro definition (like JSP tags) very convenient
6. Using an expression language
Disadvantages:
1. Not the official standard
2, user groups and third-party tag libraries do not have more JSP

Reasons to choose Freemarker:
1, performance. Velocity should be the best, followed by the JSP, and the average page Freemarker performance is the worst (albeit just a few milliseconds to more than 10 milliseconds gap). But on pages with complex pages (with a lot of judgment and date amounts formatted), Freemarker performance is better than using tag and El jsp.
2, macro definition is more convenient than JSP tag
3, built-in a lot of common functions. such as HTML filtering, date amount formatting and so on, very convenient to use
4. Support JSP Tag
5. Strict MVC separation can be achieved


Advantages and disadvantages of Freemarker

I. Introduction of Freemarker
Freemarker is a template engine written in the Java language that generates text output based on a template.
The so-called template, is a copy of the basic content has been written, with a fixed format of the document, which vacated or with placeholders identified content, by the user to fill, different users give different data. The placeholder in the template, when the template is run, is parsed by the template engine and replaces the contents of the placeholder part with Dynamic data.
Freemarker is not related to the Web container, that is, it does not know the servlet or HTTP while the Web is running. Not only can it be used as an implementation technique for the presentation layer, but it can also be used to generate xml,jsp or Java text.


Second, the advantages and disadvantages of Freemarker
(a) Advantages of Freemarker
1. Using one of the advantages of freemarker, you can completely separate the presentation layer and business logic.
The use of JSP development process in the pages of a large number of business logic in the code, so that the content of the page messy, in the late a lot of changes in the maintenance process becomes very difficult.
Freemarker does not support Java scripting code at all.
The principle of Freemarker is: template + data model = output, template is only responsible for the data in the page performance, does not involve any logic code, and all the logic is processed by the data model. The output that the user eventually sees is created after the template and data model are merged.

2. The advantages of using Freemarker can improve the development efficiency.
In our previous development, we used JSP pages to present the data, the so-called presentation layer. We all know that JSP in the first execution of the need to convert to the Servlet class, the development phase of the function tuning timely, the need to frequently modify the JSP, every modification to compile and transform, then imagine the day we wasted in the program compile time. Compared to JSP, Freemarker template technology does not have the problem of compiling and converting, so there is no problem. And in the development process, we do not have to wait for the interface design developers to complete the page prototype, we can then develop the program.

3. The advantages of using the Freemarker three, make the development process of the Division of personnel more clear.
In the past, using JSP to display data, as programmers, we are not familiar with the interface design technology, the interface developers, but also not familiar with the program language. For both, the alternating work itself is difficult. Sometimes a little careless, may be a page element deleted or removed a program symbol, so that the page aliasing or program errors, so that the two sides need to communicate with each other to solve the problem. Sometimes because of the time in the project, the amount of tasks and other factors, it may be done by a person to complete, which may increase the workload of a party developer.
With Freemarker, as an interface developer, just focus on creating HTML files, images, and other visualizations of Web pages, regardless of the data, while the program developer focuses on the system implementation and is responsible for preparing the data to be displayed for the page.


Two Disadvantages of Freemarker
1. Using one of the drawbacks of freemarker, applying freemarker template technology, you may see data that has expired after you modify the template. For example, if a static HTML page is generated, if the template changes and the HTML page generated by the template is not updated in time, the user sees the outdated data.
2. The disadvantage of using Freemarker, Freemarker template technology in the application process, the variables in the Freemarker must be assigned, if not assigned, then an exception will be thrown. If you want to avoid errors, you should use the IF/ELSEIF/ELSE directive to make a sentence, and if you judge every variable, it will increase the trouble of programming.
3. The disadvantage of using Freemarker, Freemarker's map-qualified key must be string, other data types cannot be manipulated
4. The disadvantage of using Freemarker, Freemarker does not support cluster applications. In order to weave the convenience, the serialized things are put in the session, such as Session,request, and so on, in the process of development is indeed convenient, but if the application put in the cluster, there will be errors.

Introduction to the difference between JSP and Freemarker and volicity

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.