SpringBoot整合Freemarker的基本步驟
添加pom依賴
<!-- springboot整合freemarker --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
在application.yml中添加相關(guān)配置
# 配置freemarker spring: freemarker: # 設(shè)置模板后綴名 suffix: .ftl # 設(shè)置文檔類型 content-type: text/html # 設(shè)置頁面編碼格式 charset: UTF-8 # 設(shè)置頁面緩存 cache: false # 設(shè)置ftl文件路徑 template-loader-path: - classpath:/templates # 設(shè)置靜態(tài)文件路徑,js,css等 mvc: static-path-pattern: /static/**
創(chuàng)建freemarker模板
目錄:src/main/resources 創(chuàng)建templates文件夾,文件夾里新建freemarker.ftl文件
<!DOCTYPE> <html> <head> <title>freemark</title> </head> <body> <h1>Hello ${name} from resource freemark!</h1> </body> </html>
創(chuàng)建控制層
package com.ahut.action; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * * @ClassName: FreemarkerAction * @Description: freemarker控制層 * @author cheng * @date 2018年1月22日 下午8:19:39 */ @Controller @RequestMapping(value = "/freemarker") public class FreemarkerAction { /** * 日志管理 */ private static Logger log = LoggerFactory.getLogger(FreemarkerAction.class); * * @Title: toDemo * @Description: 跳轉(zhuǎn)freemarker頁面 * @param mv * @return @RequestMapping(value = "/toDemo") public ModelAndView toDemo(ModelAndView mv) { log.info("====>>跳轉(zhuǎn)freemarker頁面"); mv.addObject("name", "jack"); mv.setViewName("freemarker"); return mv; } }
測試訪問
啟動(dòng)項(xiàng)目,輸入http://localhost:8080/freemarker/toDemo,看到以下界面
Freemarker獲取項(xiàng)目根路經(jīng)
application.properties
spring.freemarker.request-context-attribute=request
ftl
<#assign base=request.contextPath /> <!DOCTYPE html> <html lang="zh"> <head> <base id="base" href="${base}" rel="external nofollow" > <title>首頁</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="${base}/static/bootstrap-3.3.4/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet"> <script src="${base}/static/bootstrap-3.3.4/js/bootstrap.min.js"></script>
js
var base = document.getElementById("base").href; // 與后臺(tái)交互 _send = function(async, url, value, success, error) { $.ajax({ async : async, url : base + '/' + url, contentType : "application/x-www-form-urlencoded; charset=utf-8", data : value, dataType : 'json', type : 'post', success : function(data) { success(data); }, error : function(data) { error(data); } }); };
Springboot配置靜態(tài)資源
package com.ahut.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * @author cheng * @className: WebConfig * @description: 靜態(tài)資源配置類 * @dateTime 2018/4/19 17:59 */ @Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { /** * 日志管理 */ private Logger log = LoggerFactory.getLogger(WebConfig.class); * @description: * @author cheng * @dateTime 2018/4/19 17:59 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { log.info("配置靜態(tài)資源所在目錄"); // 和頁面有關(guān)的靜態(tài)目錄都放在項(xiàng)目的static目錄下 registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); } }
Freemarker頁面引用靜態(tài)資源(CSS、JS)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>SpringBoot - 登錄</title> <meta name="keywords" content="springboot"> <meta name="description" content="SpringBoot"> <link rel="shortcut icon" href="favicon.ico" rel="external nofollow" > <link href="/static/css/bootstrap.min.css?v=3.3.6" rel="external nofollow" rel="stylesheet" type="text/css"> <link href="/static/css/font-awesome.css?v=4.4.0" rel="external nofollow" rel="stylesheet"> <!-- Sweet Alert --> <link href="/static/css/plugins/sweetalert/sweetalert.css" rel="external nofollow" rel="stylesheet"> <link href="/static/css/animate.css" rel="external nofollow" rel="stylesheet"> <link href="/static/css/style.css?v=4.1.0" rel="external nofollow" rel="stylesheet"> <!--[if lt IE 9]> <meta http-equiv="refresh" content="0;ie.html"/> <![endif]--> <script>if (window.top !== window.self) { window.top.location = window.location; }</script> </head> <body class="gray-bg"> <div class="middle-box text-center loginscreen animated fadeInDown"> <div> <div> <h1 class="logo-name">Spring</h1> </div> <h3>歡迎使用 SpringBoot</h3> <div class="form-group"> <input type="text" id="userAccount" class="form-control" placeholder="用戶名" required=""> <input type="password" id="userPassword" class="form-control" placeholder="密碼" required=""> <button class="btn btn-primary block full-width m-b" onclick="login()">登 錄</button> <p class="text-muted text-center"><a href="login.ftl#" rel="external nofollow" > <small>忘記密碼了?</small> </a> | <a href="register.html" rel="external nofollow" >注冊一個(gè)新賬號(hào)</a> </p> </div> </div> <!-- 全局js --> <script src="/static/js/jquery.min.js?v=2.1.4"></script> <script src="/static/js/bootstrap.min.js?v=3.3.6"></script> <!-- Sweet alert --> <script src="/static/js/plugins/sweetalert/sweetalert.min.js"></script> <script> // 登錄 function login() { var userAccount = $("#userAccount").val(); var userPassword = $("#userPassword").val(); if (userAccount == "") { return false; } if (userPassword == "") { // 登錄 $.ajax({ url: "/v1/login", type: "GET", data: { userAccount: userAccount, userPassword: userPassword }, success: function (data) { if ("SUCCESS" == data.type) { // 成功 swal({ title: "登錄成功", timer: 1000, type: "success", showConfirmButton: false }); } else if ("FAIL" == data.type) { // 失敗 title: data.msg, type: "error", } } }) } </script> </body> </html>
到此這篇關(guān)于SpringBoot整合Freemarker的基本步驟的文章就介紹到這了,更多相關(guān)SpringBoot整合Freemarker內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot整合Freemarker的實(shí)現(xiàn)詳細(xì)過程
- springboot整合freemarker代碼自動(dòng)生成器
- Springboot整合freemarker和相應(yīng)的語法詳解
- 如何在SpringBoot+Freemarker中獲取項(xiàng)目根目錄
- Springboot整合FreeMarker的實(shí)現(xiàn)示例
- springboot整合freemarker的踩坑及解決
- SpringBoot整合freemarker實(shí)現(xiàn)代碼生成器
- SpringBoot整合FreeMarker的過程詳解
- SpringBoot結(jié)合FreeMarker視圖渲染的實(shí)現(xiàn)
相關(guān)文章
基于javassist進(jìn)行動(dòng)態(tài)編程過程解析
這篇文章主要介紹了基于javassist進(jìn)行動(dòng)態(tài)編程過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Redis Java Lettuce驅(qū)動(dòng)框架原理解析
這篇文章主要介紹了Redis Java Lettuce驅(qū)動(dòng)框架原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12Required?request?body?is?missing的問題及解決
這篇文章主要介紹了Required?request?body?is?missing的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Java中實(shí)現(xiàn)多線程關(guān)鍵詞整理(總結(jié))
這篇文章主要介紹了Java中實(shí)現(xiàn)多線程關(guān)鍵詞整理,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05java byte數(shù)組與16進(jìn)制間相互轉(zhuǎn)換的示例
這篇文章主要介紹了java byte數(shù)組與16進(jìn)制間相互轉(zhuǎn)換的示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-10-10Springboot WebFlux集成Spring Security實(shí)現(xiàn)JWT認(rèn)證的示例
這篇文章主要介紹了Springboot WebFlux集成Spring Security實(shí)現(xiàn)JWT認(rèn)證的示例,幫助大家更好的理解和學(xué)習(xí)使用springboot框架,感興趣的朋友可以了解下2021-04-04spring cloud如何修復(fù)zuul跨域配置異常的問題
最近的開發(fā)過程中,使用spring集成了spring-cloud-zuul,在配置zuul跨域的時(shí)候遇到了問題,下面這篇文章主要給大家介紹了關(guān)于spring cloud如何修復(fù)zuul跨域配置異常的問題,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09