【精品文档】532关于计算机专业JSP表达式语言介绍简介概述的毕业设计论文英文英语外文文献翻译成品资料:JSP和EL介绍中英文双语对照15文档格式.docx
《【精品文档】532关于计算机专业JSP表达式语言介绍简介概述的毕业设计论文英文英语外文文献翻译成品资料:JSP和EL介绍中英文双语对照15文档格式.docx》由会员分享,可在线阅读,更多相关《【精品文档】532关于计算机专业JSP表达式语言介绍简介概述的毕业设计论文英文英语外文文献翻译成品资料:JSP和EL介绍中英文双语对照15文档格式.docx(35页珍藏版)》请在冰点文库上搜索。
![【精品文档】532关于计算机专业JSP表达式语言介绍简介概述的毕业设计论文英文英语外文文献翻译成品资料:JSP和EL介绍中英文双语对照15文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-4/29/16d2b3c9-dda1-4bc3-afe3-526fdc769443/16d2b3c9-dda1-4bc3-afe3-526fdc7694431.gif)
Unliketheearlierchapters,thischapterprovidesamorein-depthexplorationofacornerstoneelementoftheJSPstandard:
JSP’sExpressionLanguage,orELforshort.
Inthischapter,youwilldiscoverthefollowing:
❑WhyELisanindispensablepartofJSP2.0
❑HowtouseELnamedvariables
❑HowtoperformtypeconversionandcoercioninEL
❑HowtohandlenullvaluesinEL
❑Howtoworkwitharithmetic,logical,comparison,andotheroperatorsinEL
❑ThedifferentwaysofaccessingmembersofacollectioninEL
❑HowtoaccesspropertiesofobjectsinEL
❑HowtoaccessnestedobjectpropertiesinEL
❑HowtouseimplicitobjectsinEL
❑HowtodefineandaccessfunctionsinEL
❑HowtouseanamespacewhenreferencingELfunctions
Thischaptercoversalotofground,andincludesfiveseparateexamples.Bytheendofthechapter,youshouldbeveryfamiliarwithEL,andbecomfortableusingitinyourownapplications.
ELandItsVitalRoleinJSP
ELwasnotpartoftheJSPstandardpriorto2.0.OneofthemainreasonsforcreatingELwastoensurethatpresentation-levelJSPpagescouldbecreatedwithoutrelyingonscriptingelements.Chapter3,“JSPBasics2:
GeneralizedTemplatingandServerScripting,”coveredtheuseofscriptingelements(typicallycodewritteninJava)thatareembeddedwithinaJSPpage.
TheneedforscriptingelementswithinJSPistypicallydrivenbytherequirementsofanapplication.
Majorapplicationrequirementsthatdemandtheuseofscriptingelementsincludethefollowing:
❑Toprovidecontrol-flowforJSPexecution
❑Toset,andsubsequentlyaccess,variablesthatarelocaltotheJSPpage
❑TorenderavaluefromacomplexexpressionthatinvolvesJavaobjects
❑ToaccesspropertiesofanarbitraryJavaobject
❑TocallmethodsofJavaBeansorotherJavaobjects
Unfortunately,experiencehasshownthattheuseofscriptingelementsinJSPsmakeslargeprojectsdiffi-culttomaintainoverthelongterm.Italsoencouragesprogrammingpracticesthatmaytightlycouplethepresentation(userinterface)ofanapplicationtoitsbusinesslogic,reducingitsflexibilityandscala-bility.ThisisahighlyundesirablepracticeinthecreationofWebapplicationsandservices.Ideally,JSPsshouldbecreatedfreeofscriptingelementsifatallpossible.
InordertocreateaJSPversionthatcanworkcompletelyfreeofscriptingelements,itisessentialthatallfiveoftheapplicationrequirementscanbesatisfiedwithouttheuseofembeddedJavacoding.JSP2.0satisfiestheserequirements.ThefirsttwoitemsarehandledbyJSTL(exploredindetailinChapter6,“JSPTagLibrariesandJSTL”),whilethelastthreerequirementsarehandledbyEL.
ELisindependentofJSP2.0.DespitethevitalroleofELinJSP2.0,theuseofELisnotexclusivetoJSPcontainers.TheELparserisalsonottightlyintegratedintotheJSPparser,butcanbedetachedandreusedforotherpurposes.Infact,ELisincorporatedintobothJavaServerFaces(JSF,aserver-sideGUIconstructionkittechnology;
seeChapter21,“JavaServerFaces,”formoreinformation),andJSTL.Therefore,becomingfamiliarwithELwillsaveyoutimewhenexploringtheseothertechnologies.
ELNamedVariables
WhenworkingwithEL(andJSTL),youwillfrequentlyworkwithnamedvariables.NamedvariablesinELrefertoattributesattachedtoJSPscopingobjects.Forexample,thefollowingELexpressionwillren-derthevalueofthenamedvariablecalledbearCount:
TheELparserwillsearchfortheattributethroughthevariousJSPscopingobjectsinthefollowingorder:
1Page
2.Request
Session(ifcurrentlyvalid)
Application
Inpractice,thisisequivalenttoacalltotheJavaPageContext.findAttribute()method.IfyouaremaintaininglegacyJSPcodewithembeddedJava,youmayseethiscall.YoucanconsulttheJSPJavadocs(APIdocumentation)toseehowthismethodworks.
Figure5-1illustratesthesearchforanELnamedvariable.
Figure5-1:
OrderofsearchforanELnamedvariable
InFigure5-1,youcanseethesearchorderforanELnamedvariable:
FirstthesetofELimplicitobjectsissearchedforamatchingname(ELimplicitobjectsarecoveredlaterinthischapter),andthenthefourscopes.Ifthenamedvariableisnotfound,someactionmaycreateit.
Iftheattributewiththespecifiednamecannotbelocatedinanyscope,anullvalueisreturned.Thenullvaluewillrenderasa“”(emptystring)intheoutput,andwillnottriggeranyerror.
ApplyingEL
Asthenameimplies,theExpressionLanguageworkswithexpressions.Itisaprogramminglanguageusedintheconstructionofexpressions.AllELexpressionsareenclosedinthe${...}notation.TheexpressionisevaluatedbeforetherestoftheJSPisevaluated.YoucanplaceELexpressionsintwoplaceswithinaJSP:
❑Inlinewithtemplatedata
❑WithinattributesofJSPactions,standardorcustom
UsingELexpressionsinlinewithtemplatedata
ThemosttypicaluseofELexpressionsistorenderatextualstringinlinewithtemplatedata.Forexample,considerthefollowingJSPfragment:
<
b>
Thereare${5+1}bearsinthecave.<
/b>
Inthiscase,theELexpressionisplacedinlinewiththeHTMLtemplatedata.AtJSPprocessingtime,theexpression${5+1}willevaluateto6,andbeplacedatthesamepositionastheoriginalexpression.ThiswillresultinthefollowingHTML:
Thereare6bearsinthecave.<
UsingELexpressionsinattributevalues
TheotherplacewhereyouwillseeELexpressionsusediswithintheattributesofcertaintags,includingJSTLtags,standardJSPactions,andcustomtags.
Forexample,considerthefollowingJSPfragmentusingJSTL:
c:
iftest=”${salary>
100000}”>
Richcousin!
br/>
/c:
if>
Inthiscase,theELexpression${salary>
100000}isusedtoconditionallyrenderthebodyoftheJSTLtag.Itisplacedinthetestattributeofthe<
customJSTLaction.ThiswillcausetheELexpressiontobeevaluatedbeforetheJSTLcustomactionisevaluated.TheHTML<
willberenderedonlyifthenamedvariablecalledsalaryisgreaterinvaluethan100,000.
Forexample,considerthefollowingJSPfragmentfeaturingtheJSTL<
set>
tag:
setvar=”datetime”val=’${dateNow}-${timeNow}’/>
IfthedateNownamedvariablecontainsthestring“May1,2005”andtimeNowcontains“11:
00AM”,thentheresultingdatetimenamedvariablewillcontain“May1,2005-11:
00AM”.
Asshownintheprecedingsection,anELexpressioncanbeanarithmeticexpression,oranexpressionfeaturingacomparison.ThefollowingTryItOutsectionexploressomeoftheseELexpressions.
Totryoutthefirstexample,deploych05.wartoyourTomcat5server.ReviewChapter1ifyoudonotrememberhowtodeployaWARfile.
AccessthefollowingURLusingyourbrowsertoseetheexample’soutput:
http:
//localhost:
8080/ch05/example1/
YoushouldseesomethingsimilartowhatisshowninFigure5-2.
Figure5-2:
OutputresultofELexpressions
TheoutputinFigure5-2istheresultofacollectionofdifferentELexpressions.ThefollowingHowItWorkssectionexaminestheELexpressionsusedtogeneratethisoutput.
HowItWorks
TheJSPusedtogeneratetheoutputshowninFigure5-2islocatedat<
TomcatInstallation
Notethatoperatorprecedencehascausedthemultiplicationanddivisiontobeexecutedfirst.Furthermore,thecalculationhascausedadecimaltoappearintheoutput.YoucanusethenumericformattingtagsinJSTLtoeliminatethis.Forexample,thefollowingJSPfragmentinindex.jsprendersthesameresultwith-outthedecimalpoint.
TheformatNumberJSTLtagisusedtoformatthenumericaloutput,usingapatternthatspecifiesnodecimalpointshouldbedisplayed.Thisresultsinthefollowingoutput.
Comparisonoperators
ELsupportstheentirerangeofcomparisonoperatorsfoundinmostprogramminglanguages.Eachoftheoperatorshasasymbolicformandanabbreviatedtextualform.Thefollowingtableenumeratestheavailableoperators.
NotethatinthepreviouscomparisonELalwaysattemptstoconvertthetypeofthedatabeingcom-pared“intherightway.”Thatis,thecharacterdataisconvertedtonumericvaluebeforebeingcom-pared,exceptinthecasewheretwocharactersarecompared.
Inthepreviousexample,theemptystring“”isconsideredemptywhileanyotherstringvalueisnot.Anonexistentvariable,Junk,isgivenanullvalueinEL.Thisnullvalueisconsideredemptywhentestedwiththeemptyoperator.Ontheotherhand,theguessvariableexists(usedearlier)andthereforeisnotempty.Inotherwords,dependingonthedatatypeoftheoperand,theemptyoperatorcanalsobeusedtotestfortheconditionsshowninthefollowingtable.
ThenamedvariableStrVarisinitiallysettothestringtrue.ThisisavariableoftypeString.However,becausethetestattributeoftheJSTL<
tagrequiresaBooleanexpression,itisautomaticallycon-vertedtoaBooleanbyELbeforeuse.Thiscausestheprintingofthe“equal!
”message.
Finally,thecomparisonoperatorisbeingusedonthespecialnullvalue.Itisshown