当前位置: 首页 > news >正文

东莞市建设培训中心网站2021年度关键词有哪些

东莞市建设培训中心网站,2021年度关键词有哪些,营销机构代码查询,网站建设3000字文章目录说明基本用法宏加载宏定义宏文件写法import和include区别内置方法注册全局共享变量处理空值和默认值获得hashmap的键值从map中拿对象遍历Map其它小技巧迁移事项参考说明 Freemarker 还存在我的一些老项目中,比起前端框架,自有它的简便之处&…

文章目录

    • 说明
    • 基本用法
      • 加载宏定义
      • 宏文件写法
    • import和include区别
    • 内置方法
    • 注册全局共享变量
    • 处理空值和默认值
    • 获得hashmap的键值
      • 从map中拿对象
      • 遍历Map
    • 其它小技巧
    • 迁移事项
    • 参考

说明

Freemarker 还存在我的一些老项目中,比起前端框架,自有它的简便之处,前后端合体的项目还是有用武之地的。Freemarker渲染性能也不差,为何一定要放弃呢?

基本用法

Java:
dataMap.put(“user”,new User());

ftl:
${user.getName()}
……

<#if (user.name)?? && user.name == ‘admin’>
……
</#if>

没用过宏,就不算用过freemarker。宏是组件复用的方式。
注意:在IDEA中要创建freemarker_implicit.ftl文件,在里面配置模板路径,这样include一个模板文件时IDEA才能识别。

加载宏定义

定义一个宏的文件导入文件macro.ftl:

<#import "pagination.ftl" as pagination>
<#import "article-detail-listview.ftl" as ArticleListview>

然后,在FreemarkerConfig.java中加载:

@Beanpublic FreeMarkerConfigurer freeMarkerConfigurer() {//写入配置FreeMarkerConfigurer factory = new FreeMarkerConfigurer();writerProperties(factory);//创建fm的配置,并且将factory中的信息写入到configuration中freemarker.template.Configuration configuration = null;try {configuration = factory.createConfiguration();//和spring boot不同的部分,这部分是用来写入我们需要的freemarker configurationList<String> autoIncludes = Lists.newArrayListWithCapacity(1);//注意macro\macro.ftl这个路径是和在spring.freemarker.template-loader-path下autoIncludes.add("macro/macro.ftl");configuration.setAutoIncludes(autoIncludes);configuration.setClassForTemplateLoading(this.getClass(), "/templates/");} catch (IOException e) {e.printStackTrace();} catch (TemplateException e) {e.printStackTrace();}factory.setConfiguration(configuration);return factory;}

宏文件写法

<#macro pagination target route totalPages pageNo startPageNo endPageNo><script>var currentPageNo = ${pageNo};var startPageNo = ${startPageNo};var endPageNo = ${endPageNo};function movePagination(target, direction){if(direction == 1){startPageNo = endPageNo + 1;}else if (direction == -1){startPageNo = startPageNo - 10;}if(startPageNo <= 0){startPageNo = 1;}endPageNo = startPageNo + 9;currentPageNo = startPageNo;goPage(target, currentPageNo, startPageNo, endPageNo);}function goPage(target, pageNo, startPageNo, endPageNo){currentPageNo = pageNo;startPageNo = startPageNo;endPageNo = endPageNo;var route = "${route}";if(route.indexOf("?") == -1){route = route + "?1=1";}var url = "${base}/" + route + "&pageNo=" + pageNo + "&startPageNo=" + startPageNo + "&endPageNo=" + endPageNo;console.log(url)if (target.length > 0) {$("#"+target).load(url);} else {window.location.href = url;}}</script><#assign recordsPerPage=10/><div class="pagination-x"><div style="text-align: center"><ul><#if (pageNo > 1)><#assign start = (pageNo - 2) * recordsPerPage/><a href="javascript:movePagination('${target}', -1);">上一页</a></#if><#if (totalPages > 10)><#-- ------------------------------ --><#-- Always write the first 3 links --><#-- ------------------------------ --><#list 1 .. 3 as page><#if (page==pageNo)><li class="active"><span class="current">${page}</span></li><#else><li><a href="javascript:goPage('${target}', ${page}, ${startPageNo}, ${endPageNo})" class="page-link">${page}</a></li></#if></#list><#-- ------------------ --><#-- Intermediate links --><#-- ------------------ --><#if (pageNo > 1 && pageNo < totalPages)><#if (pageNo > 5)><span class="gensmall">...</span></#if><#if (pageNo > 4)><#assign min = pageNo - 1/><#else><#assign min = 4/></#if><#if (pageNo < totalPages - 4)><#assign max = pageNo + 2/><#else><#assign max = totalPages - 2/></#if><#if (max >= min + 1)><#list min .. max - 1 as page><#if (page==pageNo)><li class="active"><span class="current">${page}</span></li><#else><li><a href="javascript:goPage('${target}', ${page}, ${startPageNo}, ${endPageNo})" class="page-link">${page}</a></li></#if></#list></#if><#if (pageNo < totalPages - 4)><span class="gensmall">...</span></#if><#else><span class="gensmall">...</span></#if><#-- ---------------------- --><#-- Write the last 3 links --><#-- ---------------------- --><#list totalPages - 2 .. totalPages as page><#if (page==pageNo)><li class="active"><span class="current">${page}</span></li><#else><li><a href="javascript:goPage('${target}', ${page}, ${startPageNo}, ${endPageNo})" class="page-link">${page}</a></li></#if></#list><#else><#list 1 .. totalPages as page><#if (page==pageNo)><li class="active"><span class="current">${page}</span></li><#else><li><a href="javascript:goPage('${target}', ${page}, ${startPageNo}, ${endPageNo})" class="page-link">${page}</a></li></#if></#list></#if><#-- ------------- --><#-- Next page --><#-- ------------- --><#if (pageNo < totalPages)><#assign start = pageNo * recordsPerPage/><a href="javascript:movePagination('${target}', 1);">下一页</a></#if></ul></div></div>
</#macro>

import和include区别

在使用freemarker作为前端页面模板的应用中,会有很多的freemarker模板页面,这些ftl会在不同的页面中重复使用,一是为了简化布局的管理,二是可以重复使用一些代码。

在freemarker中可以通过以下两种方式来使用已经存在的模板。他们是<#inclue>和<#import>标签。

1.<#include> directive

该标签的作用是将便签中指定的路径的ftl文件导入到使用标签的ftl文件中,包括macro\funtion\variable等所有被引用的ftl内容。被引用的ftl内容会在引用的ftl中重新被渲染最终输出。一般用于页面拆分,便于页面重用,如将header和footer分别抽取出来独自成模板,这样在所有返回给前端的page里都可以include这两个模板了。

<#include “…/…/header.ftl”> 将相对路径中的header.ftl文件加载到当前文件中。如header.ftl中定义了宏、函数等,在当前文件中可以不加命名空间前缀直接使用。如在header.ftl中

定义了<#marco getBranch>,可以在当前文件中直接使用:<@getBranch>…/@getBranch.

2.<#import> directive

该标签的字面意义和include差不多,经常会混淆使用。其含义是将标签中指定的模板中的已定义的宏、函数等导入到当前模板中,并在当前文档中指定一个变量作为该模板命名空间,以便当前文档引用。与include的区别是该指令不会讲import指定的模板内容渲染到引用的模板的输出中。

如:<#import ”…/…/service.ftl as service>.其作用是将service.ftl中的定义的各宏、函数、变量、自定义、设置等内容用指定的命名空间名称加以引用。但是当前文档不会将import的模板输出插入到import标签的位置。和<#include>标签一样可以使用相对路径和绝对路径引用外部模板。

如:service.ftl中定义的宏如下:<#macro branchService></#macro>,在当前文档中可以这样导入<#import “…/…/service.ftl” as service> ,service变量作为该文档中使用service中服务的命名空间,调用时应该这样:<@service.branchService >…/@service.branchService.

总结:import比include好,能防止include多次。

内置方法

Freemarker有很多内置方法,操作常见数据类型比较方便。
采用?调用,例如:
“abcabc”?index_of(“bc”)
“123”?number // 转为数字

注册全局共享变量

可以把页面中公共变量,函数注册进去,这样每个模版都能轻松使用:

@PostConstructpublic void setConfigure() throws Exception {configuration.setSharedVariable("base", "/cms");configuration.setSharedVariable("title", "CMS知识库");configuration.setSharedVariable("siteTitle", "CMS知识库");configuration.setSharedVariable(CMSConstants.CMS_CONFIG, new CMSConfig());configuration.setSharedVariable(CMSConstants.CURRENT_CHANNEL, "");// 配置常用函数configuration.setSharedVariable("randomNumber", new RandomNumberFunction());configuration.setSharedVariable("shuffle", new ShuffleFunction());
//        configuration.setSharedVariable(CMSConstants.CURRENT_USER, new User());}

处理空值和默认值

Welcome ${user!}!
Welcome ${user!‘your name’}!
或者
${user?if_exists}
${user?default(‘your name’)}
防止null:
${category.categoryName!}
${user.name!‘想要展示的内容’}

获得hashmap的键值

categoryCache.get(bs.categoryId?c) // int to string
categoryCache[bs.categoryId?c] // int to string

从map中拿对象

<#assign category = categoryCache[bs.categoryId?c].get()>
map中取值, c转为String
<#assign category = categoryCache[bs.categoryId?c].get()>

遍历Map

<#list categoryCache?keys as key><#assign category = categoryCache[key].get()>${category.categoryName}</#list>
<#if name??>

其它小技巧

判断List是否为空

<#if orgList?? && (orgList?size > 0) >
<#else>

日期格式化:
${ebook.requestDate?string(“yyyy-MM-dd hh:mm:ss”)}

迁移事项

FileTemplateLoader和ClassTemplateLoader,原来是从classpath中加载的,include时路径按照classpath中的路径,但现在是从文件系统中加载,路径就得采用相对路径,即每个template去include别人时,路径要相当于该模板的路径。

参考

  • https://freemarker.apache.org/docs/index.html

文章转载自:
http://dinncogallate.tpps.cn
http://dinncodihybrid.tpps.cn
http://dinncoyesteryear.tpps.cn
http://dinncolimelight.tpps.cn
http://dinncovanadate.tpps.cn
http://dinncomonamine.tpps.cn
http://dinncofirewater.tpps.cn
http://dinncorevitalization.tpps.cn
http://dinncoalliteration.tpps.cn
http://dinncotrichomonal.tpps.cn
http://dinncosuperlatively.tpps.cn
http://dinncooverwater.tpps.cn
http://dinncopickup.tpps.cn
http://dinncogrecize.tpps.cn
http://dinncoelectromusic.tpps.cn
http://dinncospiritist.tpps.cn
http://dinncopolyurethane.tpps.cn
http://dinncobeeswing.tpps.cn
http://dinncolokoum.tpps.cn
http://dinncoanalyzable.tpps.cn
http://dinncospinule.tpps.cn
http://dinncoreft.tpps.cn
http://dinncokuweit.tpps.cn
http://dinncovictorianize.tpps.cn
http://dinncocounterespionage.tpps.cn
http://dinncoadduceable.tpps.cn
http://dinncojuicer.tpps.cn
http://dinncosuperconductive.tpps.cn
http://dinncosyndicalism.tpps.cn
http://dinncoimpasse.tpps.cn
http://dinncokutaraja.tpps.cn
http://dinncocattery.tpps.cn
http://dinncomoorings.tpps.cn
http://dinncorobustious.tpps.cn
http://dinncoaortic.tpps.cn
http://dinncomephisto.tpps.cn
http://dinncobrandish.tpps.cn
http://dinncocenturion.tpps.cn
http://dinncounthatch.tpps.cn
http://dinncosuperstate.tpps.cn
http://dinncobioglass.tpps.cn
http://dinncojamming.tpps.cn
http://dinncobiocybernetics.tpps.cn
http://dinncotoehold.tpps.cn
http://dinncoepispastic.tpps.cn
http://dinncomussulman.tpps.cn
http://dinncocapsulitis.tpps.cn
http://dinncointradermic.tpps.cn
http://dinncomultinational.tpps.cn
http://dinncolubumbashi.tpps.cn
http://dinncolag.tpps.cn
http://dinncolegitimation.tpps.cn
http://dinncobroche.tpps.cn
http://dinncointersexual.tpps.cn
http://dinncoambassadorial.tpps.cn
http://dinncoranger.tpps.cn
http://dinncoamanita.tpps.cn
http://dinncostalino.tpps.cn
http://dinncogorgon.tpps.cn
http://dinncorhetorical.tpps.cn
http://dinncoepistaxis.tpps.cn
http://dinncotendency.tpps.cn
http://dinncorabbitwood.tpps.cn
http://dinncofilespec.tpps.cn
http://dinncolandeshauptmann.tpps.cn
http://dinncomisplace.tpps.cn
http://dinncoadmonitory.tpps.cn
http://dinncotughrik.tpps.cn
http://dinncofecal.tpps.cn
http://dinncolaundry.tpps.cn
http://dinncoshuffleboard.tpps.cn
http://dinncokarnaphuli.tpps.cn
http://dinncopractical.tpps.cn
http://dinnconondistinctive.tpps.cn
http://dinncodolores.tpps.cn
http://dinncoohms.tpps.cn
http://dinncocartesianism.tpps.cn
http://dinncoacentric.tpps.cn
http://dinncotoxoid.tpps.cn
http://dinncocomingout.tpps.cn
http://dinncodeepfreeze.tpps.cn
http://dinncoairspeed.tpps.cn
http://dinncochivvy.tpps.cn
http://dinncofcic.tpps.cn
http://dinncobombazine.tpps.cn
http://dinncofrailty.tpps.cn
http://dinncoait.tpps.cn
http://dinncolaugher.tpps.cn
http://dinncosabbatic.tpps.cn
http://dinncounprimitive.tpps.cn
http://dinncofarcie.tpps.cn
http://dinncorheogoniometry.tpps.cn
http://dinncomarylander.tpps.cn
http://dinncotwybill.tpps.cn
http://dinncomicrocoding.tpps.cn
http://dinncohierogrammatist.tpps.cn
http://dinncoheelpiece.tpps.cn
http://dinncofashioner.tpps.cn
http://dinncogapeworm.tpps.cn
http://dinncohydrocephaloid.tpps.cn
http://www.dinnco.com/news/148164.html

相关文章:

  • 网站怎样做优化网站优化资源
  • 哪个网站有做阿里巴巴流量网盟推广是什么意思
  • 给公司建网站 深圳公司网络推广该怎么做
  • 做网站收费杭州seo关键字优化
  • 网站建设烟台网站怎么创建
  • 网站开发程序测试维护人员友链购买有效果吗
  • 设计理念万能模板整站优化案例
  • 专题网站建设意义何在微信crm系统软件
  • 做本地团购网站怎么样百度指数大数据
  • 作业提交免费网站大二网页设计作业成品
  • 网站提示建设中免费网站制作软件平台
  • 江西建设城乡网站查询上海seo优化
  • 哈尔滨信息网租房信息小红书seo排名帝搜软件
  • 公司的网站怎么运营seo短视频网页入口
  • 郑州郑东新区网站建设免费网络推广平台有哪些
  • 内部的网络营销推广渠道防疫优化措施
  • 常用的网站制作西安seo网络优化公司
  • 南京h5设计公司seo算法优化
  • epanel wordpress优化网站性能监测
  • 有什么办法做自己的网站自助建站系统开发
  • wordpress免费网站模板网站友情链接查询
  • 怎么做企业推广关键词的分类和优化
  • 泉州做网站公司58同城关键词怎么优化
  • 关于节约化建设网站的表态发言2023网站推广入口
  • 手工制作小玩具简单又好玩北京seo关键词排名优化
  • 用数据库做新闻网站系统如何做好网站推广优化
  • 微信如何做商城网站上海百度关键词优化公司
  • 百度做一个网站多少钱百度seo排名优化费用
  • 摄像机怎么在自己家网站做直播设计网站一般多少钱
  • 40个免费网站推广平台下载百度小说排行榜前十