Spring MVC 3.0.5 + spring 3.0.5 + mybatis3.0.4 detailed description of all annotation instances (5)

Source: Internet
Author: User
Tags configuration settings website performance

This is the last article in this series. It mainly describes the basic concepts of the freemarker template engine and the usage of Common commands.
1. Basic concepts of freemmarker
Freemmarker is a Java template engine that generates text output based on templates. It is one of the most used Page Template technologies except JSP, And the other famous template is velocity.
You can use freemarker to generate the required content. Generally, the data model is provided by Java. freemarker renders the data model through the template engine, so that we can finally get the desired content. When freemarker is used as the display layer, the access speed is faster, because when the freemarker engine renders a page, it only needs to fill in the Data. Others are fixed template content. In addition, freemarker can easily generate static pages dynamically. For websites with large traffic volumes, nginx can greatly improve website performance.
In the ssm3 example, the view layer is implemented using freemarker template technology. This document describes freemarker knowledge points used in ssm3. For more information, see the official documentation.
At the end of this article, we have the ssm3 source code. If you need the ssm3 source code, you can download it by yourself. The example is the most basic and is easy for beginners to learn and understand.
Ii. freemmarker configuration instructions
In the spring 3.0 framework, freemarker has been integrated and encapsulated moderately. It is very convenient to use. To use the functions of the freemmarker template, you only need to configure the following two items.
A. freemmarker view parser

When we send a rest request to access the resource, when the Controller completes execution and returns the result to us, all the returned strings are specified as result pages. Then, we use the parser above, parse the returned strings into our designed page. In addition, if you want to use the freemarker macro provided by spring, you also need to add the exposespringmacrohelpers attribute to set it to true. You can think of these macros as a function or a method.
B. freemmarker environment Configuration

The preceding configuration sets the freemarker environment attribute. The templateloaderpath specifies the path where the template file is stored. It defines the template prefix, and all displayed pages follow the path rule. The encoding format of the template file is also specified. If the configuration is not displayed, the system default encoding will be used otherwise, which is prone to garbled characters. Therefore, you must set the encoding in a unified manner, do not use gb2312 or GBK encoding, and use Unicode.
UTF-8 coding is a good habit.
In addition to the configuration settings we have seen above, freemarkerconfigurer also has a very important attribute, freemarkervariables, which can be used to set custom command labels developed by ourselves to meet different business needs.
3. freemmarker command mark
To fill in data on the final result page, use the freemarker flag command to do this. However, these markup commands generally do not appear separately. They are basically used in multiple combinations. To make it easy for everyone to understand, the elephant illustrates some pages in ssm3.
Freemarker has two types: pre-defined commands and user-defined commands. Predefine commands start with #, and user-defined commands start. Spring's freemarker macro also starts.

The assign command defines a variable in the format of <# assign
Name = value>. Here CTX is the variable name. value can be a value or expression. In this way, we can reference this variable in all template pages.
In the expression,! If RC. getcontextpath () is null or does not exist, assign the string "/ssm3" as the default value to the CTX variable. In this way, even if the context value cannot be obtained for some reason, the CTX variable will not have a null or incorrect value, resulting in a system exception.
When using freemarker, be sure to note that freemarker never allows reference of non-existent variables (that is, the variable is null), unless it explicitly tells it how to handle a variable that does not exist (null.
This is very important. In most cases, the bug is due to the null value not being processed. Please pay more attention when using freemarker.

Command include to load the template page. You can use it to insert another freemarker template file into a template.

<# If> conditional judgment command, list ?? Whether it is null or not. If the list is not null, It will be executed .?? Generally, it is executed together with the if command.
<# List as entity> Start looping. list is the attribute name defined in the model, as is the syntax of the List loop definition variable, and entity is the name of the List loop variable. $ {...} It has the same meaning as the same expression in jstl. Freemarker is replaced with the actual value in the output.
Download source code: ssm3
This article is original for pineapple elephants. If you want to reprint it, please indicate the source. Http://bolo.blogjava.net/

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.