This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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
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.
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
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
posted
0
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
posted
0
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
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.