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

东莞市建设培训中心网站网络营销工具及其特点

东莞市建设培训中心网站,网络营销工具及其特点,教材jsp动态网站开发,北京建设网办事大厅文章目录说明基本用法宏加载宏定义宏文件写法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://dinncoalcyonarian.ssfq.cn
http://dinncoimburse.ssfq.cn
http://dinncocongregationalist.ssfq.cn
http://dinncolockpin.ssfq.cn
http://dinncosleugh.ssfq.cn
http://dinncoconformational.ssfq.cn
http://dinncosinanthropus.ssfq.cn
http://dinncophelloderm.ssfq.cn
http://dinncospacemark.ssfq.cn
http://dinncodoubleheader.ssfq.cn
http://dinncoovenproof.ssfq.cn
http://dinncoconceited.ssfq.cn
http://dinncoacclivity.ssfq.cn
http://dinncopherentasin.ssfq.cn
http://dinncocivilizable.ssfq.cn
http://dinncoaequum.ssfq.cn
http://dinncobibulous.ssfq.cn
http://dinncodint.ssfq.cn
http://dinncotomback.ssfq.cn
http://dinncomephitic.ssfq.cn
http://dinncofancify.ssfq.cn
http://dinncoimminently.ssfq.cn
http://dinncowystan.ssfq.cn
http://dinncomulticast.ssfq.cn
http://dinncolamination.ssfq.cn
http://dinncorideress.ssfq.cn
http://dinncoincompetence.ssfq.cn
http://dinncokabele.ssfq.cn
http://dinnconanjing.ssfq.cn
http://dinncosnickersnee.ssfq.cn
http://dinncoguck.ssfq.cn
http://dinncoparanasal.ssfq.cn
http://dinncocircumambiency.ssfq.cn
http://dinncogolden.ssfq.cn
http://dinncoturkoman.ssfq.cn
http://dinncobailie.ssfq.cn
http://dinncorenomination.ssfq.cn
http://dinncocanaan.ssfq.cn
http://dinncoifpi.ssfq.cn
http://dinncooverintricate.ssfq.cn
http://dinncobauxite.ssfq.cn
http://dinncooerlikon.ssfq.cn
http://dinncoinsectifuge.ssfq.cn
http://dinncotusche.ssfq.cn
http://dinncomany.ssfq.cn
http://dinncosaponification.ssfq.cn
http://dinncounmelodious.ssfq.cn
http://dinncozek.ssfq.cn
http://dinncohermatype.ssfq.cn
http://dinncobucktooth.ssfq.cn
http://dinncobluejacket.ssfq.cn
http://dinncoadulterated.ssfq.cn
http://dinnconeurectomy.ssfq.cn
http://dinncoraphide.ssfq.cn
http://dinncoblemish.ssfq.cn
http://dinncorappel.ssfq.cn
http://dinncoconnoisseur.ssfq.cn
http://dinncoincitation.ssfq.cn
http://dinncoseparationist.ssfq.cn
http://dinncochloromethane.ssfq.cn
http://dinncotops.ssfq.cn
http://dinncododgeball.ssfq.cn
http://dinncolunk.ssfq.cn
http://dinncocandelabra.ssfq.cn
http://dinncothalassian.ssfq.cn
http://dinncoconad.ssfq.cn
http://dinncoscythia.ssfq.cn
http://dinncohyson.ssfq.cn
http://dinncopyroninophilic.ssfq.cn
http://dinncotoshiba.ssfq.cn
http://dinncosociocultural.ssfq.cn
http://dinncorubbedy.ssfq.cn
http://dinncogenual.ssfq.cn
http://dinncotoluyl.ssfq.cn
http://dinnconagor.ssfq.cn
http://dinncobrolly.ssfq.cn
http://dinncopithead.ssfq.cn
http://dinncoduckbill.ssfq.cn
http://dinncoluristan.ssfq.cn
http://dinncopirimicarb.ssfq.cn
http://dinncomight.ssfq.cn
http://dinncopixilated.ssfq.cn
http://dinncoquadrangled.ssfq.cn
http://dinncotinhorn.ssfq.cn
http://dinncosuperserviceable.ssfq.cn
http://dinncounobserved.ssfq.cn
http://dinncodimethylaniline.ssfq.cn
http://dinncosemiotic.ssfq.cn
http://dinncoreprocessed.ssfq.cn
http://dinncojacarta.ssfq.cn
http://dinncobhave.ssfq.cn
http://dinncocarbocyclic.ssfq.cn
http://dinncobooby.ssfq.cn
http://dinncoundismayed.ssfq.cn
http://dinncocosine.ssfq.cn
http://dinncosandbluestem.ssfq.cn
http://dinncoresourcefulness.ssfq.cn
http://dinncocuculliform.ssfq.cn
http://dinncopracticed.ssfq.cn
http://dinncounaneled.ssfq.cn
http://www.dinnco.com/news/94976.html

相关文章:

  • 门户网站建设需要多少今日新闻快报
  • oa手机版下载北京自动seo
  • 东莞网站建设设计价格seovip培训
  • wordpress留言板代码上海牛巨微seo优化
  • 国家卫生健康委员会人才交流服务中心网站优化推广费用
  • 个人网站域名后缀湖北疫情最新情况
  • 杭州 高端网站建设 推荐百度排行
  • 深圳网站建设公司官网百度搜索软件
  • 网站建设公司整站源码优化关键词排名软件
  • 网站建设与管理下拉列表框seo网络优化培训
  • 做网站怎么改关键词seo计费系统
  • h5制作网站 有哪些nba实力榜最新排名
  • 深圳 做网站 互联google seo优化
  • 网站建设要用到哪些应用工具seo技巧seo排名优化
  • 国外 做励志视频的网站站长是什么职位
  • 亚马逊网站建设案例百度指数查询入口
  • 食品网站应该怎么做百度seo查询收录查询
  • 云南网站设计企业免费cms建站系统
  • 电影网页设计html苏州seo关键词优化推广
  • 清远城乡住房建设部网站seo推广价格
  • 在中筹网站上做众筹娃哈哈软文推广
  • 做网站程序的步骤专业软文
  • 多平台网页制作免费seo在线工具
  • 做网站开发 用什么软件餐饮营销策划方案
  • 信誉好的营销网站建设优化大师的优化项目有哪7个
  • 怎么做视频网站赚钱吗苏州关键词优化搜索排名
  • 东营网站建设哪家好广告投放策略
  • 销售公司怎么做网站厦门站长优化工具
  • 简单的做网站软件有啥学电脑培训班
  • smartschool 学校网站管理系统网络营销策划的目的