thymeleaf-帮助文档(官方)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Tutorial:UsingThymeleafDocumentversion:20141222-22December2014Projectversion:2.1.4.RELEASEProjectwebsite:(DocumentObjectModel),insteadofexplicitlywritingthatlogicascodeinsidethetemplate.Itsarchitectureallowsafastprocessingoftemplates,relyingonintelligentcachingofparsedfilesinordertousetheleastpossibleamountofI/Ooperationsduringexecution.Andlastbutnotleast,ThymeleafhasbeendesignedfromthebeginningwithXMLandWebstandardsinmind,allowingyoutocreatefullyvalidatingtemplatesifthatisaneedforyou.1.2WhatkindoftemplatescanThymeleafprocess?Out-of-the-box,Thymeleafallowsyoutoprocesssixkindsoftemplates,eachofwhichiscalledaTemplateMode:XMLValidXMLXHTMLValidXHTMLHTML5LegacyHTML5Allofthesemodesrefertowell-formedXMLfilesexcepttheLegacyHTML5mode,whichallowsyoutoprocessHTML5fileswithfeaturessuchasstandalone(notclosed)tags,tagattributeswithoutavalueornotwrittenbetweenquotes.Inordertoprocessfilesinthisspecificmode,Thymeleafwillfirstperformatransformationthatwillconvertyourfilestowell-formedXMLfileswhicharestillperfectlyvalidHTML5(andareinfacttherecommendedwaytocreateHTML5code).AlsonotethatvalidationisonlyavailableforXMLandXHTMLtemplates.Nevertheless,thesearenottheonlytypesoftemplatethatThymeleafcanprocess,andtheuserisalwaysabletodefinehis/herownmodebyspecifyingbothawaytoparsetemplatesinthismodeandawaytowritetheresults.Thisway,anythingthatcanbemodelledasaDOMtree(beitXMLornot)couldeffectivelybeprocessedasatemplatebyThymeleaf.1.3Dialects:TheStandardDialectThymeleafisanextremelyextensibletemplateengine(infactitshouldbebettercalledatemplateengineframework)thatallowsyoutocompletelydefinetheDOMnodesthatwillbeprocessedinyourtemplatesandalsohowtheywillbeprocessed.AnobjectthatappliessomelogictoaDOMnodeiscalledaprocessor,andasetoftheseprocessors—plussomeextraartifacts—iscalledadialect,ofwhichThymeleaf’scorelibraryprovidesoneout-of-the-boxcalledtheStandardDialect,whichshouldbeenoughfortheneedsofabigpercentofusers.1Page2of76TheStandardDialectisthedialectthistutorialcovers.Everyattributeandsyntaxfeatureyouwilllearnaboutinthefollowingpagesisdefinedbythisdialect,evenifthatisn’texplicitlymentioned.Ofcourse,usersmaycreatetheirowndialects(evenextendingtheStandardone)iftheywanttodefinetheirownprocessinglogicwhiletakingadvantageofthelibrary’sadvancedfeatures.ATemplateEnginecanbeconfiguredseveraldialectsatatime.Theofficialthymeleaf-spring3andthymeleaf-spring4integrationpackagesbothdefineadialectcalledthe“SpringStandardDialect”,mostlyequivalenttotheStandardDialectbutwithsmalladaptationstomakebetteruseofsomefeaturesinSpringFramework(forexample,byusingSpringExpressionLanguageinsteadofThymeleaf’sstandardOGNL).SoifyouareaSpringMVCuseryouarenotwastingyourtime,asalmosteverythingyoulearnherewillbeofuseinyourSpringapplications.TheThymeleafStandardDialectcanprocesstemplatesinanymode,butisespeciallysuitedforweb-orientedtemplatemodes(XHTMLandHTML5ones).BesidesHTML5,itspecificallysupportsandvalidatesthefollowingXHTMLspecifications:XHTML1.0Transitional,XHTML1.0Strict,XHTML1.0Frameset,andXHTML1.1.MostoftheprocessorsoftheStandardDialectareattributeprocessors.ThisallowsbrowserstocorrectlydisplayXHTML/HTML5templatefilesevenbeforebeingprocessed,becausetheywillsimplyignoretheadditionalattributes.Forexample,whileaJSPusingtaglibrariescouldincludeafragmentofcodenotdirectlydisplayablebyabrowserlike:form:inputTextname=userNamevalue=${user.name}/…theThymeleafStandardDialectwouldallowustoachievethesamefunctionalitywith:inputtype=textname=userNamevalue=JamesCarrotth:value=${user.name}/Whichnotonlywillbecorrectlydisplayedbybrowsers,butalsoallowusto(optionally)specifyavalueattributeinit(“JamesCarrot”,inthiscase)thatwillbedisplayedwhentheprototypeisstaticallyopenedinabrowser,andthatwillbesubstitutedbythevalueresultingfromtheevaluationof${user.name}duringThymeleafprocessingofthetemplate.Ifneeded,thiswillallowyourdesigneranddevelopertoworkontheverysametemplatefileandreducetheeffortrequiredtotransformastaticprototypeintoaworkingtemplatefile.TheabilitytodothisisafeatureusuallycalledNaturalTemplating.1.4OverallArchitectureThymeleaf’scoreisaDOMprocessingengine.Specifically,itusesitsownhigh-performanceDOMimplementation—notthestandardDOMAPI—forbuildingin-memorytreerepresentationsofyourtemplates,onwhichitlateroperatesbytraversingtheirnodesandexecutingprocessorsonthemthatmodifytheDOMaccordingtothecurrentconfigurationandthesetofdatathatispassedtothetemplateforitsrepresentation—knownasthecontext.TheuseofaDOMtemplaterepresentationmakesitverywellsuitedforwebapplicationsbe

1 / 76
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功