1. 程式人生 > >SpringBoot之整合FreeMarker

SpringBoot之整合FreeMarker

目錄:

一.執行springboot_freemarker工程

二.Springboot-freemarker工程配置詳解

三.小結

一.執行springboot_freemarker工程

    git clone 下載工程springboot_repository,專案地址見[email protected]:snail-wj/springboot_repository.git

    1.資料庫準備

        建庫
        create database springbootdb;
        建表
        create TABLE person(
id int AUTO_INCREMENT PRIMARY KEY,
person_name VARCHAR(50) NOT NULL ,
person_gender VARCHAR(50) NOT NULL ,
description VARCHAR(50) NOT NULL
);
    2.專案結構介紹
org.spring.springboot.controller - Controller 層
org.spring.springboot.dao - 資料操作層 DAO
org.spring.springboot.domain - 實體類
org.spring.springboot.service - 業務邏輯層
Application - 應用啟動類
resources/application.properties - 應用配置檔案,應用啟動會自動讀取配置
resources/web - *.ftl檔案,是 FreeMarker 檔案配置路徑。在 application.properties 配置
resources/mapper - DAO Maper XML 檔案
    3.執行工程
        右鍵執行springboot-freemarker工程Application應用啟動類的main函式,然後在瀏覽器訪問:
        獲取ID編號為1的城市資訊
localhost:8080/api/city/1
        獲取城市列表頁面
localhost:8080/api/city

二.Springboot-freemarker工程配置詳解

        1.pom.xml檔案詳解
        
<?xml version="1.0" encoding="UTF-8"?>
	<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
		<modelVersion>4.0.0</modelVersion>
	 
		<groupId>springboot</groupId>
		<artifactId>springboot-freemarker</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<name>springboot-freemarker :: Spring Boot 整合 FreeMarker 案例</name>
	 
		<!-- Spring Boot 啟動父依賴 -->
		<parent>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-parent</artifactId>
			<version>1.5.1.RELEASE</version>
		</parent>
	 
		<properties>
			<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
			<mysql-connector>5.1.39</mysql-connector>
		</properties>
	 
		<dependencies>
			<!-- Spring Boot Freemarker 依賴 -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-freemarker</artifactId>
			</dependency>
	 
			<!-- Spring Boot Web 依賴 -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-web</artifactId>
			</dependency>
	 
			<!-- Spring Boot Test 依賴 -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-test</artifactId>
				<scope>test</scope>
			</dependency>
	 
			<!-- Spring Boot Mybatis 依賴 -->
			<dependency>
				<groupId>org.mybatis.spring.boot</groupId>
				<artifactId>mybatis-spring-boot-starter</artifactId>
				<version>${mybatis-spring-boot}</version>
			</dependency>
	 
			<!-- MySQL 連線驅動依賴 -->
			<dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
				<version>${mysql-connector}</version>
			</dependency>
	 
			<!-- Junit -->
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>4.12</version> 
			</dependency>
		</dependencies>
	</project>
        我們在pom.xml中增加Spring Boot FreeMarker依賴
    2.配置FreeMarker
        然後在application.properties中加入FreeMarker相關配置:
## Freemarker 配置
## 檔案配置路徑
spring.freemarker.template-loader-path=classpath:/web/
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
    3.展示層Controller詳解
            
	/**
		 * @author WJ
		 * @date 2018/04/26
		 */
		@Controller
		public class CityController {

			@Autowired
			private CityService cityService;

			@RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
			public String findOne(Model model, @PathVariable("id") Long id){
				model.addAttribute("city" , cityService.findOne(id));
				return "city";
			}


			@RequestMapping(value = "/api/city", method = RequestMethod.GET)
			public String findAll(Model model){
				List<City> cityList = cityService.findAll();
				model.addAttribute("cityList", cityList);
				return "cityList";
			}
		}
        a.這裡不是走HTTP+JSON模式,使用了@Controller而不是先前的@RestController        b.方法返回值是String型別,和.application.properties配置的Freemarker檔案
配置路徑下的各個*.ftl檔名一致,這樣才會準確的把資料渲染到ftl檔案裡面進
行展示        c.用Model類,向Model加入資料,並指定在該資料在Freemarker取值指定的名稱。

三.小結

    再次感謝泥瓦匠BySocket

相關推薦

SpringBoot整合FreeMarker

目錄:一.執行springboot_freemarker工程二.Springboot-freemarker工程配置詳解三.小結一.執行springboot_freemarker工程    git clone 下載工程springboot_repository,專案地址見[em

springboot(3)——整合freemarker模板、AOP統一處理、全域性異常處理

《三》、整合freemarker模板、AOP統一處理、全域性異常處理 一、整合freemarker模板引擎 1、引入freemarker依賴 <dependency> <groupId>org.springframe

SpringBoot整合Thymeleaf-基於SpringBoot2.0.2版本

前言 Thymeleaf 是一個優秀的、面向Java 的XML慶HTML/HTML5 頁面模板,具有豐 富的標籤語言和函式。因此,在使用Spring Boot 框架進行頁面設計時, 一般都會選擇

SpringBoot整合Druid資料來源

在上一節的專案上引入依賴 引入依賴 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boo

springboot整合thymeleaf

引入依賴 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starte

SpringBoot 整合郵件服務.

一、前言     Spring Email 抽象的核心是 MailSender 介面,MailSender 的實現能夠把 Email 傳送給郵件伺服器,由郵件伺服器實現郵件傳送的功能。     Spring 自帶了一個 MailSender 的實現 JavaMailSenderImpl,它會使用 Ja

SpringBoot整合redis實現快取

主要程式碼: String key = "teacher_"+id; boolean hasKey = redisTemplate.hasKey(key); ValueOperations<String,Teacher> o

SpringBoot整合Spring AOP

package com.zkn.learnspringboot.aop; import com.alibaba.fastjson.JSON; import com.google.common.collect.Maps; import org.aspectj.lang.JoinPoint; import or

springboot整合jedis的使用

在pom.xml匯入jedis包  <!--redis客戶端--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis<

springboot整合thyemeleaf

一.前言 二.整合 1.poml.xml新增包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-sta

SpringBoot整合Redis分析和實現-基於Spring Boot2.0.2版本

背景介紹 公司最近的新專案在進行技術框架升級,基於的Spring Boot的版本是2.0.2,整合Redis資料庫。網上基於2.X版本的整個Redis少之又少,中間踩了不少坑,特此把整合過程記錄,以供小夥伴們參考。 本文的基於在於會搭建Spring Boot專

springboot整合jackson

springboot預設配置json轉換工具就是jackson, 本文介紹自定義各種配置的方法, 廢話不多說 直接上程式碼 配置檔案: #jackson #日期格式化 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss #spri

springboot整合jsp(springMVC)

1、大體步驟 (1)       建立Maven web project; (2)       在pom.xml檔案新增依賴; (3)       配置application.proper

SpringBoot整合Redis

override () XML val urn turn 整合 prope work 一、SpringBoot整合單機版Redis 1、在pom.xml文件中加入redis的依賴 <dependency> <groupId>org.spring

一頭扎進springboot使用Freemarker模板引擎渲染web檢視

在springboot的官方文件中是不建議在專案中使用jsp這樣的技術的,取而代之的是freemark、velocity這樣的模板引擎。 首先和大家來說一下這個模板引擎的概念,這裡特指用於web開發的模板引擎。模板引擎是為了使使用者介面與業務資料(內容)分離而產生的,它可以

SpringBoot整合其它熱門技術詳細總結

開心一笑 【坐在沙發上看電視,女友洗完澡出來蠻開心的問我,親愛的:為什麼洗完澡總覺得自己漂亮了好多?我問她:你洗澡的時候洗了頭吧?她說:是啊!我回答說:那肯定是腦子進水了…剛說完這句我就後悔了,看著女票的臉色,我想靜靜…】 提出問題 Spr

03-SpringBootWEB(一)——整合Freemarker與Thymeleaf

主要記錄 Spring Boot 與 Web 開發相關的知識,包括整合Freemarker 和 Thymeleaf。 1. 整合 Freemarker 1.1 新增Freemarker依賴 在pom.xml中新增Freemarker依賴,如下: <de

SpringBoot(二)-- 整合FreeMarker模板

pro span req -- oot -a log enc sta 1.pom依賴 <!-- 引入freeMarker的依賴包. --> <dependency> <groupId>org.sp

SpringBoot旅 -- 定時任務兩種(Spring Schedule 與 Quartz 整合 )實現

目的 config object cnblogs java title cor 進行 eat 相關文章 Spring Boot 相關文章目錄 前言 最近在項目中使用到定時任務,之前一直都是使用Quartz 來實現,最近看Spring 基礎發現其實Spring 提供

SpringBoot整合Freemarker+Mybatis

strong res esp ngs wire hid names 插件 sql 開發工具 , 開始 新建工程 .選擇Spring Initializr 下一步 下一步,選擇需要的組件 ..改一下工程名,Finish ..目錄結構 首先,修改po