JSF学习总结经典教程.docx
- 文档编号:13711017
- 上传时间:2023-06-16
- 格式:DOCX
- 页数:53
- 大小:164.62KB
JSF学习总结经典教程.docx
《JSF学习总结经典教程.docx》由会员分享,可在线阅读,更多相关《JSF学习总结经典教程.docx(53页珍藏版)》请在冰点文库上搜索。
JSF学习总结经典教程
WhatisJSF?
JavaServerFaces(JSF)isaMVCwebframeworkthatsimplifiestheconstructionofuserinterfaces(UI)forserver-basedapplicationsbyusingreusableUIcomponentsinapage.JSFprovidesfacilitytoconnectUIwidgetswithdatasourcesandtoserver-sideeventhandlers.TheJSFspecificationdefinesasetofstandardUIcomponentsandprovidesanApplicationProgrammingInterface(API)fordevelopingcomponents.JSFenablesthereuseandextensionoftheexistingstandardUIcomponents.
Benefits
JSFreducestheeffortincreatingandmaintainingapplicationswhichwillrunonaJavaapplicationserverandwillrenderapplicationUIontoatargetclient.JSFfacilitatesWebapplicationdevelopmentby
∙providingreusableUIcomponents
∙makingeasydatatransferbetweenUIcomponents
∙managingUIstateacrossmultipleserverrequests
∙enablingimplementationofcustomcomponents
∙wiringclientsideeventtoserversideapplicationcode
JSFUIcomponentmodel
JSFprovidesdeveloperscapabilitytocreateWebapplicationfromcollectionsofUIcomponentsthatcanrenderthemselvesindifferentwaysformultipleclienttypes(forexampleHTMLbrowser,wirelessorWAPdevice).
JSFprovides
∙Corelibrary
∙AsetofbaseUIcomponents-standardHTMLinputelements
∙ExtensionofthebaseUIcomponentstocreateadditionalUIcomponentlibrariesortoextendexistingcomponents.
∙MultiplerenderingcapabilitiesthatenableJSFUIcomponentstorenderthemselvesdifferentlydependingontheclienttypes
JSFArchitecture
AJSFapplicationissimilartoanyotherJavatechnology-basedwebapplication;itrunsinaJavaservletcontainer,andcontains
∙JavaBeanscomponentsasmodelscontainingapplication-specificfunctionalityanddata
∙Acustomtaglibraryforrepresentingeventhandlersandvalidators
∙AcustomtaglibraryforrenderingUIcomponents
∙UIcomponentsrepresentedasstatefulobjectsontheserver
∙Server-sidehelperclasses
∙Validators,eventhandlers,andnavigationhandlers
∙Applicationconfigurationresourcefileforconfiguringapplicationresources
Therearecontrollerswhichcanbeusedtoperformuseractions.UIcanbecreatedbywebpageauthorsandthebusinesslogiccanbeutilizedbymanagedbeans.
JSFprovidesseveralmechanismsforrenderinganindividualcomponent.Itisuptothewebpagedesignertopickthedesiredrepresentation,andtheapplicationdeveloperdoesn'tneedtoknowwhichmechanismwasusedtorenderaJSFUIcomponent.
LifeCycle
JSFapplicationlifecycleconsistofsixphaseswhichareasfollows
∙Restoreviewphase
∙Applyrequestvaluesphase;processevents
∙Processvalidationsphase;processevents
∙Updatemodelvaluesphase;processevents
∙Invokeapplicationphase;processevents
∙Renderresponsephase
ThesixphasesshowtheorderinwhichJSFprocessesaform.Thelistshowsthephasesintheirlikelyorderofexecutionwitheventprocessingateachphase.
Phase1:
Restoreview
JSFbeginstherestoreviewphaseassoonasalinkorabuttonisclickedandJSFreceivesarequest.
Duringthisphase,theJSFbuildstheview,wireseventhandlersandvalidatorstoUIcomponentsandsavestheviewintheFacesContextinstance.TheFacesContextinstancewillnowcontainsalltheinformationrequiredtoprocessarequest.
Phase2:
Applyrequestvalues
Afterthecomponenttreeiscreated/restored,eachcomponentincomponenttreeusesdecodemethodtoextractitsnewvaluefromtherequestparameters.Componentstoresthisvalue.Iftheconversionfails,anerrormessageisgeneratedandqueuedonFacesContext.Thismessagewillbedisplayedduringtherenderresponsephase,alongwithanyvalidationerrors.
Ifanydecodemethods/eventlistenerscalledrenderResponseonthecurrentFacesContextinstance,theJSFmovestotherenderresponsephase.
Phase3:
Processvalidation
Duringthisphase,theJSFprocessesallvalidatorsregisteredoncomponenttree.Itexaminesthecomponentattributerulesforthevalidationandcomparestheserulestothelocalvaluestoredforthecomponent.
Ifthelocalvalueisinvalid,theJSFaddsanerrormessagetotheFacesContextinstance,andthelifecycleadvancestotherenderresponsephaseanddisplaythesamepageagainwiththeerrormessage.
Phase4:
Updatemodelvalues
AftertheJSFchecksthatthedataisvalid,itwalksoverthecomponenttreeandsetthecorrespondingserver-sideobjectpropertiestothecomponents'localvalues.TheJSFwillupdatethebeanpropertiescorrespondingtoinputcomponent'svalueattribute.
IfanyupdateModelsmethodscalledrenderResponseonthecurrentFacesContextinstance,theJSFmovestotherenderresponsephase.
Phase5:
Invokeapplication
Duringthisphase,theJSFhandlesanyapplication-levelevents,suchassubmittingaform/linkingtoanotherpage.
Phase6:
Renderresponse
Duringthisphase,theJSFaskscontainer/applicationservertorenderthepageiftheapplicationisusingJSPpages.Forinitialrequest,thecomponentsrepresentedonthepagewillbeaddedtothecomponenttreeastheJSPcontainerexecutesthepage.Ifthisisnotaninitialrequest,thecomponenttreeisalreadybuiltsocomponentsneednottobeaddedagain.Ineithercase,thecomponentswillrenderthemselvesastheJSPcontainer/Applicationservertraversesthetagsinthepage.
Afterthecontentoftheviewisrendered,theresponsestateissavedsothatsubsequentrequestscanaccessitanditisavailabletotherestoreviewphase.
ManagedBeans
∙ManagedBeanisaregularJavaBeanclassregisteredwithJSF.Inotherwords,ManagedBeansisajavabeanmanagedbyJSFframework.
∙Themanagedbeancontainsthegetterandsettermethods,businesslogicorevenabackingbean(abeancontainsalltheHTMLformvalue).
∙ManagedbeansworksasModelforUIcomponent.
∙ManagedBeancanbeaccessedfromJSFpage.
∙InJSF1.2,amanagedbeanhadtoregisteritinJSFconfigurationfilesuchasfaces-config.xml.
∙FromJSF2.0onwards,Managedbeanscanbeeasilyregisteredusingannotations.Thisapproachkeepsbeansandthereregistrationatoneplaceanditbecomeseasiertomanage.
UsingXMLConfiguration
UsingAnnotation
@ManagedBean(name="helloWorld",eager=true)
@RequestScoped
publicclassHelloWorld{
@ManagedProperty(value="#{message}")
privateMessagemessage;
...
}
@ManagedBeanAnnotation
@ManagedBean marksabeantobeamanagedbeanwiththenamespecifiedin name attribute.Ifthenameattributeisnotspecified,thenthemanagedbeannamewilldefaulttoclassnameportionofthefullyqualifiedclassname.InourcaseitwouldbehelloWorld.
Anotherimportantattributeis eager.Ifeager="true"thenmanagedbeaniscreatedbeforeitisrequestedforthefirsttimeotherwise"lazy"initializationisusedinwhichbeanwillbecreatedonlywhenitisrequested.
ScopeAnnotations
Scopeannotationssetthescopeintowhichthemanagedbeanwillbeplaced.Ifscopeisnotspecifiedthenbeanwilldefaulttorequestscope.Eachscopeisbrieflydiscussedbelow.
Scope
Description
@RequestScoped
BeanlivesaslongastheHTTPrequest-responselives.ItgetcreateduponaHTTPrequestandgetdestroyedwhentheHTTPresponseassociatedwiththeHTTPrequestisfinished.
@NoneScoped
BeanlivesaslongasasingleELevaluation.ItgetcreateduponanELevaluationandgetdestroyedimmediatelyaftertheELevaluation.
@ViewScoped
BeanlivesaslongasuserisinteractingwiththesameJSFviewinthebrowserwindow/tab.ItgetcreateduponaHTTPrequestandgetdestroyedonceuserpostbacktoadifferentview.
@SessionScoped
BeanlivesaslongastheHTTPsessionlives.ItgetcreateduponthefirstHTTPrequestinvolvingthisbeaninthesessionandgetdestroyedwhentheHTTPsessionisinvalidated.
@ApplicationScoped
Beanlivesaslongasthewebapplicationlives.ItgetcreateduponthefirstHTTPrequestinvolvingthisbeanintheapplication(orwhenthewebapplicationstartsupandtheeager=trueattributeissetin@ManagedBean)andgetdestroyedwhenthewebapplicationshutsdown.
@CustomScoped
Beanlivesaslongasthebean'sentryinthecustomMapwhichiscreatedforthisscopelives.
@ManagedPropertyAnnotation
JSFisasimplestaticDependencyInjection(DI)framework.
JSF是一个简单的静态依赖注入(DI)框架。
Using @ManagedProperty annotationamanagedbean'spropertycanbeinjectedinanothermanagedbean.
ExampleApplication
LetuscreateatestJSFapplicationtotesttheaboveannotationsformanagedbeans.
Step
Description
1
Createaprojectwithaname helloworld underapackage com.tutorialspoint.test asexplainedinthe JSF-CreateApplication chapter.
2
Modify HelloWorld.java asexplainedbelow.Keeprestofthefilesunchanged.
3
Create Message.java underapackage com.tutorialspoint.test asexplainedbelow.
PageNavigation
NavigationrulesarethoserulesprovidedbyJSFFrameworkwhichdescribewhichviewistobeshownwhenabuttonorlinkisclicked.
∙NavigationrulescanbedefinedinJSFconfigurationfilenamedfaces-config.xml.
∙Navigationrulescanbedefinedinmanagedbeans.
∙Navigationrulescancontainconditionsbasedonwhichresultedviewcanbeshown.
∙JSF2.0providesimplicitnavigationaswellinwhichthereisnoneedtodefinenavigationrulesassuch.
Implici
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JSF 学习 总结 经典 教程