• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Re-initializing a Session Managed Bean

 
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Richard Green:



I meant using the current Session Bean and reinitializing it
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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??
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Hey! You're stepping on my hand! Help me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic