Freemarker 从入门到放弃

Freemarker 从入门到放弃

site 官网

http://freemarker.org/

当前版本version 2.3.23 ~ 2.3.25

文件大小size: freemarker-2.3.23.jar 1.35MB

基础知识

模板 + 数据 = 视图

模板 ftl = freemarker template language, 即freemarker是一种语言

数据 java : bean map method
ftl : function, macros
jstl : buildin objects

保留关键字

true: boolean value true''
false: boolean value
false”
gt: comparison operator greater than''
gte: comparison operator
greater than or equivalent”
lt: comparison operator less than''
lte: comparison operator
less than or equivalent”
as: used by a few directives
in: used by a few directives
using: used by a few directives

注释

<#-- uses mouse as a string -->

 #@$%&?![]<>{}, 
\用来转义

系统指令directive #list #if #else #items #assign #macro

 #if + #else
 #list + #else
 #include

?

 二目?     
 fruits?join(", ")?upper_case


 三目 a ? b : c

!

 <h1>Welcome ${user!"visitor"}!</h1>

比较与运算

<#if cargo.weight < 100>Light cargo</#if>

${cargo.weight / 2 + 100}

小抄 Quick overview (cheat sheet)

类型值 Specify values directly

Strings: "Foo" or 'Foo' or "It's \"quoted\"" or 'It\'s "quoted"' or r"C:\raw\string"
Numbers: 123.45
Booleans: true, false
Sequences: ["foo", "bar", 123.45]; Ranges: 0..9, 0..<10 (or 0..!10), 0..
Hashes: {"name":"green mouse", "price":150}    

获取值 Retrieving variables

Top-level variables: user
Retrieving data from a hash: user.name, user["name"]
Retrieving data from a sequence: products[5]
Special variable: .main

字符串操作 String operations

Interpolation and concatenation: "Hello ${user}!" (or "Hello " + user + "!")
Getting a character: name[0]
String slice: Inclusive end: name[0..4], Exclusive end: name[0..<5], Length-based (lenient): name[0..*5], Remove starting: name[5..]

序列操作 Sequence operations

Concatenation: users + ["guest"]
Sequence slice: Inclusive end: products[20..29], Exclusive end: products[20..<30], Length-based (lenient): products[20..*10], Remove starting: products[20..]

哈希操作 Hash operations

Concatenation: passwords + { "joe": "secret42" }

Arithmetical calculations: (x * 1.5 + 10) / 2 - y % 100

Comparison: x == y, x != y, x < y, x > y, x >= y, x <= y, x lt y, x lte y, x gt y, x gte y, ...etc.

Logical operations: !registered && (firstVisit || fromEurope)

Built-ins: name?upper_case, path?ensure_starts_with('/')

Method call: repeat("What", 3)

空值处理 Missing value handler operators:

Default value: name!"unknown" or (user.name)!"unknown" or name! or (user.name)!
Missing value test: name?? or (user.name)??

赋值操作
Assignment operators: =, +=, -=, *=, /=, %=, ++, –

macros @

定义

<#macro greet>
  <font size="+2">Hello Joe!</font>
</#macro>

使用

<@greet/>

定义

<#macro greet person>
Hello ${person}!

tags @ 用户自定义指令

Configurations

  1. IDE eclipse要安装JBoss套件,编辑语法高亮等
  2. Sublime也有支持ftl的插件
  3. Freemarker的各种参数

用Spring进行配置


 <bean id="freemarkerConfig"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

        <property name="templateLoaderPath">

            <value>/WEB-INF/ftl/</value>

        </property>

        <property name="freemarkerSettings">
            <props>
                <prop key="locale">zh_CN</prop>
                <prop key="number_format">0.####</prop>
                <prop key="default_encoding">UTF8</prop>
            </props>
        </property>
        <property name="freemarkerVariables">
            <map>
                <entry key="t_mainSite_topic" value-ref="t_mainSite_topic"/>
                <entry key="t_cms_content" value-ref="t_cms_content"/>
                <entry key="t_ceiling" value-ref="t_ceiling"/>
                ......
                </map>
        </property>
    </bean>

直接Java代码配置 freemarker.template.Configuration

// Create your Configuration instance, and specify if up to what FreeMarker
// version (here 2.3.25) do you want to apply the fixes that are not 100%
// backward-compatible. See the Configuration JavaDoc for details.
Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);

// Specify the source where the template files come from. Here I set a
// plain directory for it, but non-file-system sources are possible too:
cfg.setDirectoryForTemplateLoading(new File("/where/you/store/templates"));

// Set the preferred charset template files are stored in. UTF-8 is
// a good choice in most applications:
cfg.setDefaultEncoding("UTF-8");

// Sets how errors will appear.
// During web page *development* TemplateExceptionHandler.HTML_DEBUG_HANDLER is better.
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

// Don't log exceptions inside FreeMarker that it will thrown at you anyway:
cfg.setLogTemplateExceptions(false);

如何开始一个项目的七种办法

Freemarker CLI, 难度等级 ★★★

~/workspace/freemarker_cli

make.sh

  1 #!/bin/bash
  2 cls=~/.m2/repository/org/freemarker/freemarker/2.3.23/freemarker-2.3.23.jar
  3 ls -al $cls
  4 javac -classpath $cls *.java

run.sh

  1 #!/bin/bash
  2 cls=~/.m2/repository/org/freemarker/freemarker/2.3.23/freemarker-2.3.23.jar:.
  3 java -classpath $cls Free

Freemarker as Servlet, 难度等级 ★★

~/workspace/freemarker_servlet

  1. 拷贝freemarker-2.3.25.jar到lib
  2. 编辑web.xml, 增加
     <servlet>
      <servlet-name>freemarker</servlet-name>
      <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>freemarker</servlet-name>
      <url-pattern>*.ftl</url-pattern>
      <!-- HTML and XML auto-escaped if incompatible_improvements >= 2.3.24: -->
      <url-pattern>*.ftlh</url-pattern>
      <url-pattern>*.ftlx</url-pattern>
    </servlet-mapping>  
  1. 增加你的servlet和模板 hello.ftl, 可参考waterproofnet项目

    [Context Menu] -> [new] -> [Sevlet]

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.getRequestDispatcher("hello.ftl")
                .forward(request, response);
    }

Freemarker maven in Eclipse, 难度等级 ★

「eclipse」·「new」·「Maven Project」·「Select a Archetype」
「Catalog:Nexus Indexer」
spring-boot-sample-web-freemarker-archetype
[next] -> 写上你项目的组名和项目名,Group Id, Artifact Id, Version, Package
[Finish]

And Rock!

Freemarker Online, 难度等级 ★

Maven Archetype Generate
~/workspace/freemarker_online

spring-boot, 难度等级 ★★

Copy

mvn archetype:generate
mvn spring-boot:run

项目用例之jeecms, 难度等级 ★★★★

(略)

项目用例之adminssll, 难度等级 ★★★★★

项目里在致有三种不同的使用方式

首页模板

StaticPageGenerator.java

找家门

NeighborhoodController.java

动态专题

SpecialTopicController.java

To configure your project to inherit from the spring-boot-starter-parent simply set the parent:



org.springframework.boot
spring-boot-starter-parent
1.3.6.RELEASE


org.springframework.boot
spring-boot-starter-freemarker
1.3.6.RELEASE

Using Spring Boot without the parent POM

Not everyone likes inheriting from the spring-boot-starter-parent POM. You may have your own corporate standard parent that you need to use, or you may just prefer to explicitly declare all your Maven configuration.

If you don’t want to use the spring-boot-starter-parent, you can still keep the benefit of the dependency management (but not the plugin management) by using a scope=import dependency:





org.springframework.boot
spring-boot-dependencies
1.3.6.RELEASE
pom
import


docker、docker、docker

j360-docker打包发布j360-boot到docker

https://github.com/xuminwlt/j360-docker

spring-boot入门工程之

https://github.com/xuminwlt/j360-boot

spring-boot官方地址

http://projects.spring.io/spring-boot/

思考题

. 如何在页面上显示 ${…}
. 如何缩略字符串
. 如何显示中文日期时间

参考答案

  1. 使用转义或raw类型
    {…}
    {r"{…}”}

  2. 全用区间类型 Ranges, Length limited ranges were introduced in FreeMarker 2.3.21

Ranges are just sequences, but they are created by specifying what range of whole numbers they contain, instead of specifying their items one by one. For example, 0..


    <#-- Parsing with SimpleDateFormat patterns: -->
    <#assign someDate = "10/25/1995"?date("MM/dd/yyyy")>
    <#assign someTime = "15:05:30"?time("HH:mm:ss")>
    <#assign someDatetime = "1995-10-25 03:05 PM"?datetime("yyyy-MM-dd hh:mm a")>

    <#-- Parsing with custom date formats: -->
    <#assign someDatetime = "October/25/1995 03:05 PM"?datetime.@worklog>

Warning!

A frequent mistake of users is the usage of interpolations in places where they needn’t/shouldn’t/can’t be used. Interpolations work only in text sections (e.g.

Hello {name}!</h1>) and in string literals (e.g. <#include "/footer/{company}.html”>). A typical WRONG usage is <#if {big}>...</#if>, which will cause a syntactical error. You should simply write <#if big>...</#if>. Also, <#if "{big}”>…

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值