JSF 源代码赏析之Lifecycle

JSF 源代码赏析之Lifecycle

关键字: jsf sourcecode lifecycle

JSF的生命周期在JSF应用中起着至关重要的作用,每一个JSF请求的处理都需要经过一次生命周期,本文从源码的角度分析JSF的生命周期。
在讨论生命周期之前,我们先要讨论FacesContext的一些元素,他们在整个生命周期中扮演了非常重要的角色。么个JSF应用必须保存它所处理的请求信息,FacesContext为处理请求和生成响应保存了所有必需的上下文信息,具体而言,它包括:
1.信息队列,MessageQueue,保存所有的消息
2.当前的组件树,ViewRoot,
3.外部上下文,ExternalContext
4.Application。
下面就是Sun的FacesContextImpl中的变量:
com.sun.faces.context.FacesContextImpl:
  1. //RelationshipInstanceVariables
  2. privateResponseStreamresponseStream=null;
  3. privateResponseWriterresponseWriter=null;
  4. privateExternalContextexternalContext=null;
  5. privateApplicationapplication=null;
  6. privateUIViewRootviewRoot=null;
  7. privateELContextelContext=null;
  8. privateRenderKitFactoryrkFactory;
  9. privateRenderKitlastRk;
  10. privateStringlastRkId;
  11. /**
  12. *StoremappingofclientIdtoArrayListofFacesMessage
  13. *instances.ThenullkeyisusedtorepresentFacesMessageinstances
  14. *thatarenotassociatedwithaclientIdinstance.
  15. */
  16. privateMap<String,List<FacesMessage>>componentMessageLists;
  17. //AttributeInstanceVariables
  18. privatebooleanrenderResponse=false;
  19. privatebooleanresponseComplete=false;

这里面有很多重要的对象值得我们去研究,按照从上到下的顺序,我们先来看看ExternalContext。
ExternalContext其实是对ServletContext(或PortletContext)的封装,提供了访问外部容器资源的各种方法,ExternalContext基类定义如下:
javax.faces.context.ExternalContext:
  1. publicabstractclassExternalContext{
  2. publicstaticfinalStringBASIC_AUTH="BASIC";
  3. publicstaticfinalStringCLIENT_CERT_AUTH="CLIENT_CERT";
  4. publicstaticfinalStringDIGEST_AUTH="DIGEST";
  5. publicstaticfinalStringFORM_AUTH="FORM";
  6. //----------------------------------------------------------PublicMethods
  7. publicabstractvoiddispatch(Stringpath)
  8. throwsIOException;
  9. publicabstractStringencodeActionURL(Stringurl);
  10. publicabstractStringencodeNamespace(Stringname);
  11. publicabstractStringencodeResourceURL(Stringurl);
  12. publicabstractStringgetAuthType();
  13. publicabstractObjectgetContext();
  14. publicabstractStringgetInitParameter(Stringname);
  15. publicabstractMapgetInitParameterMap();
  16. publicabstractStringgetRemoteUser();
  17. publicabstractObjectgetRequest();
  18. publicvoidsetRequest(Objectrequest){
  19. ExternalContextimpl;
  20. if(null!=(impl=(ExternalContext)this.getRequestMap().
  21. get("com.sun.faces.ExternalContextImpl"))){
  22. impl.setRequest(request);
  23. return;
  24. }
  25. thrownewUnsupportedOperationException();
  26. }
  27. publicvoidsetRequestCharacterEncoding(Stringencoding)throwsUnsupportedEncodingException{
  28. ExternalContextimpl;
  29. if(null!=(impl=(ExternalContext)this.getRequestMap().
  30. get("com.sun.faces.ExternalContextImpl"))){
  31. impl.setRequestCharacterEncoding(encoding);
  32. return;
  33. }
  34. thrownewUnsupportedOperationException();
  35. }
  36. publicabstractStringgetRequestContextPath();
  37. publicabstractLocalegetRequestLocale();
  38. publicabstractIterator<locale></locale>getRequestLocales();
  39. publicabstractIterator<string></string>getRequestParameterNames();
  40. publicabstractStringgetRequestPathInfo();
  41. publicabstractStringgetRequestServletPath();
  42. publicStringgetRequestCharacterEncoding(){
  43. ExternalContextimpl;
  44. if(null!=(impl=(ExternalContext)this.getRequestMap().
  45. get("com.sun.faces.ExternalContextImpl"))){
  46. //noinspectionTailRecursion
  47. returnimpl.getRequestCharacterEncoding();
  48. }
  49. thrownewUnsupportedOperationException();
  50. }
  51. publicStringgetRequestContentType(){
  52. ExternalContextimpl;
  53. if(null!=(impl=(ExternalContext)this.getRequestMap().
  54. get("com.sun.faces.ExternalContextImpl"))){
  55. //noinspectionTailRecursion
  56. returnimpl.getRequestContentType();
  57. }
  58. thrownewUnsupportedOperationException();
  59. }
  60. publicStringgetResponseCharacterEncoding(){
  61. ExternalContextimpl;
  62. if(null!=(impl=(ExternalContext)this.getRequestMap().
  63. get("com.sun.faces.ExternalContextImpl"))){
  64. //noinspectionTailRecursion
  65. returnimpl.getResponseCharacterEncoding();
  66. }
  67. thrownewUnsupportedOperationException();
  68. }
  69. publicStringgetResponseContentType(){
  70. ExternalContextimpl;
  71. if(null!=(impl=(ExternalContext)this.getRequestMap().
  72. get("com.sun.faces.ExternalContextImpl"))){
  73. //noinspectionTailRecursion
  74. returnimpl.getResponseContentType();
  75. }
  76. thrownewUnsupportedOperationException();
  77. }
  78. publicabstractURLgetResource(Stringpath)throwsMalformedURLException;
  79. publicabstractInputStreamgetResourceAsStream(Stringpath);
  80. publicabstractSet<string></string>getResourcePaths(Stringpath);
  81. publicabstractObjectgetResponse();
  82. publicabstractObjectgetSession(booleancreate);
  83. publicabstractPrincipalgetUserPrincipal();
  84. publicabstractbooleanisUserInRole(Stringrole);
  85. publicabstractvoidlog(Stringmessage,Throwableexception);
  86. publicabstractvoidredirect(Stringurl)throwsIOException;
  87. }

这个抽象类共有1000多行,提供了访问外部资源的各种方法,主要是对ServletContext或是PortletContext中方法的封装,比如getRemoteUser、getRequest、getSession等方法都是很常用的,但是在运用时也要注意,如果在程序中写死是ServletContext或HttpServletRequest,那么以后对于更换到Portal环境中是不利的,这个如果需要转换的话需要注意了。
下面来看看Application对象。Application对象是应用系统范围内的单例类,提供了对FacesContext文件的对象封装,从这个对象中可以得到很多FacesContext文件中的配置,还是来看看定义吧.

  1. //-------------------------------------------------------------Properties
  2. publicabstractActionListenergetActionListener();
  3. publicabstractvoidsetActionListener(ActionListenerlistener);
  4. publicabstractvoidsetDefaultLocale(Localelocale);
  5. publicabstractStringgetDefaultRenderKitId();
  6. publicabstractvoidsetDefaultRenderKitId(StringrenderKitId);
  7. publicabstractStringgetMessageBundle();
  8. publicabstractvoidsetMessageBundle(Stringbundle);
  9. publicabstractNavigationHandlergetNavigationHandler();
  10. publicabstractvoidsetNavigationHandler(NavigationHandlerhandler);
  11. publicabstractPropertyResolvergetPropertyResolver();
  12. publicResourceBundlegetResourceBundle(FacesContextctx,Stringname){
  13. Applicationapp=getRIApplicationImpl(ctx);
  14. if(app!=null){
  15. //noinspectionTailRecursion
  16. returnapp.getResourceBundle(ctx,name);
  17. }
  18. thrownewUnsupportedOperationException();
  19. }
  20. publicabstractVariableResolvergetVariableResolver();
  21. publicabstractvoidsetVariableResolver(VariableResolverresolver);
  22. publicvoidaddELResolver(ELResolverresolver){
  23. Applicationapp=getRIApplicationImpl();
  24. if(app!=null){
  25. app.addELResolver(resolver);
  26. }else{
  27. thrownewUnsupportedOperationException();
  28. }
  29. }
  30. publicELResolvergetELResolver(){
  31. Applicationapp=getRIApplicationImpl();
  32. if(app!=null){
  33. //noinspectionTailRecursion
  34. returnapp.getELResolver();
  35. }
  36. thrownewUnsupportedOperationException();
  37. }
  38. publicabstractViewHandlergetViewHandler();
  39. publicabstractvoidsetViewHandler(ViewHandlerhandler);
  40. publicabstractStateManagergetStateManager();
  41. publicabstractvoidsetStateManager(StateManagermanager);
  42. //-------------------------------------------------------ObjectFactories
  43. publicabstractvoidaddComponent(StringcomponentType,
  44. StringcomponentClass);
  45. publicabstractUIComponentcreateComponent(StringcomponentType)
  46. throwsFacesException;
  47. publicabstractUIComponentcreateComponent(ValueBindingcomponentBinding,
  48. FacesContextcontext,
  49. StringcomponentType)
  50. rowsFacesException;
  51. publicUIComponentcreateComponent(ValueExpressioncomponentExpression,
  52. FacesContextcontext,
  53. StringcomponentType)
  54. rowsFacesException{
  55. if(null==componentExpression||null==context||
  56. null==componentType){
  57. //PENDING-i18n
  58. StringBuilderbuilder=newStringBuilder(64);
  59. builder.append("nullparameters-");
  60. builder.append("componentExpression:").append(componentExpression);
  61. builder.append(",context:").append(context);
  62. builder.append(",componentType:").append(componentType);
  63. thrownewNullPointerException(builder.toString());
  64. }
  65. Objectresult;
  66. booleancreateOne=false;
  67. try{
  68. if(null!=(result=
  69. componentExpression.getValue(context.getELContext()))){
  70. //iftheresultisnotaninstanceofUIComponent
  71. createOne=(!(resultinstanceofUIComponent));
  72. //wehavetocreateone.
  73. }
  74. if(null==result||createOne){
  75. result=this.createComponent(componentType);
  76. componentExpression.setValue((context.getELContext()),result);
  77. }
  78. }catch(ELExceptionelex){
  79. thrownewFacesException(elex);
  80. }
  81. return(UIComponent)result;
  82. }
  83. publicabstractIterator<String>getComponentTypes();
  84. publicabstractvoidaddConverter(StringconverterId,
  85. publicabstractvoidaddConverter(ClasstargetClass,
  86. StringconverterClass);
  87. publicabstractConvertercreateConverter(StringconverterId);
  88. publicabstractConvertercreateConverter(ClasstargetClass);
  89. publicabstractIterator<String>getConverterIds();
  90. publicExpressionFactorygetExpressionFactory(){
  91. Applicationapp=getRIApplicationImpl();
  92. if(app!=null){
  93. //noinspectionTailRecursion
  94. returnapp.getExpressionFactory();
  95. }
  96. thrownewUnsupportedOperationException();
  97. }
  98. publicObjectevaluateExpressionGet(FacesContextcontext,
  99. Stringexpression,
  100. ClassexpectedType)throwsELException{
  101. Applicationapp=getRIApplicationImpl(context);
  102. if(app!=null){
  103. //noinspectionTailRecursion
  104. returnapp.evaluateExpressionGet(context,expression,expectedType);
  105. }
  106. thrownewUnsupportedOperationException();
  107. }
  108. publicabstractMethodBindingcreateMethodBinding(Stringref,
  109. Classparams[])
  110. throwsReferenceSyntaxException;
  111. publicabstractIterator<Locale>getSupportedLocales();
  112. publicabstractvoidsetSupportedLocales(Collection<Locale>locales);
  113. publicvoidaddELContextListener(ELContextListenerlistener){
  114. Applicationapp=getRIApplicationImpl();
  115. if(app!=null){
  116. app.addELContextListener(listener);
  117. }else{
  118. thrownewUnsupportedOperationException();
  119. }
  120. }
  121. publicvoidremoveELContextListener(ELContextListenerlistener){
  122. Applicationapp=getRIApplicationImpl();
  123. if(app!=null){
  124. app.removeELContextListener(listener);
  125. }else{
  126. thrownewUnsupportedOperationException();
  127. }
  128. }
  129. publicELContextListener[]getELContextListeners(){
  130. Applicationapp=getRIApplicationImpl();
  131. if(app!=null){
  132. //noinspectionTailRecursion
  133. returnapp.getELContextListeners();
  134. }else{
  135. thrownewUnsupportedOperationException();
  136. }
  137. }
  138. publicabstractvoidaddValidator(StringvalidatorId,
  139. StringvalidatorClass);
  140. publicabstractValidatorcreateValidator(StringvalidatorId)
  141. throwsFacesException;
  142. publicabstractIterator<String>getValidatorIds();
  143. publicabstractValueBindingcreateValueBinding(Stringref)
  144. throwsReferenceSyntaxException;
  145. //---------------------------------------------------------PrivateMethods
  146. privatestaticApplicationgetRIApplicationImpl(FacesContextcontext){
  147. ExternalContextextContext;
  148. if(context!=null){
  149. extContext=context.getExternalContext();
  150. }else{
  151. extContext=
  152. FacesContext.getCurrentInstance().getExternalContext();
  153. }
  154. if(extContext!=null){
  155. return((Application)extContext.getApplicationMap().
  156. get("com.sun.faces.ApplicationImpl"));
  157. }
  158. returnnull;
  159. }
  160. privatestaticApplicationgetRIApplicationImpl(){
  161. returngetRIApplicationImpl(null);
  162. }

你可能感兴趣的:(lifecycle)