JSP Velocity Freemarker comparison

JSP has been the De facto standard for Java and web development for years. JSP is one of the first popular dynamic web application frameworks. Since then as architecture models have evolved. PHP has taken a foothold in the simple dynamic web applications and large enterprise application with complex business logic has moved to MVC design using Java or .Net. Where does that leave JSP and how does it compare with Velocity and Freemarker? 

JSPOverview

Originally,JavaServerPagesweredesignedtobeutilizedwithinaJ2EEwebcontainerthatwouldallowforfullapplicationdevelopment.OlderapplicationsweredesignedtobewrittentodirectlyaccesstheJSPfiles.ThisistheMVCmodel1architectureapproach.TheclientdirectlyreferencestheJSPpage,theJSPpagereferencesabeanwhichinturncanaccessdatabaseinformation.Withtoday'sstandards,however,itistypicallynotacceptabletoutilizeJSPasanindependentarchitecturesolution.Assoftwarearchitecturehasevolvedanddesignpatternshavechanged,itseemsthatthepurposesofJSPpageshavechangedaswell.

TheabilityofJSPtopermitscripting,tags,andhtmlcodemakesitaspowerfulasJavaitselfwiththecapabilitytobedirectlyrunasawebpage.Torebuttalthestrengthofthispoint,thisunrestrictedabilityofallowingsomanydifferentdevelopmenttechniquesissimplydangerous.ItbecomesdangeroustoboththeapplicationarchitectureandtotheaveragedeveloperasitisveryeasytointroduceinconsistencyindesignpatternsintotheJSP.ThecomplexityofJSPcangrowquicklyandfurtherdevelopmentorongoingmaintenancecouldsufferinbothtimeandcost.

AnexampleofabusiveJSPdevelopmentcanoccurwhenmixingdifferentJSPsyntax.UsinganparticulartaglibrarywithwithanexpressionorusingETLinsideandoutsideoftags.JSPScripletsareinviolationoftheMVCapproachwhenusingJSPasaviewcomponentwhichistheprimaryfunctionJSPpagesareusedtoday.

VeloctiyandFreemarkerOverview

VelocityandFreemarkeraretemplateenginesthatdefineatemplatelanguageinwhichtoparse.TheytakeJavaobjectsandmergethedatawiththetemplate.VelocityandFreemarkeraresosimilarIwillnotcompareonetotheother.Bothareactiveprojects.Velocityrecentlycameoutofitsshellafterafewslowyearsandhadsomereleasesin2007.Freemarkermayhaveafewbitmorefunctionalitythanvelocitybutisalsoabitmorecomplex.

JSPvsTemplateEngines

*CompileTime-JSPneedtobeprocessedandcompiledtobeforeitisexecuted.JSPfileisconvertedtoaJavafile.Itisthencompiledintoaclassfile.Bothvelocityandfreemarkerdonotneedcompiliation.Thisisespeciallybeneficialtoadeveloper.EverytimeadevelopermodifiesaJSPitisveryslowtorunitduetocompilation.TheVelocityengineisnamedVelocityforareason.

*LearningCurve-JSPisverylargewithmanytagdifferentlibraries.VelocityandFreemarkeraresimple.Theengineandcorefunctionalitycanbeunderstoodwithinamatterofhours.

*Readibility-JSPallowsfortaglibraries,scriplets,expressionlanguage,andothervarioustechniquesthatisrecognizedascode.Velocityusesashortlistofbuiltincommands.

*CommunityBase-ManydevelopersknowJSP.Itisaverycommonlanguagefordeveloperstolistasaskill.BothfreemarkerandvelocityhavealargeuserbasebutneitherisaslargeasJSP.ThisisbecauseJSPwasoriginallyusedasafullapplicationframeworkwhendeployedtoacompliantwebcontainer.

*Templatestructure-JSPallowsformanydifferenttypesofsyntax.Scriptingtechniquessuchasdeclarations,scriptlets,andexpressionsaswellasactionsandtags.Velocityusesa#commandforcommandsand$varforvariables.Freemarkeruses<#command>forcommandsand${var}forvariables.JSP2.0triestoappearastemplateenginewith${}tryingtoresembletemplatestyle

*CustomTools-JSPallowsforthecreationofJSPtaglibs.BothFreemarkerandVelocityallowtheutilizationofJSPtaglibsaswell.Freemarkerhasalotofextrabuiltinfunctionalityaswell.Velocityallowsthedevelopertocreatetheirowncustomtools.ThesetoolsarecreatedinJavaandutilizedasJavaobjectswithinthetemplate.

*Integration-MostofthemajorlibrariesanddevelopmenttoolsintegratewithJSP,VelocityandFreemarker.FrameworkssuchasSitemesh,Tiles,Struts,andSpringhavefunctionalityavailabletoutilizeall3technologies.

therearecoupleofthingswhereFreeMarkermightsuityoubetter:

1.Thisissubjective,buttheoverallsyntaxislessawkward.I’mawarethere’slotofsyntacicsugarinJSP2.0that’saimedathidingtheuglinessofdirectlyusingJSTLtagsforcontrolflowetc.butrememberthatFreeMarkerhashadaratherergonomicsyntaxfor“foreach”and“if”foryears.

2.Macros.Ifyouwanttohavereusabletemplatesnippets,youcanwritethemasmacrosandimportthem.AFAIK,inJSP2.0youfinallyhaveasortofmacros,butyouneedtodefineeachsnippetinitsownfile.Thisisnowhereaselegantasthefullnamespace/macrosolutioninFreeMarker.

3.Transforms.TransformsaretheequivalentofbodytagsinJSP,withasignificantdistinctionthatwhileJSPbodytagsfirstcachealloftheirbodyandonlyafterthatactonthem,FreeMarkertransformsarefullyoutput-driven,infacttheyareimplementedinternallyaschainsofjava.io.Writerobjects.Therefore,regardlessofhowmanytransformsyouhavenested,youcanstillhaveprogressivepagerendering(“can”becauseatransformcanchoosetocacheallofitsbodylikeaJSPbodytag,butisnotmandatedtodosoandcanprovideacleverimplementationthatallowsprogressiveprocessing).

4.Also,theusualargumentaboutusabilityoutsidetheHTTPprotocolstack.IfyoursystemdoesanytextoutputbesideHTTPresponses–i.e.sendsSMTPmail,youcanuseFreeMarkerforgeneratingmailmessagesfromtemplates,too,orforgeneratingarbitraryXML.ItevenhasdeclarativeXMLprocessingcapabilitieswhichmakeitpossibletouseitsimilartoXSLT,butsignificantlylesscomplex.Youcansimplifyyourprojectbyhavingonelanguageforallyourtextoutputgeneratingneeds.

5.Datamodelflexibility–ifyoulike,youcanimplementthedatamodelinterfacestonaturallyrepresentanyunderlyingdatamodel.I.e.wehaveawrapperforJythonobjectsthatprettyfaithfullyreflectsthelookandfeelofJythonobjectsinthetemplatesyntax.PeoplehavereportedprovidingasimilarbindingforJavaScriptetc.

There’salsolotsofgoodieslikeautomaticHTMLorXMLescapingofexpressions,convenientliteralsyntaxforlistsandmaps,etc.

Thismuchoffthetopofmyhead–it’sabitlatehere,soIcan’tthinkofanythingelseatthisverymoment.

Velocity'sSimplisticdesign

Ofthethreetechnologies,velocityisthesimplest.Ithasasmallamountofverypowerfulcommands.Syntaxisasfollows.

Displayvariablename

$var

Displayvariablename.Ifnullitdisplaysblank

$!var

Directives

#foreach($itemin$collection)itemis$item#end

#if($order.total==0)Nocharge#end

#parse("header.vm")

#macro

#include("disclaimer.txt")

#set($customer=${order.customer})

CodeComparisons

HereissomecodethatiswritteninJSP.Thisisthelogicthatwouldbeusedwhenprocessinganumbernamed"number"ispassedinasaparameter.

<%@pagelanguage="java"%>

<%@tagliburi="/WEB-INF/struts-logic.tld"prefix="logic"%>

<!--Isthenumberguessright?-->

<logic:equalparameter="number"value="7">

Youguessedright!Youwinahighspeedblender!

</logic:equal>

<!--Ifthenumberguessedwaswrong-->

<logic:notEqualparameter="number"value="7">

<!--LessThan-->

<logic:lessThanparameter="number"value="7">

Alittlehigher...

</logic:lessThan>

<!--GreaterThan-->

<logic:greaterThanparameter="number"value="7">

Alittlelower...

</logic:greaterThan>

</logic:notEqual>

Andhereisthesamelogicwithinavelocitytemplate

<!--Isthenumberguessright?-->

#if($number==7)

Youguessedright!Youwinahighspeedblender!

#end

<!--Ifthenumberguessedwaswrong-->

#if($number!=7)

<!--LessThan-->

#if($number<7)

Alittlehigher...

<!--GreaterThan-->

#elseif($number>7)

Alittlelower...

#end

#end

Nowaanotherexamplewiththesametask.Createalistanddisplayit.FirstJSP.

<%@pagelanguage="java"%>

<%@tagliburi="/WEB-INF/struts-bean.tld"prefix="bean"%>

<%@tagliburi="/WEB-INF/struts-logic.tld"prefix="logic"%>

<%

java.util.ArrayListlist=newjava.util.ArrayList();

list.add("First");

list.add("Second");

list.add("Third");

list.add("Fourth");

list.add("Fifth");

pageContext.setAttribute("list",list,PageContext.PAGE_SCOPE);

%>

<logic:iterateid="myCollectionElement"name="list">

ElementValue:<bean:writename="myCollectionElement"/><br/>

</logic:iterate>

Andhereisthevelocityversion.

#set($list=["First","Second","Third","Fourth","Fifth"])

#foreach($itemin$list)

ElementValue:$item<br/>

#end

Conclusion

JSPhasprovideddeveloperswithaveryusefullibraryandtoolset.Ithasbeenaroundforalongtimeandtherearenosignsthatitsusersaredeclining.DuetoMVCstylearchitecture,serversideprocessingincludingbusinesslogicshouldnottakeplacewithintheviewcomponent.IdidnotgetintoVelocityvsFreemarkerdebatesincetheyareidenticalfromanarchitecturalstandpoint.Youcanmakethatdecisionbycomparingthetemplatelanguageitselfandchoosewhichisbettersuitedforyourneeds.WhendeterminingwhichtechnologytoutilizeIhopethatyoutaketimetoresearcheachtechnologyandusethisarticleasareference.

相关推荐