摘要:時(shí)間年月日星期日說明本文部分內(nèi)容均來自慕課網(wǎng)。整體目錄結(jié)構(gòu)如下項(xiàng)目文件用于數(shù)據(jù)持久化配置項(xiàng)目配置配置視圖解析器配置靜態(tài)資源映射配置配置配置自定義指令配置解析器配置,類似于項(xiàng)目啟動類新建,注冊配置類,并將其和當(dāng)前關(guān)聯(lián)。
時(shí)間:2017年3月19日星期日
說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com
教學(xué)示例源碼:無
個人學(xué)習(xí)源碼:https://github.com/zccodere/s...
本節(jié)要點(diǎn)
什么是Freemarker 數(shù)據(jù)模型+模版=輸出(HTML) 前端設(shè)計(jì)師和程序員的學(xué)習(xí)側(cè)重點(diǎn)
什么是Freemarker
Freemarker是一款模版引擎 Freemarker不是web框架 官網(wǎng):http:// freemarker.org
Freemarker原理
前端設(shè)計(jì)師和程序員的學(xué)習(xí)側(cè)重點(diǎn)
MVC設(shè)計(jì)(Model、View、Controller) 前端設(shè)計(jì)師側(cè)重于View(模版設(shè)計(jì)) 程序員全面掌握MVC
如何開始?
一點(diǎn)心得
先劃一個范圍 再定一個目標(biāo) 創(chuàng)建可行計(jì)劃 邊玩邊學(xué)1-2 maven構(gòu)建freemarker項(xiàng)目
本節(jié)要點(diǎn)
Maven構(gòu)建Spring+Freemarker項(xiàng)目 配置文件介紹 運(yùn)行小例:列表展示
Maven構(gòu)建Spring+Freemarker項(xiàng)目
Eclipse+Maven使用簡介 Maven依賴Spring和Freemarker的jar包 Spring配置文件和Freemarker Servlet配置文件
配置文件介紹
Spring配置文件applicationContext.xml Spring Freemarker Servlet配置文件spring-servlet.xml1-3 maven構(gòu)建freemarker項(xiàng)目代碼實(shí)戰(zhàn)
我學(xué)習(xí)時(shí),使用springboot來進(jìn)行項(xiàng)目搭建,同時(shí),項(xiàng)目基于javaconfig進(jìn)行配置。
整體目錄結(jié)構(gòu)如下:
項(xiàng)目POM文件
4.0.0 com.zccoder myfreemarker 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.5.1.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-freemarker org.springframework.boot spring-boot-configuration-processor true com.alibaba fastjson 1.2.28 org.springframework.boot spring-boot-maven-plugin
配置Freemarker
package com.myimooc.myfreemarker.config; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerView; import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import com.myimooc.myfreemarker.web.controller.RoleDirectiveModel; /** * Web項(xiàng)目SpringMvc配置 * @author ZhangCheng * @date 2017-03-19 * @version V1.0 */ @Configuration @EnableWebMvc @ComponentScan("com.myimooc.myfreemarker") public class SpringMvcConfig extends WebMvcConfigurerAdapter{ /** * 配置視圖解析器 * @return */ @Bean public FreeMarkerViewResolver getFreeMarkerViewResolver(){ FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver(); freeMarkerViewResolver.setOrder(1); freeMarkerViewResolver.setSuffix(".html"); freeMarkerViewResolver.setCache(false); freeMarkerViewResolver.setRequestContextAttribute("request"); freeMarkerViewResolver.setContentType("text/html;charset=utf-8"); freeMarkerViewResolver.setViewClass(FreeMarkerView.class); return freeMarkerViewResolver; } /** * 配置靜態(tài)資源映射 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); } /** * 配置FASTJSON * @return */ @Bean public FastJsonHttpMessageConverter fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames); fastJsonConfig.setCharset(Charset.forName("UTF-8")); fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); ListsupportedMediaTypes = new ArrayList (); supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(supportedMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); return fastConverter; } /** * 配置FreeMarker * @return */ @Bean public FreeMarkerConfigurer getFreeMarkerConfigurer(){ FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); freeMarkerConfigurer.setDefaultEncoding("UTF-8"); freeMarkerConfigurer.setTemplateLoaderPath("classpath:/templates/"); Properties settings = new Properties(); settings.setProperty("template_update_delay", "5"); settings.setProperty("url_escaping_charset", "UTF-8"); settings.setProperty("defaultEncoding", "UTF-8"); settings.setProperty("whitespace_stripping", "true"); settings.setProperty("boolean_format", "true,false"); settings.setProperty("number_format", "0.##########"); settings.setProperty("locale", "zh_CN"); settings.setProperty("datetime_format", "yyyy-MM-dd HH:mm:ss"); settings.setProperty("date_format", "yyyy-MM-dd"); settings.setProperty("time_format", "HH:mm:ss"); settings.setProperty("tag_syntax", "square_bracket"); settings.setProperty("classic_compatible", "true"); settings.setProperty("template_exception_handler", "ignore"); settings.setProperty("auto_import", "/spring.ftl as spring, /common/spring.ftl as spring"); freeMarkerConfigurer.setFreemarkerSettings(settings); // 配置自定義指令 Map variables = new HashMap (); variables.put("role", new RoleDirectiveModel()); freeMarkerConfigurer.setFreemarkerVariables(variables); return freeMarkerConfigurer; } /** * 配置JSON解析器 */ @Override public void configureMessageConverters(List > converters) { super.configureMessageConverters(converters); converters.add(this.fastJsonHttpMessageConverters()); } }
配置Web,類似于web.xml
package com.myimooc.myfreemarker.config; import java.util.EnumSet; import javax.servlet.DispatcherType; import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import org.springframework.context.annotation.Configuration; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.DispatcherServlet; /** * Web項(xiàng)目啟動類 * * @author ZhangCheng * @date 2017-03-19 * @version V1.0 * */ @Configuration public class WebConfig implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(SpringMvcConfig.class); // 新建WebApplication,注冊配置類,并將其和當(dāng)前servletContext關(guān)聯(lián)。 context.setServletContext(servletContext); // 注冊SpringMVC的DispatcherServlet。 ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); // 注冊SpringMVC的字符過濾器 FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encoding", new CharacterEncodingFilter()); EnumSetdispatcherTypes = EnumSet.allOf(DispatcherType.class); dispatcherTypes.add(DispatcherType.REQUEST); dispatcherTypes.add(DispatcherType.FORWARD); encodingFilter.addMappingForUrlPatterns(dispatcherTypes, true, "*"); encodingFilter.setInitParameter("encoding", "UTF-8"); } }
新建模版頁面
編寫控制器
啟動項(xiàng)目
1-4 小例子:列表demo展示編寫控制器
編寫Freemarker
Insert title here ${username}
效果如下:
1-5 補(bǔ)充:springboot集成freemarker輸入網(wǎng)址:start.spring.io
第二章:基礎(chǔ)技能 2-1 freemarker取值章節(jié)簡介本節(jié)要點(diǎn)
Java中常用的數(shù)據(jù)模型 取值(插值)指令 邏輯指令:if、switch2-2 Java數(shù)據(jù)模型、freemarker取值
Java中常用的數(shù)據(jù)模型
基本類型數(shù)據(jù)(比如Integer) 封裝的對象類型(比如User對象) 集合類型:List、Map
取值指令
常用${var}語法進(jìn)行取值 對null、不存在對象取值${var!} 取包裝對象的值,通過“點(diǎn)”語法:${User.name} 取值的時(shí)候進(jìn)行計(jì)算、賦值 Date類型格式${date?string(‘yyyy-MM-dd’)} 如何轉(zhuǎn)義HTML內(nèi)容:${var?html}2-3 freemarker取java基本數(shù)據(jù)模型的值
控制器
頁面層
效果圖
2-4 boolean類型值的format布爾值:${booleanVar?string("yes","no")}
2-5 date類型值的format日期:${dateVar?string("yyyy-MM-dd")}
2-6 null或者不存在的變量取值null:${nullVar!"我是默認(rèn)值"}
missing:${ssssVar!"我是默認(rèn)值"}
編寫代碼:
2. 賦值運(yùn)算
效果如下:
3-2 自定義對象User變量的取值 3-3 集合List的遍歷語法:
[#list listName as item] ${item!}3-4 集合Map的遍歷
[/#list]
語法:
[#list map?keys as key] ${key}:${map[key]!}3-5 if語法
[/#list]
語法:
判斷某個對象或值是否存在
[#if myList?exists] [/#if] 或 [#if myList??] [/#if]3-6 switch語法
語法:
本節(jié)要點(diǎn)
字符串、集合操作 自定義函數(shù) 自定義指令
補(bǔ)充表達(dá)式指令
+ :字符串連接,集合連接 [index]:下標(biāo)取值
自定義函數(shù)
自定義排序函數(shù) 實(shí)現(xiàn)TemplateMethodModelEx接口
自定義指令
實(shí)現(xiàn)TemplateDirectiveModel接口4-2 string基本操作指令
代碼:
6. 字符操作
效果圖:
4-3 自定義函數(shù)步驟一:編寫自定義函數(shù)類
步驟二:再返回的控制器里面,添加自定義函數(shù)類,并指定方法名
步驟三:在頁面使用排序方法
步驟四:驗(yàn)證輸出
4-4 list排序內(nèi)建函數(shù)、常用指令使用內(nèi)建函數(shù)進(jìn)行排序,item_index為下標(biāo),默認(rèn)為升序
[#list mylistinfo?sort as item] ${item_index}:${item}, [/#list]
使用內(nèi)建函數(shù)進(jìn)行排序,降序
[#list mylistinfo?sort?reverse as item] ${item_index}:${item}, [/#list]
其它常用內(nèi)建函數(shù)
效果圖:
4-5 自定義指令步驟一:編寫自定義指令類
步驟二:注冊自定義指令類
步驟三:頁面使用自定義指令
步驟四:驗(yàn)證輸出
4-6 freemarker常用內(nèi)建函數(shù)本節(jié)要點(diǎn)
處理字符串內(nèi)建函數(shù) 處理數(shù)字的內(nèi)建函數(shù) 處理list的內(nèi)建函數(shù) 其他內(nèi)建函數(shù)
處理字符串內(nèi)建函數(shù)
substring、cap_first、ends_with、contains date、datetime、time starts_with、index_of、last_index_of、split、trim
處理數(shù)字的內(nèi)建函數(shù)
string、x?string(“0.##”) round、floor、ceiling
梳理List的內(nèi)建函數(shù)
first、last、seq_contains、seq_index_of size、reverse、sort、sort_by chunk
其他內(nèi)建函數(shù)
is函數(shù):is_string、is_number、is_method ()、has_content函數(shù) eval求值4-7 freemarker內(nèi)建函數(shù)代碼講解
代碼示例
效果圖如下:
4-8 macro、function指令本節(jié)要點(diǎn)
宏macro、nested、return指令 函數(shù)function、return指令 課程總結(jié)
macro、nested、return
macro語法 [@macro_name param /] 調(diào)用macro nested語法
function、return
function語法 ${function_name(param)}調(diào)用
macro、nested、return章節(jié)
macro語法:
[#macro macro_name param1 param2 param3 paramN] Trmplate_code ${param1} [#nested/] [/#macro]
調(diào)用
[@macro_name param1=”value1” param2=”value2”/] [@macro_name param1=”value1” param2=”value2”/] Nested_template [@macro_name/]
Function語法:
[#function function_name param1 param2] [#return param1 + param2] [/#function]
調(diào)用
${doAdd(100,100)}
代碼示例
效果如下
第五章:課程總結(jié) 5-1 課程總結(jié)文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/66856.html
摘要:時(shí)間年月日星期四說明本文部分內(nèi)容均來自慕課網(wǎng)。哈希表實(shí)現(xiàn)命令,將哈希表中的域的值設(shè)為實(shí)現(xiàn)命令,返回哈希表中給定域的值實(shí)現(xiàn)命令,刪除哈希表中的一個或多個指定域,不存在的域?qū)⒈缓雎?。?shí)現(xiàn)命令,返回哈希表中,所有的域和值。 時(shí)間:2018年04月19日星期四說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):https://www.imooc.com教學(xué)源碼:https://github.com/zc...
時(shí)間:2017年07月09日星期日說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:無學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:概述 1-1 課程概述 主要內(nèi)容 驗(yàn)證碼歷史 課程內(nèi)容 不同方案對比 設(shè)計(jì)與實(shí)現(xiàn) 總結(jié) 1-2 驗(yàn)證碼歷史 驗(yàn)證碼歷史 無驗(yàn)證碼:垃圾騷擾 Luis von Ahn:Captcha 不斷...
摘要:時(shí)間年月日星期六說明本文部分內(nèi)容均來自慕課網(wǎng)。可以更加專注于業(yè)務(wù)邏輯開發(fā),縮短項(xiàng)目開發(fā)周期,提高項(xiàng)目開發(fā)速度。 時(shí)間:2017年07月15日星期六說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:無學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:課程介紹 1-1 課程介紹 在用戶進(jìn)行信息概略瀏覽的時(shí)候,提供縮...
摘要:時(shí)間年月日星期五說明本文部分內(nèi)容均來自慕課網(wǎng)。慕課網(wǎng)教學(xué)源碼無學(xué)習(xí)源碼第一章課程簡介引言通過一個項(xiàng)目案例的講解,如何在應(yīng)用中實(shí)現(xiàn)圖片水印的添加。 時(shí)間:2017年07月21日星期五說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:無學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:課程簡介 1-1 引言 通過一...
摘要:到目前為止,使用越來越廣泛,不光光只是它強(qiáng)大的生成技術(shù),而且它能夠與進(jìn)行很好的集成。注意使用數(shù)字范圍來定義集合時(shí)無需使用方括號數(shù)字范圍也支持反遞增的數(shù)字范圍如對象對象使用花括號包括中的對之間以英文冒號分隔,多組對之間以英文逗號分隔。 Freemarker的介紹 ??Freemarker 是一款模板引擎,是一種基于模版生成靜態(tài)文件的通用 工具,它是為程序員提供的一個開發(fā)包,或者說是一個類...
閱讀 4066·2021-11-18 13:22
閱讀 1860·2021-11-17 09:33
閱讀 2902·2021-09-26 09:46
閱讀 1237·2021-08-21 14:11
閱讀 2911·2019-08-30 15:53
閱讀 2731·2019-08-30 15:52
閱讀 1959·2019-08-30 10:52
閱讀 1542·2019-08-29 15:30