第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

FreeMarker | 取值篇

標(biāo)簽:
Java SpringBoot
第一部分:Spring Boot 集成 FreeMarker

1、pom.xml 需要这些依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2、yml

我喜欢 yml,所以删掉 application.properties,新建 application.yml

3、配置

application.yml 中添加如下配置

# freemarker
spring:
      freemarker:
                template-loader-path: classpath:/templates/
                cache: false
                charset: UTF-8
                check-template-location: true
                content-type: text/html
                expose-request-attributes: true
                expose-session-attributes: true
                request-context-attribute: request
                suffix: .html

4、Controller - View

package com.fengwenyi.demo.freemarker.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Wenyi Feng
 */
@Controller
public class HomeController {

    @RequestMapping("/")
    public ModelAndView home() {
        ModelAndView mv = new ModelAndView("home");
        return mv;
    }

}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>freemarker</title>
</head>
<body>
<center>
    <h1>Hello FreeMarker!</h1>
    <h6>Welcome to Learn FreeMarker With Me!</h6>
    <div><i>目录</i></div>
    <div>
        <ul>
            <li><a target="_blank" href="/value/get">取值 - 2018.06.13</a> </li>
            <li><a target="_blank" href="/value/get">运算,集合 - 2018.06.14</a> </li>
            <li><a target="_blank" href="/value/get">高级技巧 - 2018.06.15</a> </li>
        </ul>
    </div>
</center>
</body>
</html>

5、运行效果

Hello World

第二部分:取值

1、学过 freemarker 的的童鞋都知道,需要在spring的配置文件中添加上一些属性,那Spring boot 应该怎么做呢?

# freemarker
spring:
      freemarker:
                template-loader-path: classpath:/templates/
                cache: false
                charset: UTF-8
                check-template-location: true
                content-type: text/html
                expose-request-attributes: true
                expose-session-attributes: true
                request-context-attribute: request
                suffix: .html
                settings:
                      #number_format: '0.##'   #数字格式化,无小数点,如果有小数,只保留两位小数
                      number_format: '0.#############################################'
                      date_format: 'yyyy_MM_dd HH:mm:ss' # 这个是对java.sql.Date起作用
                      # boolean_format: 'Y, N' # 一般不这么配置,因为我们需要逻辑值,如果需要,我们可以在需要的地方将Boolean->String

2、Controller


@GetMapping("/get")
public ModelAndView getValue() {
    ModelAndView mv = new ModelAndView("get-value");
    mv.addObject("intVar", 100);
    mv.addObject("longVar", 100000000000000L);
    mv.addObject("stringVar", "我是字符串");
    mv.addObject("doubleVar", Math.PI);
//        mv.addObject("doubleVar", 3.14);
//        mv.addObject("doubleVar", 3.1415D);
    mv.addObject("booleanVar", Boolean.TRUE);
    mv.addObject("dateUtilVar", new Date());
    mv.addObject("dateSqlVar", new java.sql.Date(new Date().getTime()));
    mv.addObject("nullVar", null);
    return mv;
}

3、View

<table border="1">
    <tr>
        <th width="200">Type</th>
        <th width="300">Value</th>
    </tr>

    <tr>
        <td align="center">Integer</td>
        <td align="center"><font color="red">${intVar}</font></td>
    </tr>
    <tr>
        <td align="center">Long</td>
        <td align="center"><font color="red">${longVar}</font></td>
    </tr>
    <tr>
        <td align="center">String</td>
        <td align="center"><font color="red">${stringVar}</font></td>
    </tr>
    <tr>
        <td align="center">Double</td>
        <td align="center"><font color="red">${doubleVar}</font></td>
    </tr>
    <tr>
        <td align="center">Boolean</td>
        <td align="center"><font color="red">${booleanVar?string('Yes', 'No')}</font></td>
    </tr>
    <tr>
        <td align="center">java.util.Date</td>
        <td align="center"><font color="red">${dateUtilVar?string('yyyy/MM/dd HH:mm:ss')}</font></td>
    </tr>
    <tr>
        <td align="center">java.sql.Date</td>
        <td align="center"><font color="red">${dateSqlVar}</font></td>
    </tr>
    <tr>
        <td align="center">null</td>
        <td align="center"><font color="red">${nullVar!}</font></td>
    </tr>
    <tr>
        <td align="center">null</td>
        <td align="center"><font color="red">${nullVar! '-'}</font></td>
    </tr>
    <tr>
        <td align="center">不存在的变量</td>
        <td align="center"><font color="red">${notExist! '-'}</font></td>
    </tr>
</table>

4、效果

取值

资料

1、FreeMarker 模块的代码已上传至Github:
https://github.com/fengwenyi/FreeMarker-demo

2、学习视频:Java模板引擎之Freemarker

點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
JAVA開發(fā)工程師
手記
粉絲
1.4萬
獲贊與收藏
707

關(guān)注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學(xué)

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消