Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the entry-level knowledge points of FreeMarker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this article, the editor introduces in detail "what are the introduction to FreeMarker knowledge points", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "introduction to FreeMarker knowledge points" can help you solve your doubts.

FreeMarker is a java-based template engine for generating text (such as HTML). Its syntax and function are similar to JSP, where JSP relies on Servlet containers (such as Tomcat), while FreeMarker only needs the Java runtime environment.

This picture on the official website vividly illustrates the purpose and working principle of FreeMarker: template + data = text (HTML)

In Jspxcms, a template is a template file (also with .html suffix) in the / template/1/default/ directory, and the data comes from SpringMVC Controller's Model.addAttribute (String name, Object value) or Jspxcms custom tags (such as: [@ InfoList node='news';list]. [/ @ InfoList]).

For more information, please refer to the documentation on the official website of FreeMarker, and the official FreeMarker also provides FreeMarker Chinese documentation.

Expression.

The EL expression is basically the same as JSP's: ${myname}, ${user.username}.

Gets the Map value. ${customs ['abc']}

Gets the array value. ${arr [0]}

Arithmetic. +-* /%. For example, ${100-xampx} ${xamp2} ${12}

Comparison operation.

< = >

= =! = or lt lte gt gte

Logical operation. | | & &!

Built-in function (Built-in)

Built-in functions are a major advantage of FreeMarker. Many of the functions that need to be implemented through complex processing in JSP can only be achieved with simple built-in functions.

Null value processing. ${mouse! "No mouse"} ${username! "anonymous user"}. The output null value in FreeMarker will report an error, and if you want nothing to be displayed and no error reported when the object is null, you can handle ${mouse!} ${user.username!} ${(user.username)!} (the last way to avoid errors caused by the user object being null).

Boolean value processing. ${foo?string ("yes", "no")}

Date processing. ${lastUpdated?string ("yyyy-MM-dd HH:mm:ss")}

HTML escape. ${username?html}. To avoid direct output

< >

Equivalence, which leads to a XSS attack, usually escapes the output value.

JS escape. ${foo?js_string}. It is useful to deal with characters such as quotation marks in js, and it is useful to assign values to js variables, such as var s = "${foo?js_string}".

Get the substring. ${'abc'?substring (2)} ${username?substring (0P3)}

Gets the string length. ${'abc'?length} ${username?length}

Gets the list size. ${list?size}

Lowercase conversion. ${GrEeN MoUsE "? lower_case}

Uppercase conversion. ${GrEeN MoUsE "? upper_case}

Label (Directive)

The FreeMarker tag is similar to the JSP tag. Tags use angle brackets by default

< >

In order to avoid confusion with HTML tags in Jspxcms and facilitate editing in Dreamweaver, square brackets [] are used as tag symbols. The following examples all use square brackets.

There are two kinds of tags, one is the system comes with a tag that starts with [#, and the other is a custom tag that starts with [@.

Comment tag: [#-this is the code that needs to be commented -]

Custom label

Take the InfoList tag as an example.

[@ InfoList node='news';infos] [# list infos as info] ${info.title} [/ # list] [/ @ InfoList] [@ InfoList node='news';list] [# list list as bean] ${bean.title} [/ # list] [/ @ InfoList] [@ InfoList;list] [# list list as bean] ${bean.title} [/ # list] [/ @ InfoList]

The tag name is InfoList. The tag used to get the list of documents.

Parameter node='news'. Node is the parameter name, and 'news' is the parameter value. Get the document whose column code is news. Sometimes parameters are not required.

Return value; infos. Semicolon; followed by the return value. Infos is the object returned by the label. The list of documents obtained by the tag is stored in this object, and the name of the object can be defined at will.

If (judgment tag) [# if 2 > 1]... [# elseif username== "abc"]... [# elseif username?starts_with ("red")]... [# else]... [/ # if]

Determine whether it is null: [# if username??]... [/ # if]

List (list label) [# list sequence as item]... [/ # list] [# list 1.. 10 as I]... [/ # list]

Gets the sequence number of the loop ${item_index} ${item_index + 1}.

Determine whether there is another object [# if item_has_next]... [/ # if]

Item in sequence as item must be consistent with item_index and item_has_next. If sequence as info, use ${info_index}, ${info_has_next}.

Break (pop-out tag) [# list seq as x] ${x} [# if x = "spring"] [# break] [/ # if] [/ # list] include (including tag) [# include "/ common/copyright.ftl"] [# include "/ common/navbar.html" parse=false /]

Parse . Boolean value, default true. Whether to parse the included template. If you want to include a plain text file without parsing the contents, you can set it to false.

Assign (define variable label) [# assign myname= "abc"] [# assign myname=username] [# assign myname] Confucius [/ # assign] escape (escape label)

To avoid cross-site scripting attacks (XSS), HTML escapes the output, such as ${foo?html}. But all variables have to do this escape is not only troublesome, but also easy to forget. In addition, FreeMarker null value handling is also troublesome and easy to forget, such as ${foo!}, ${(user.username)!}.

Using excape tags can solve this problem very well.

[# escape x as (x)!? html]... ${user.username}... [/ # escape]

As long as the code contained in this tag is equivalent to ${(foo.bar)!? html}, for example, ${user.username} is equivalent to ${(user.username)!? html}. It includes both null value handling and HTML escape processing.

Noescape (do not escape tags)

You can use the noescape tag when there are objects within the escape tag that do not need to be escaped.

[# escape x as (x)!? html]... [# noescape] ${text} [/ # noescape]... [/ # escape] here, this article "what are the basic knowledge points of FreeMarker" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report