天天看點

SpringBoot使用Freemarker的記錄SpringBoot使用Freemarker附錄

文章目錄

  • SpringBoot使用Freemarker
    • 添加依賴
    • 添加rapid-framework
    • 添加配置類
  • 附錄

SpringBoot使用Freemarker

遇到的問題

  • Failed at: @override name=“xxxx”
  • java.lang.ClassNotFoundException: org.apache.commons.lang.StringUtils

模版的使用避免了大量的重複代碼和邏輯,在此次項目中我使用Freemarker作為網頁模版工具,但是遇到了一些問題,特記錄下來以供參考。

添加依賴

首先需要添加Freemarker模版的jar包,在pom.xml中添加以下配置:

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

編寫添加模版,并運作,發現模版報錯Failed at: @override name="。

模版中的 @extends @block @override 三個宏均無法展開且報錯。

添加rapid-framework

于是按照網上的方法添加rapid-framework依賴:

<dependency>
    <groupId>com.googlecode.rapid-framework</groupId>
    <artifactId>rapid-core</artifactId>
    <version>4.0</version>
</dependency>
           

添加配置類

package com.xxx.xxx;

import cn.org.rapid_framework.freemarker.directive.BlockDirective;
import cn.org.rapid_framework.freemarker.directive.ExtendsDirective;
import cn.org.rapid_framework.freemarker.directive.OverrideDirective;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class FreemarkerConfiguration {
    @Autowired
    freemarker.template.Configuration configuration;
    @PostConstruct
    public void setSharedVariable(){
        configuration.setSharedVariable("block",new BlockDirective());
        configuration.setSharedVariable("override",new OverrideDirective());
        configuration.setSharedVariable("extends",new ExtendsDirective());
    }
}

           

再次運作程式卻報錯 java.lang.ClassNotFoundException: org.apache.commons.lang.StringUtils,根據提示添加commons-lang依舊報錯,網上其他人說可以用commons.lang3,依舊無法使用,幾經查找使用帶上版本資訊的方法可行:

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.4</version>
    <!-- 版本資訊解決導入包運作報錯問題java.lang.ClassNotFoundException: commons.lang.StringUtils -->
</dependency>
           

再次使用模版編輯并運作,發現模版可用。

附錄

<!-- 基礎模版 -->
<html>
	<head>
	    <meta charset="UTF-8">
	    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
	    <meta http-equiv="X-UA-Compatible" content="ie=edge">
	    <title>Carbon - Admin Template</title>
	    <link rel="stylesheet" href="./vendor/simple-line-icons/css/simple-line-icons.css">
	    <link rel="stylesheet" href="./vendor/font-awesome/css/fontawesome-all.min.css">
	    <link rel="stylesheet" href="./css/styles.css">
	</head>
	<body>
		<div>
			<@block name="pageContent">Page Content.</@block>
		</div>
		<script src="./vendor/jquery/jquery.min.js"></script>
		<script src="./vendor/popper.js/popper.min.js"></script>
		<script src="./vendor/bootstrap/js/bootstrap.min.js"></script>
		<script src="./vendor/chart.js/chart.min.js"></script>
		<script src="./js/carbon.js"></script>
		<script src="./js/demo.js"></script>
	</body>
</html>
           
<!-- 内容頁 -->
<@override name="pageContent">
    <h1>首頁</h1>
    <div>
        <label>231321fd33333333333d6757575ssfsf3213</label>
    </div>
</@override>
<@extends name="base.ftl"></@extends>
           

運作結果圖:

SpringBoot使用Freemarker的記錄SpringBoot使用Freemarker附錄

繼續閱讀