wood burning stoves*
The moose likes JSF and the fly likes Re-initializing a Session Managed Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Re-initializing a Session Managed Bean" Watch "Re-initializing a Session Managed Bean" New topic
Author

Re-initializing a Session Managed Bean

Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
Lets say a managed bean is defined with session scope, that means its only initialized once during a session and also the values it holds stays "alive" during the session, lets say during that session, one wants to clear all those value set in the Managed bean that is have a new bean, like calling a new Bean initializing the constructor once again, Is this possible in JSF. except ofcourse invalidating the session
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
Originally posted by Richard Green:


I meant using the current Session Bean and reinitializing it
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
Originally posted by Abiodun Adisa:


I meant using the current Session Bean and reinitializing it


hmmm.... make your session beans implement an interface that has reinitialize() method in it.

grab your managed bean out of the session and inovke the reinitialize method??
Joe ONeil
Greenhorn

Joined: Jan 16, 2009
Posts: 12
add these to your FacesUtil file

private static String getJsfEl(String value) {
return "#{" + value + "}";
}

public static void resetManagedBean(String beanName) {
getValueBinding(getJsfEl(beanName)).setValue(
FacesContext.getCurrentInstance(), null);
}

and reference this example
http://www.javaworld.com/javaworld/jw-07-2004/jw-0719-jsf.html
probably all the jsf applications in the world have this as thier basic design
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Abiodun Adisa wrote:
Originally posted by Richard Green:


I meant using the current Session Bean and reinitializing it
Either you didn't understood his suggestion, or you stated your problem not clearly enough.

Where exactly do you want to "reinitialize" it? What exactly do you expect of "reinitializing"?

His line of code shows how to replace the session scoped managed bean with the given name with a new instance. And as far I understand your topicstart, this is a valid answer.
Joe ONeil
Greenhorn

Joined: Jan 16, 2009
Posts: 12
Set the initial values you want for the session bean in the constructor or an init method that is called by the constructor
When the bean is reset it you are basically destroying the bean and a new one is created and the constructor is fired again or you can give the properties default values
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964
    
  19

Usually - and this is in general Java code, not just JSF - if I have a bean I want to initialize more than once, I make a method named init() and have the constructor(s) invoke it.

The same approach works for JSF. One of the biggest problems I've found with JSF, in fact is that people insist on making esoteric complicated solutions to problems that JSF was designed to make simple.

That's not to say that there aren't some "gotchas". When you invoke init() from an action method in JSF, don't expect the bean to be reset and then set to properties from the associated view. The JSF lifecycle default operation would call the setters first and then invoke init(), wiping out the work of the setters. For a case like that, you'd need to cache data as it came in from the setters and then have the init method restore the newly-set values from the cached data. Other possibilities exist, but this is an example of how simple things can be if you allow them to be so.


Customer surveys are for companies who didn't pay proper attention to begin with.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Besides, there shouldn´t be any need to do so. The session scope is apparently too broad for you. Check if h:inputHidden, or t:saveState, or a4j:keepAlive can help you.
Joe ONeil
Greenhorn

Joined: Jan 16, 2009
Posts: 12
Different scenarios require different approaches. You can reset the bean and page By creating a loadSomething() method this will be called after the bean is constructed and and then will forward the page to itself

Public String Reset(){
YourSessionBean = new YourSessionBean();
YourBean.loadSomething();// this method will set all your default values
Return “Page_u_are_On”;


This will do a total page refresh with new values

Now if You using partial page rendering say with a changeValueListener or action method I will Bind the component and reset the values in the java code also so that the page and bean are refreshed(but not the whole page)

Class Abc{
HtmlInputText xComponent;
String testComp;

//I can do this in an action
public void Reset(){
YourSessionBean = new YourSessionBean();
YourBean.loadSomething();

xComponent.setValue(“anyvalue”);
}


public HtmlInputText getXComponent() {
return xComponent;
}
public void setXComponent(HtmlInputText component) {
xComponent = component;
}
public String getTestComp() {
return testComp;
}
public void setTestComp(String testComp) {
this.testComp = testComp;
}


This is JSP /or XML Page
<t:inputText binding=”#{abc.xComponent}” value="#{abc.testComp}"/>

The init method will only work if the default values do not affect any of the Component on the JSF page


dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Hi, I am pretty new to JSF. I am curious whether any of the suggested methods are considered best practice. Our approach has been to call the init method on the bean in a performAction method, but the code isn't exactly what I would call elegant.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Re-initializing a Session Managed Bean
 
Similar Threads
few doubts in MVC design pattern and EJB Concepts
Managed Bean Life cycle
Obtaining an instance of a Managed Bean declared with Application Level Scope
Transaction between 2 methods in the same bean
How to keep variable value alive in JSF