生活随笔
收集整理的這篇文章主要介紹了
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:
//?Relationship?Instance?Variables ?? ?????private?ResponseStream?responseStream?=?null; ?? ?????private?ResponseWriter?responseWriter?=?null; ?? ?????private?ExternalContext?externalContext?=?null; ?? ?????private?Application?application?=?null; ?? ?????private?UIViewRoot?viewRoot?=?null; ?? ?????private?ELContext?elContext?=?null; ?? ?????private?RenderKitFactory?rkFactory; ?? ?????private?RenderKit?lastRk; ?? ?????private?String?lastRkId; ?? ?? ?????/** ?? ??????*?Store?mapping?of?clientId?to?ArrayList?of?FacesMessage ?? ??????*?instances.??The?null?key?is?used?to?represent?FacesMessage?instances ?? ??????*?that?are?not?associated?with?a?clientId?instance. ?? ??????*/ ?? ?????private?Map<String,List<FacesMessage>>?componentMessageLists; ?? ?????//?Attribute?Instance?Variables ?? ?? ?????private?boolean?renderResponse?=?false; ?? ?????private?boolean?responseComplete?=?false; ?? 這里面有很多重要的對象值得我們去研究,按照從上到下的順序,我們先來看看ExternalContext。
ExternalContext其實是對ServletContext(或PortletContext)的封裝,提供了訪問外部容器資源的各種方法,ExternalContext基類定義如下:
javax.faces.context.ExternalContext:
public?abstract?class?ExternalContext?{?? ???? ????? ????public?static?final?String?BASIC_AUTH?=?"BASIC";?? ????public?static?final?String?CLIENT_CERT_AUTH?=?"CLIENT_CERT";?? ????public?static?final?String?DIGEST_AUTH?=?"DIGEST";?? ????public?static?final?String?FORM_AUTH?=?"FORM";?? ?? ?????? ????public?abstract?void?dispatch(String?path)?? ????throws?IOException;?? ????public?abstract?String?encodeActionURL(String?url);?? ?????? ????public?abstract?String?encodeNamespace(String?name);?? ????public?abstract?String?encodeResourceURL(String?url);?? ????public?abstract?String?getAuthType();?? ??? public?abstract?Object?getContext();?? ????public?abstract?String?getInitParameter(String?name);?? ????public?abstract?Map?getInitParameterMap();?? ????public?abstract?String?getRemoteUser();?? ????public?abstract?Object?getRequest();?? ????public?void?setRequest(Object?request)?{?? ????????ExternalContext?impl;?? ????????if?(null?!=?(impl?=?(ExternalContext)?this.getRequestMap().?? ????????????????get("com.sun.faces.ExternalContextImpl")))?{?? ????????????impl.setRequest(request);?? ????????????return;?? ????????}?? ?????????? ????????throw?new?UnsupportedOperationException();?? ????}?? ????public?void?setRequestCharacterEncoding(String?encoding)?throws?UnsupportedEncodingException?{?? ????????ExternalContext?impl;?? ????????if?(null?!=?(impl?=?(ExternalContext)?this.getRequestMap().?? ????????????????get("com.sun.faces.ExternalContextImpl")))?{?? ????????????impl.setRequestCharacterEncoding(encoding);?? ????????????return;?? ????????}?? ??????? throw?new?UnsupportedOperationException();?? ????}?? ????public?abstract?String?getRequestContextPath();?? ????public?abstract?Locale?getRequestLocale();?? ????public?abstract?Iterator<locale></locale>?getRequestLocales();?? ????public?abstract?Iterator<string></string>?getRequestParameterNames();?? ????public?abstract?String?getRequestPathInfo();?? ????public?abstract?String?getRequestServletPath();?? ?????? ????public?String?getRequestCharacterEncoding()?{?? ????????ExternalContext?impl;?? ????????if?(null?!=?(impl?=?(ExternalContext)?this.getRequestMap().?? ????????????????get("com.sun.faces.ExternalContextImpl")))?{?? ?????????????? ????????????return?impl.getRequestCharacterEncoding();?? ????????}?? ????????throw?new?UnsupportedOperationException();?? ????}?? ?? ????public?String?getRequestContentType()?{?? ????????ExternalContext?impl;?? ????????if?(null?!=?(impl?=?(ExternalContext)?this.getRequestMap().?? ????????????????get("com.sun.faces.ExternalContextImpl")))?{?? ?????????????? ????????????return?impl.getRequestContentType();?? ????????}?? ?? ????????throw?new?UnsupportedOperationException();?? ????}?? ?? ????public?String?getResponseCharacterEncoding()?{?? ????????ExternalContext?impl;?? ????????if?(null?!=?(impl?=?(ExternalContext)?this.getRequestMap().?? ????????????????get("com.sun.faces.ExternalContextImpl")))?{?? ?????????????? ????????????return?impl.getResponseCharacterEncoding();?? ????????}?? ????????? throw?new?UnsupportedOperationException();?? ????}?? ?? ?????public?String?getResponseContentType()?{?? ????????ExternalContext?impl;?? ????????if?(null?!=?(impl?=?(ExternalContext)?this.getRequestMap().?? ????????????????get("com.sun.faces.ExternalContextImpl")))?{?? ?????????????? ????????????return?impl.getResponseContentType();?? ????????} ????????throw?new?UnsupportedOperationException();?? ????}?? ????public?abstract?URL?getResource(String?path)?throws?MalformedURLException;?? ????public?abstract?InputStream?getResourceAsStream(String?path);?? ????public?abstract?Set<string></string>?getResourcePaths(String?path);?? ????public?abstract?Object?getResponse();?? ????public?abstract?Object?getSession(boolean?create);?? ????public?abstract?Principal?getUserPrincipal();?? ????public?abstract?boolean?isUserInRole(String?role);?? ????public?abstract?void?log(String?message,?Throwable?exception);?? ?? ?public?abstract?void?redirect(String?url)?throws?IOException;?? }?? 這個抽象類共有1000多行,提供了訪問外部資源的各種方法,主要是對ServletContext或是PortletContext中方法的封裝,比如getRemoteUser、getRequest、getSession等方法都是很常用的,但是在運用時也要注意,如果在程序中寫死是ServletContext或HttpServletRequest,那么以后對于更換到Portal環境中是不利的,這個如果需要轉換的話需要注意了。
??? 下面來看看Application對象。Application對象是應用系統范圍內的單例類,提供了對FacesContext文件的對象封裝,從這個對象中可以得到很多FacesContext文件中的配置,還是來看看定義吧.
??? ?? ?public?abstract?ActionListener?getActionListener(); ?? ?? ?? ?public?abstract?void?setActionListener(ActionListener?listener); ?? ?? ?? ?public?abstract?void?setDefaultLocale(Locale?locale); ?? ?? ?public?abstract?String?getDefaultRenderKitId(); ?? ?? ?? ?public?abstract?void?setDefaultRenderKitId(String?renderKitId); ?? ?? ?public?abstract?String?getMessageBundle(); ?? ?? ?? ?public?abstract?void?setMessageBundle(String?bundle); ?? ?? ?? ?public?abstract?NavigationHandler?getNavigationHandler(); ?? ?? ?? ?public?abstract?void?setNavigationHandler(NavigationHandler?handler); ?? ?? ?? ?public?abstract?PropertyResolver?getPropertyResolver(); ?? ?? ?? ? ?? ?public?ResourceBundle?getResourceBundle(FacesContext?ctx,?String?name)?{ ?? ?????Application?app?=?getRIApplicationImpl(ctx); ?? ?????if?(app?!=?null)?{ ?? ??????????? ?????????return?app.getResourceBundle(ctx,?name); ?? ?????} ?? ????? ?? ?????throw?new?UnsupportedOperationException(); ?? ?} ?? ?? ?public?abstract?VariableResolver?getVariableResolver(); ?? ?? ?? ?public?abstract?void?setVariableResolver(VariableResolver?resolver); ?? ?? ?? ?public?void?addELResolver(ELResolver?resolver)?{ ?? ?????Application?app?=?getRIApplicationImpl(); ?? ?????if?(app?!=?null)?{ ?? ?????????app.addELResolver(resolver); ?? ?????}?else?{ ?? ?????????throw?new?UnsupportedOperationException(); ?? ?????} ?? ?} ?? ?? ?public?ELResolver?getELResolver()?{ ?? ?????Application?app?=?getRIApplicationImpl(); ?? ?????if?(app?!=?null)?{ ?? ??????????? ?????????return?app.getELResolver(); ?? ?????} ?? ?????throw?new?UnsupportedOperationException(); ?? ?} ?? ?? ?? ?public?abstract?ViewHandler?getViewHandler(); ?? ?? ?public?abstract?void?setViewHandler(ViewHandler?handler); ?? ?? ?public?abstract?StateManager?getStateManager(); ?? ?? ?public?abstract?void?setStateManager(StateManager?manager); ?? ?? ?? ??? ?? ?public?abstract?void?addComponent(String?componentType, ?? ???????????????????????????????????String?componentClass); ?? ?? ?? ?public?abstract?UIComponent?createComponent(String?componentType) ?? ?????throws?FacesException; ?? ?? ?? ?public?abstract?UIComponent?createComponent(ValueBinding?componentBinding, ?? ?????????????????????????????????????????????FacesContext?context, ?? ?????????????????????????????????????????????String?componentType) ?? rows?FacesException; ?? ?? ?public?UIComponent?createComponent(ValueExpression?componentExpression, ?? ????????????????????????????????????FacesContext?context, ?? ????????????????????????????????????String?componentType) ?? rows?FacesException?{ ?? ?????if?(null?==?componentExpression?||?null?==?context?|| ?? ?????????null?==?componentType)?{ ?? ?????????? ?????????StringBuilder?builder?=?new?StringBuilder(64); ?? ?????????builder.append("null?parameters?-?"); ?? ?????????builder.append("componentExpression:?").append(componentExpression); ?? ?????????builder.append(",?context:?").append(context); ?? ?????????builder.append(",?componentType:?").append(componentType); ?? ?????????throw?new?NullPointerException(builder.toString()); ?? ?????} ?? ?? ?????Object?result; ?? ?????boolean?createOne?=?false; ?? ?? ?????try?{ ?? ?????????if?(null?!=?(result?=? ?? ?????????????componentExpression.getValue(context.getELContext())))?{ ?? ??????????????? ?????????????createOne?=?(!(result?instanceof?UIComponent)); ?? ??????????????? ?????????} ?? ?????????if?(null?==?result?||?createOne)?{ ?? ?????????????result?=?this.createComponent(componentType); ?? ?????????????componentExpression.setValue((context.getELContext()),?result); ?? ?????????} ?? ?????}?catch?(ELException?elex)?{ ?? ?????????throw?new?FacesException(elex); ?? ?????} ?? ?? ?????return?(UIComponent)?result;???? ?? ?} ?? ?? ?? ?public?abstract?Iterator<String>?getComponentTypes(); ?? ?? ?? ?public?abstract?void?addConverter(String?converterId,? ?? ?? ?public?abstract?void?addConverter(Class?targetClass, ?? ???????????????????????????????????String?converterClass); ?? ?? ?public?abstract?Converter?createConverter(String?converterId); ?? ?? ?? ?public?abstract?Converter?createConverter(Class?targetClass); ?? ?? ?? ?public?abstract?Iterator<String>?getConverterIds(); ?? ?? ?? ?? ?public?ExpressionFactory?getExpressionFactory()?{ ?? ?????Application?app?=?getRIApplicationImpl(); ?? ?????if?(app?!=?null)?{ ?? ??????????? ?????????return?app.getExpressionFactory(); ?? ?????} ?? ?? ?????throw?new?UnsupportedOperationException(); ?? ?} ?? ?? ?? ?? ?public?Object?evaluateExpressionGet(FacesContext?context, ?? ?????????????????????????????????????String?expression, ?? ?????????????????????????????????????Class?expectedType)?throws?ELException?{ ?? ?????Application?app?=?getRIApplicationImpl(context); ?? ?????if?(app?!=?null)?{ ?? ??????????? ?????????return?app.evaluateExpressionGet(context,?expression,?expectedType); ?? ?????} ?? ?????throw?new?UnsupportedOperationException(); ?? ?} ?? ?? ?public?abstract?MethodBinding?createMethodBinding(String?ref, ?? ???????????????????????????????????????????????????Class?params[]) ?? ?????throws?ReferenceSyntaxException; ?? ?? ?public?abstract?Iterator<Locale>?getSupportedLocales(); ?? ?? ?? ?public?abstract?void?setSupportedLocales(Collection<Locale>?locales); ?? ?? ?? ?public?void?addELContextListener(ELContextListener?listener)?{ ?? ?????Application?app?=?getRIApplicationImpl(); ?? ?????if?(app?!=?null)?{ ?? ?????????app.addELContextListener(listener); ?? ?????}?else?{ ?? ?????????throw?new?UnsupportedOperationException(); ?? ?????} ?? ?} ?? ?? ?public?void?removeELContextListener(ELContextListener?listener)?{ ?? ?????Application?app?=?getRIApplicationImpl(); ?? ?????if?(app?!=?null)?{ ?? ?????????app.removeELContextListener(listener); ?? ?????}?else?{ ?? ?????????throw?new?UnsupportedOperationException(); ?? ?????} ?? ?? ?} ?? ?? ?public?ELContextListener?[]?getELContextListeners()?{ ?? ?????Application?app?=?getRIApplicationImpl(); ?? ?????if?(app?!=?null)?{ ?? ??????????? ?????????return?app.getELContextListeners(); ?? ?????}?else?{ ?? ?????????throw?new?UnsupportedOperationException(); ?? ?????} ?? ?} ?? ?? public?abstract?void?addValidator(String?validatorId,? ?? ??????????String?validatorClass); ?? ?? public?abstract?Validator?createValidator(String?validatorId) ?? ?????throws?FacesException; ?? ?? public?abstract?Iterator<String>?getValidatorIds(); ?? public?abstract?ValueBinding?createValueBinding(String?ref) ?? ?????throws?ReferenceSyntaxException; ?? ?? ?? ??? ?? ?? ?private?static?Application?getRIApplicationImpl(FacesContext?context)?{ ?? ?????ExternalContext?extContext; ?? ?????if?(context?!=?null)?{ ?? ?????????extContext?=?context.getExternalContext(); ?? ?????}?else?{ ?? ?????????extContext?= ?? ??????????????FacesContext.getCurrentInstance().getExternalContext(); ?? ?????} ?? ?????if?(extContext?!=?null)?{ ?? ?????????return?((Application)?extContext.getApplicationMap(). ?? ??????????????get("com.sun.faces.ApplicationImpl")); ?? ?????} ?? ?????return?null; ?? ?} ?? ?? ?private?static?Application?getRIApplicationImpl()?{ ?? ?????return?getRIApplicationImpl(null); ?? ?} ??
總結
以上是生活随笔為你收集整理的JSF 源代码赏析之Lifecycle的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。