Comparison of jsp, velocity, freemark page engine, velocityfreemark
In the java field, there are three main presentation layer technologies: jsp, freemarker, and velocity.
Jsp is the most familiar technology.
Advantages:
1. Powerful functions. You can write java code.
2. Support for jsp tags)
3. Expression Language (el) supported)
4. Official Standard, wide user base, and rich third-party jsp tag Library
5. good performance. Jsp compiled into a class file for execution, with good performance
Disadvantages:
Jsp has no obvious disadvantages, so you have to pick out the points. That is, Because java code can be compiled, improper use may easily damage the mvc structure.
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
The following is a list of features that can be implemented using FreeMarker, but cannot be implemented by Velocity:
(1) Number and date support
Arithmetic Operations and comparisons can be performed on any numeric type, including precision types.
Allows you to compare and display (Format) date/time values.
(2) Internationalization
Format local sensitive numbers based on various built-in and custom numeric formats
Format local and Time Zone sensitive dates based on various built-in and custom date formats
The identifier (variable name) can contain non-English characters, such as accent characters, Arabic characters, and Chinese characters.
(3) loop Processing
Can jump out of the loop
Attackers can access control variables in the circular body outside the circular environment.
You can test whether the last cycle is reached.
(4) template-level Array Processing
You can access array elements using indexes similar to [I] syntax.
Query the length of an array.
(5) Macro
Macros can have local variables.
You can call macros recursively. You can also define the macros to be called after the template.
When a macro is called, parameters can be passed by location or name.
Macro parameters can have default values so that the ignored parameters are also valid during calls.
The called macro can have nested body content (<@ myMacro> body </@ myMacro>), which can be processed when the macro is called.
Macros are pure variables. They can be executed based on expressions or passed to another macro as parameters.
(6) namespace
Multiple namespaces can be used for variables, which is important for creating macro libraries, because this can avoid conflicts between the specified variables in the application and the names of variables in the macro library.
(7) use built-in functions/operators to maintain Java-independent string, list, and map
Converts a string to uppercase/lowercase, and the first character to uppercase/lowercase. Escape HTML, XML, or RTF, substring, split, query string length, find/replace substring, and so on.
Access the list element through the index, obtain the sublist, merge the list, query the list length, and sort the list.
Access the map element through the key variable and check whether the map is empty to obtain the list of keys or values.
(8) reveal errors in the template
When accessing an undefined variable, FreeMarker will not be silenced; you can configure FreeMarker to stop displaying error information in the render template, or skip the error section; either way, FreeMarker will record the problem (log)
FreeMarker throws an exception when an incorrect command name is written.
(9) Advanced render Control
A group of tags can be used to encapsulate an area of the template, so that HTML or XML escaping can be applied to all areas in the block area to be modified (or other conversions represented by FreeMarker expressions)
FreeMarker has a converter, which is a part of the template. During render, It is filtered by conversion. The built-in Converter includes space character compression, HTML, and XML escape. You can implement your own converter; of course, the converter can be nested
You can use the flush command to explicitly flush the output.
You can use the stop command to stop render.
(10) Text
In addition to the common string, number, and Boolean text, you can also define the list and map text in the template.
All Java escape characters are supported: \ B, \ t, \ n, \ f, \ r, \ ", \ ', \, and \. UNICODE characters can also be used for \ xXXXX.
(11) Remove advanced white space characters
FreeMarker insists that each row be removed and only contains blank characters not marked by the FreeMarker output.
For instructions that clearly require unneeded white space characters, the white space character is a big problem.
(12) integrate other technologies
You can use JSP tag library in the template
It can be used directly in the Python project.
(13) Powerful XML Conversion capabilities
In MySQL 2.3, FreeMarker has powerful new XML Conversion capabilities, making it possible to replace XSLT.
Velocity cannot really compete in this aspect, unless the Core Engine is improved, such as macro library ing to namespace, macro supports local variables
(14) Advanced template metaprogram
Capture any part of the output to the context variable.
Can interpret any context variable, if it is a template Definition
Combined use of the above two
What is the difference between velocity and JSP?
Before JSP, you can use Servlet to send HTML code to the client. But to do so, you need to write a lot of HTML Content Code. The program looks messy and you must be careful about HTML syntax errors. So JSP appears. JSP processes the data to be displayed in HTML + Java code. Some Servlet objects are encapsulated to facilitate calls on the page, and the tag library is also used to conveniently control the output and Use JavaBean. In this case, Java code will still be mixed in the JSP page, and the display and business logic cannot be completely separated. Velocity can be seen as a third party, separating pages from data processing and decoupling them.
How to Use the freemark tag in jsp pages