Comparison of velocity freemarker

Source: Internet
Author: User
Tags define local time zones

Compared with freemarker, velocity is more simple and lightweight, but its functions are not as powerful as freemarker.

For most applications, freemarker is easier to use than velocity, because velocity must also write custom toolbox classes and write some common template code repeatedly, therefore, more valuable time is lost at the beginning of development. In addition, the use of tool classes and workarounds does not seem very effective in the template engine. At the same time, velocity makes it possible to interact with a large number of Java objects in the velocity template, which violates the simple principle, although you can also transfer the code to the Controller for implementation. Of course, if you use freemarker like velocity, freemarker can be as simple as velocity.

Velocity is superior to freemarker in that it has a wide range of third-party support and a very large user community. You can get a lot of help from this community, on the contrary, freemarker is much worse in this regard. Of course, more and more third-party software is starting to support freemarker.

The following is a list of functions that freemarker can do, but velocity cannot do (and it looks at it ):

1. date and number support
You can perform operations and comparisons on any number of types, including any precision type, not just integers.
You can compare and display (Format) date/time values.

2. Internationalization
You can format the number area in a variety of built-in and custom digital format modes.
You can format date regions and time zones, a variety of built-in and custom Date Format modes.
Identifier (variable name) can contain non-English letters with the same accent, arabic letters, Chinese characters, etc.

3. Loop Processing
You can exit the loop
You can access the internal loop of the External Loop Mechanism of the control variable.
You can see whether the current cycle ends.

4. template-level Array Processing
You can use the [I] syntax to access array elements, including original and non-original indexes.
The length of the array can be obtained.

5. macro definition
Macro call can pass parameters by location or name
The default value can be set for macro parameters. If this parameter is not specified when macro is called, the default value is used instead.
You can use <@ mymacro> body </@ mymacro> to support macro nesting.
You can directly call a macro by using the macro name expressed in the text.
Macros can be used before Definition
Macros can define local variables (the new version of velocity also uses the # local command to implement this function, although the official documentation has not yet introduced)

6. namespace
You can use multiple namespace variables. It is useful when you create a macro library because it prevents name conflicts and libraries that apply for specific variables or other macro variables.

7. built-in Java-independent strings, lists, and map operations

8. The spelling errors and other errors in the template are displayed.
When accessing a non-existent variable, freemarker will report an error when executing the template. Through configuration, you can specify whether the freemarker stops executing this type of error or ignores this error, at the same time, freemarker records this problem in the log;
If you enter the name of the wrong command, freemarker throws an exception.

9. More advanced text output tools
You can enclose a block of template in a set of tags that will cause it to apply HTML escaping or XML escaping (or any other transformation you can express as a freemarker expression for that matter) on all interpolations ($ {Foo}) in the block.
Freemarker has transforms, which are blocks of template that when rendered, go through a transforming filter. built-in transforms include whitespace compressor, HTML and XML escaper. best of all, you can implement your own transformers as well (I. e. if you generate Java source code, you can write a Java code pretty-printer transform and insert it into the template ). naturally, transforms can be nested.
You can explain icitly flush the output writer with a built-in flush-direve ve.
You can stop the rendering with a built-in stop-directive.

10. Text Processing
Supports special characters in Java, such as \ B, \ t, \ n, \ f, \ r, \ ", \ ', \, and Unicode \ XXXXX.
Apart from common strings, numbers, and Boolean constants, you can define the list and map text as well as internal templates.

11. Advanced Space clearing
Freemarker will delete unnecessary spaces, cells, line breaks, and other characters, thus eliminating some annoying and obviously redundant spaces.
Freemarker also provides commands to delete more spaces.

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

13. More powerful XML Conversion Functions

14. Advanced template metaprograms
You can capture any part of the output template background variables.
You can explain the range variable as if it is a template definition.


Velocity is an earlier template language used to replace JSP.
Advantages:
1. Java code cannot be written and strict MVC separation can be implemented.
2. Good performance, which is said to be better than JSP Performance
3. Using expression language, it is said that the expression language of JSP is learning velocity.
Disadvantages:
1. Not an official standard
2. There are not many JSP libraries for user groups and third-party tag libraries.
3. insufficient support for JSP labels

Freemarker
Advantages:
1. Java code cannot be written and strict MVC separation can be implemented.
2. Excellent Performance
3. good support for JSP labels
4. A large number of built-in common functions, which are very convenient to use
5. macro definition (similar to JSP tags) is very convenient
6. Use Expression Language
Disadvantages:
1. Not an official standard
2. There are not many JSP libraries for user groups and third-party tag Libraries

Why freemarker is selected:
1. performance. Velocity should be the best, followed by JSP. The freemarker of common pages has the worst performance (although the gap between several milliseconds and dozens of milliseconds ). However, on pages with complex pages (including a large number of judgments and formatting of date amounts), freemarker has better performance than JSP with Tag and El.
2. Convenient macro definition than JSP tag
3. A large number of built-in common functions. For example, HTML filtering and date amount formatting are very convenient to use.
4. Support for JSP labels
5. Strict MVC Separation


Advantages and disadvantages of freemarker

1. Introduction to freemarker
Freemarker is a Java template engine that generates text output based on the template.
A template is a document with a fixed format that has already been written. The content blank or marked with placeholders is filled by the user, the data given by different users is different. The placeholders in the template are parsed by the template engine when the template is running, and the placeholder content is replaced by dynamic data.
Freemarker has nothing to do with the Web Container, that is, when the web is running, it does not know servlet or HTTP. It can not only be used as the Implementation Technology of the presentation layer, but also be used to generate xml, JSP, or java files.


Ii. Advantages and Disadvantages of freemarker
(1) Advantages of freemarker
1. Use freemarker to completely separate the presentation layer and business logic.
During JSP development, a large number of code with business logic exists in the page, which makes the page content messy and becomes very difficult in the later process of modification and maintenance.
Freemarker does not support Java Script code at all.
Freemarker's principle is: Template + Data Model = output. The template is only responsible for the presentation of data on the page and does not involve any logic code. All logics are processed by the data model. The final output is created after the template and data model are merged.

2. The second advantage of freemarker can improve development efficiency.
In our past development, we used JSP pages to display data, that is, the so-called performance layer. We all know that JSP needs to be converted to servlet class during the first execution. during the development stage, the function is adjusted in a timely manner and JSP needs to be modified frequently. Compilation and conversion are required for each modification, let's imagine how much time we waste in program compilation in a day. Compared with JSP, freemarker template technology does not have compilation and conversion problems, so the above problems do not exist. In addition, during the development process, we do not have to wait for the interface design and development staff to complete the page prototype before developing the program.

3. The third advantage of freemarker makes the division of labor in the development process clearer.
In the past, when JSP was used to present data, as programmers, we were not familiar with the interface design technology. On the contrary, interface developers were not familiar with the programming language. For both, the alternative work itself is difficult. Sometimes, you may accidentally delete a page element or remove a program symbol to make the page sample or program error. In this way, both parties need to communicate and cooperate with each other to solve the problem. Sometimes, because of project time, task volume, and other factors, this work may be completed by one person, which may increase the workload of a developer.
After freemarker is used, as an interface developer, only focus on creating html files, images, and other visual aspects of web pages, so you do not need to care about data. program developers focus on system implementation, prepares the data to be displayed for the page.


(2) Disadvantages of freemarker
1. One of the disadvantages of using freemarker is to apply freemarker template technology. After modifying the template, you may see expired data. For example, after a static html page is generated, if the template is changed but the HTML page generated by the template is not updated in time, the user will see expired data.
2. Use freemarker disadvantages 2. In freemarker template technology, variables in freemarker must be assigned a value during application. If no value is assigned, an exception is thrown. To avoid errors, you need to use the IF/elseif/else command to determine the segment. If you judge every variable, it increases programming complexity.
3. disadvantage 3 of using freemarker. The map limitation key of freemarker must be string, and other data types cannot be operated.
4. disadvantages of freemarker: freemarker does not support cluster applications. For convenience of compilation, all serialized items are put into the session, such as session and request, which is indeed convenient in the development process. However, if the application is put into the cluster, an error occurs.

Comparison of velocity freemarker

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.