JSF的生命周期在JSF应用中起着至关重要的作用,每一个JSF请求的处理都需要经过一次生命周期,本文从源码的角度分析JSF的生命周期。
在讨论生命周期之前,我们先要讨论FacesContext的一些元素,他们在整个生命周期中扮演了非常重要的角色。么个JSF应用必须保存它所处理的请求信息,FacesContext为处理请求和生成响应保存了所有必需的上下文信息,具体而言,它包括:
1.信息队列,MessageQueue,保存所有的消息
2.当前的组件树,ViewRoot,
3.外部上下文,ExternalContext
4.Application。
下面就是Sun的FacesContextImpl中的变量:
这里面有很多重要的对象值得我们去研究,按照从上到下的顺序,我们先来看看ExternalContext。
ExternalContext其实是对ServletContext(或PortletContext)的封装,提供了访问外部容器资源的各种方法,ExternalContext基类定义如下:
在讨论生命周期之前,我们先要讨论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 Methods
- 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"))) {
- //noinspection TailRecursion
- return impl.getRequestCharacterEncoding();
- }
- throw new UnsupportedOperationException();
- }
- public String getRequestContentType() {
- ExternalContext impl;
- if (null != (impl = (ExternalContext) this.getRequestMap().
- get("com.sun.faces.ExternalContextImpl"))) {
- //noinspection TailRecursion
- return impl.getRequestContentType();
- }
- throw new UnsupportedOperationException();
- }
- public String getResponseCharacterEncoding() {
- ExternalContext impl;
- if (null != (impl = (ExternalContext) this.getRequestMap().
- get("com.sun.faces.ExternalContextImpl"))) {
- //noinspection TailRecursion
- return impl.getResponseCharacterEncoding();
- }
- throw new UnsupportedOperationException();
- }
- public String getResponseContentType() {
- ExternalContext impl;
- if (null != (impl = (ExternalContext) this.getRequestMap().
- get("com.sun.faces.ExternalContextImpl"))) {
- //noinspection TailRecursion
- 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文件中的配置,还是来看看定义吧.
- // ------------------------------------------------------------- Properties
- 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) {
- //noinspection TailRecursion
- 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) {
- //noinspection TailRecursion
- 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);
- // ------------------------------------------------------- Object Factories
- 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) {
- // PENDING - i18n
- 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()))) {
- // if the result is not an instance of UIComponent
- createOne = (!(result instanceof UIComponent));
- // we have to create one.
- }
- 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) {
- //noinspection TailRecursion
- return app.getExpressionFactory();
- }
- throw new UnsupportedOperationException();
- }
- public Object evaluateExpressionGet(FacesContext context,
- String expression,
- Class expectedType) throws ELException {
- Application app = getRIApplicationImpl(context);
- if (app != null) {
- //noinspection TailRecursion
- 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) {
- //noinspection TailRecursion
- 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 Methods
- 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);
- }
未完待续。