`

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. }
分享到:
评论

相关推荐

    JavaEE源代码 jsf-api

    JavaEE源代码 jsf-apiJavaEE源代码 jsf-apiJavaEE源代码 jsf-apiJavaEE源代码 jsf-apiJavaEE源代码 jsf-apiJavaEE源代码 jsf-apiJavaEE源代码 jsf-apiJavaEE源代码 jsf-apiJavaEE源代码 jsf-apiJavaEE源代码 jsf-...

    JavaEE源代码 jsf-impl

    JavaEE源代码 jsf-implJavaEE源代码 jsf-implJavaEE源代码 jsf-implJavaEE源代码 jsf-implJavaEE源代码 jsf-implJavaEE源代码 jsf-implJavaEE源代码 jsf-implJavaEE源代码 jsf-implJavaEE源代码 jsf-implJavaEE源...

    Jsf 项目源代码

    sun 最新框架,jsf写的j2ee项目代码

    JSF上传 JSF大文件上传 JSF上传代码 JSF上传源代码

    自己写的JSF文件上传项目,可以最大支持最大1.99G文件 需要的jar包需奥自己加 附:jar目录截图

    JSF2.0源代码

    JSF2.0源代码,具体版本是2.03。但与其它版本差别不太,适用下不了源码的GGMM。

    jsf 源代码

    jsf 新生的 web 开发框架

    JSF工程实例源代码

    JSF工程实例源代码,包含实现文件上传与下载的全部源代码和文档,数据库使用oracle10g

    JSF实例源代码下载

    jsf教程实例的源代码,有利于读者更好的学习。

    jsf1.2源代码下载

    jsf1.2源代码。可用myeclipse关联起来查看里边源码。

    Core JSF源代码

    Core JSF(JSF核心技术)就不用我介绍了吧,这是该书的源码

    JSF入门实例 源代码

    一个小例子适合jsf入门者。本人自己敲的代码 完全可用 启动服务器(tomcat)部署之后 输入:http://localhost:8080/HelloJsf/HelloWorld.faces就可以了

    JSF1.2.07版源代码

    jsf-1_2_07-src

    JSF国际化范例 源代码

    JSF国际化范例,源代码 JSF国际化范例,源代码 JSF国际化范例,源代码

    richface and jsf 源代码

    richface,jsf源码。相当不错的资料。特别是richface.里面有不错的例子。放到tomcat下可以直接运行啊。

    JSF(java server faces)开源框架的源代码

    JSF的源代码,即是jsf本身框架的源代码,通过阅读可以更好地学习JSF,深入理解它的精髓。

    JSF 动态生成网页例子源代码

    JSP(Java Server Pages)是由Sun Microsystems公司倡导、许多公司参与一起建立的一种...JSP技术有点类似ASP技术,它是在传统的网页HTML文件(*.htm,*.html)中插入Java程序段(Scriptlet)和JSP标记(tag),从而形成JSP文档

    完整的jsf博客源代码

    完整的jsf博客例子,使用derby数据库,展示一个典型的博客,适合初学者学习jsf,是一个不错的学习例子,使用时只需要导入到eclipse中,把derby数据库的服务启动即可

    JSF IN ACTION 源代码

    JSF IN ACTION (源代码) 以前上传的书的源代码.......IN Action书的代码都可以在Manning 的官方网站(www.manning.com)上可以下载得到..希望大家喜欢

    jsf完全参考手册源代码

    jsf完全参考手册源代码

Global site tag (gtag.js) - Google Analytics