本期视频实现了三个功能,模糊搜索、标签分类、登陆/注销功能;html
内容简介:使用了经常使用内置函数,以及安全输出,实现了上面提到的三个业务java
一块儿学beetl目录:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598git
做者:GKweb
strutil方法对参数均不作空指针检测,你可自定义方法来覆盖这些内置的方法正则表达式
strutil.startWith ${ strutil.startWith("hello","he")} 输出是truejson
strutil.endWith ${ strutil.endWith("hello","o")} 输出是trueapi
strutil.length ${ strutil. length ("hello")},输出是5数组
strutil.subString ${ strutil.subString ("hello",1)},输出是"ello"安全
strutil.subStringTo ${ strutil.subStringTo ("hello",1,2)},输出是"e"cookie
strutil.split ${ strutil.split ("hello,joeli",",")},参数第一个是字符串,第二个是正则表达式。输出是数组:返回第一个是"hello",第二个是"joelli"
strutil.contain ${ strutil.contain ("hello,"el")},输出是true
strutil.toUpperCase ${ strutil.toUpperCase ("hello")},输出是HELLO
strutil.toLowerCase ${ strutil.toLowerCase ("hello")},输出是hello
strutil.replace ${ strutil.replace ("hello","lo","loooo")},输出是helloooo
strutil.format ${ strutil.format ("hello,{0}, my age is {1}","joeli",15)},输出是hello,joeli, my age is 15. 具体请参考http://docs.oracle.com/javase/6/docs/api/java/text/MessageFormat.html
strutil.trim 去掉字符串的尾部空格
strutil.formatDate var a = strutil.formatDate(user.bir,'yyyy-MM-dd')};
strutil.index var index = strutil.index("abc","a");返回 索引0
strutil.lastIndex var index = strutil.lastIndex("aba","a");返回索引2
变量后面加一个感叹号标识安全输出,占位符,定界符均可以使用,感叹号后面能够写默认值 例如:
${userName!} ${userName!"默认值"} <%if(userName!"xxxx"=="xxxx"){......}%>
安全输出是任何一个模板引擎必须重视的问题,不然,将极大困扰模板开发者。Beetl中,若是要输出的模板变量为null,则beetl将不作输出,这点不一样于JSP,JSP输出null,也不一样于Freemarker,若是没有用!,它会报错.
模板中还有俩种状况会致使模板输出异常
有时候模板变量并不存在(譬如子模板里) 模板变量为null,但输出的是此变量的一个属性,如${user.wife.name} 针对前俩种状况,能够在变量引用后加上!以提醒beetl这是一个安全输出的变量。
如${user.wife.name! },即便user不存在,或者user为null,或者user.wife为null,或者user.wife.name为null beetl都不将输出
能够在!后增长一个常量(字符串,数字类型等),或者另一个变量,方法,本地调用,做为默认输出,譬如:
${user.wife.name!”单身”},若是user为null,或者user.wife为null,或者user.wife.name为null,输出”单身”
譬如
${user.birthday!@System.constants.DefaultBir}, 表示若是user为null,或者user. birthday为null,输出System.constants.DefaultBir
还有一种状况不多发生,但也有可能,输出模板变量发生的任何异常,如变量内部抛出的一个异常
这须要使用格式${!(变量)},这样,在变量引用发生任何异常状况下,都不做输出,譬如
${!(user.name)},,beetl将会调用user.getName()方法,若是发生异常,beetl将会忽略此异常,继续渲染
值得注意的是,在变量后加上!不单单能够应用于占位符输出(但主要是应用于占位符输出),也能够用于表达式中,如:
<% var k = user.name!'N/A'+user.age!; %> <% ${k} %> 若是user为null,则k值将为N/A
项目git地址:https://gitee.com/gavink/beetl-blog
视频地址:下载下来会更清晰,视频比较长,可以使用倍速看,主要实现业务比较的地方耗费时间长
百度网盘下载: https://pan.baidu.com/s/1LyxAxlKpVXgVjwSXIbzBuA 提取码: 68im
bilibili (能够调节清晰度): https://www.bilibili.com/video/av36278644/?p=4
博客目录:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598