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