本期視頻實現了三個功能,模糊搜索、標籤分類、登陸/註銷功能;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