Managed Beans - Scopes
Existen diferentes niveles o alcances de ManagedBeans,Request Scope
Se instancia y se pobla el Bean en cada request, con información de Request.package com.roshka.tutorial.jsf; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean(name = "requestBean") @RequestScoped public class RequestManagedBean { private static org.apache.log4j.Logger logger = org.apache.log4j.Logger .getLogger(RequestManagedBean.class); Form form; public RequestManagedBean() { logger.info("RequestManagedBean.RequestManagedBean: constructor"); } @PostConstruct public void postConstruct() { logger.info("RequestManagedBean.postConstruct: "); form = new Form(); form.setName("Name..."); } public String save() { logger.info("RequestManagedBean.save: "); logger.info("RequestManagedBean.save: " + form.getName()); return null; } public String save(Form form) { logger.info("RequestManagedBean.save: "); this.form = form; return null; } public Form getForm() { return form; } public void setForm(Form form) { this.form = form; } }
View Scope (Desde JSF 2)
En el primer GET al VIEW se instancia el BEAN, se guarda la instancia en la Sesión del usuario pero solo puede ser accedida desde el mismo VIEW.package com.roshka.tutorial.jsf; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; @ManagedBean(name = "viewBean") @ViewScoped public class ViewManagedBean { private static org.apache.log4j.Logger logger = org.apache.log4j.Logger .getLogger(ViewManagedBean.class); Form form; public ViewManagedBean() { logger.info("ViewManagedBean.ViewManagedBean: constructor"); } @PostConstruct public void postConstruct() { logger.info("ViewManagedBean.postConstruct: "); form = new Form(); form.setName("Name..."); } public String save() { logger.info("ViewManagedBean.save: "); logger.info("ViewManagedBean.save: " + form.getName()); return null; } public String save(Form form) { logger.info("ViewManagedBean.save: "); this.form = form; return null; } public Form getForm() { return form; } public void setForm(Form form) { this.form = form; } }
Session Scope
Se crea una instancia por Sesión y para todas las Views se comparte la misma instanciapackage com.roshka.tutorial.jsf; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name = "sessionBean") @SessionScoped public class SessionManagedBean { private static org.apache.log4j.Logger logger = org.apache.log4j.Logger .getLogger(SessionManagedBean.class); Form form; public SessionManagedBean() { logger.info("SessionManagedBean.SessionManagedBean: constructor "); } @PostConstruct public void postConstruct() { logger.info("SessionManagedBean.SessionManagedBean: "); form = new Form(); form.setName("Name..."); } public String save() { logger.info("SessionManagedBean.save: "); logger.info("SessionManagedBean.save: " + form.getName()); return null; } public String save(Form form) { logger.info("SessionManagedBean.save: "); this.form = form; return null; } public Form getForm() { return form; } public void setForm(Form form) { this.form = form; } }
ApplicationScope
Se crea una instancia para la aplicación, Se comparte entre todos los Views de Todas las Sesiones. Ojo! No utilizar para guardar información sensible de un usuario.package com.roshka.tutorial.jsf; import javax.annotation.PostConstruct; import javax.faces.bean.ApplicationScoped; import javax.faces.bean.ManagedBean; @ManagedBean(name = "applicationBean") @ApplicationScoped public class ApplicationManagedBean { private static org.apache.log4j.Logger logger = org.apache.log4j.Logger .getLogger(ApplicationManagedBean.class); Form form; public ApplicationManagedBean() { logger.info("ApplicationManagedBean.ApplicationManagedBean: constructor"); } @PostConstruct public void postConstruct() { logger.info("ApplicationManagedBean.postConstruct: "); form = new Form(); form.setName("Name..."); } public String save() { logger.info("ApplicationManagedBean.save: "); logger.info("ApplicationManagedBean.save: " + form.getName()); return null; } public String save(Form form) { logger.info("ApplicationManagedBean.save: "); this.form = form; return null; } public Form getForm() { return form; } public void setForm(Form form) { this.form = form; } }
Se puede ver utilizando los ejemplos
request-scope/example.xhtmlview-scope/example.xhtml
session-scope/example.xhtml
application-scope/example.xhtml
all-scopes/example.xhtml
Save Views State
Para guardar la información del Estado de del View (ViewSate) se pueden seleccionar entre guardar en el Servidor o Guardar en el Servidor; Guardar en el Servidor implica consumo de memoria y Guardar en el Cliente implica mayor tráfico de Red.Server Side
<context-param> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>server</param-value> </context-param>
Client Side
<context-param><param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param>
Enlace Interesante
Para conocer más acerca de Save State, se puede leer la siguiente discusión stackoverflow.comDescargar ejemplos desde Aquí
No hay comentarios:
Publicar un comentario