You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by bansi <ma...@yahoo.com> on 2007/06/14 00:47:52 UTC

How to Re-Initialize JSF Backing Bean on Form Submit

We have backing bean defined in "session" scope
So whenever we do a submit on JSF Form, it holds onto same backing bean.
This is not desirable as
-> The Form will have  different set of values each time it does a submit 
-> The Backing bean has variable defined to instantiate a POJO i.e.private
MyPojo pojo = new MyPojo();
So every time JSF form submits to the backing bean, it holds onto the same
instance of POJO which eventually results in insertion problems into
database i.e. having same Identifier (ID) value
-> The same problem occurs if i navigate to different page and come back to
original page

Is their a way to re-initialize the Backing Bean ???
-- 
View this message in context: http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11110517
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: How to Re-Initialize JSF Backing Bean on Form Submit

Posted by bchinc <bc...@hotmail.com>.
I did reply to authoer by mistake instead of reply.
Any ways...
Putting the gist of my message below for the forum.

I was able to reset initial view with 
                                FacesContext ctx = getFacesCTX(request,
response);
				UIViewRoot newView = ctx
						.getApplication()
						.getViewHandler()
						.createView(ctx,
								"/jsp/html/UserRgnView.jsp");
				ctx.setViewRoot(newView);

This snippet is in doView() of my portlet. I do not want it to get invoked
everytime doView() is called.
In a way I need to be able to differentiate between 'initial or postback'
request and a request from another portal page (from outside of my jsf
portlet app).
Presently I do it by setting a render parameter in my processAction and
accessing it in doView().
If this parameter is set, it means request is postback request, else it
means it is either initial request or request from outside of my jsf-portlet
app, then I force the initial view.
Any other approach or simple solution is appreciated.

-- 
View this message in context: http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11242268
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: How to Re-Initialize JSF Backing Bean on Form Submit

Posted by Andrew Robinson <an...@gmail.com>.
I think there is definitely some confusion going on here. I am not
100% sure I am understanding you fully, so please forgive any possibly
incorrect conclusions, but let me see if I can address your issues.
Please also note that I have only worked with servlets and not
portlets.

State saving:
CLIENT vs. SERVER are the same thing in terms of functionality. The
state that is saved is the data that is returned from the
UIComponent.saveState() method. This information is used during the
RESTORE_VIEW phase to reconstruct the component tree. That
reconstructed component tree is then iterated over for decoding,
validating, updating and then rendering. This state has *nothing* to
do with backing bean scope.

The choice of CLIENT vs. SERVER is a choice of performance. CLIENT
side state means that the component tree's state is serialized into an
<INPUT TYPE="hidden" /> HTML control that is submitted to JSF via the
FORM submittal. SERVER side state means that up to X views (20 by
default), the state is saved into the user's HttpSession object. When
the user submits a view, the server attempts to locate the state for
the component tree in the user's session. If found, the component tree
is rebuilt and de-serialized from this information. So the performance
choice is a harder hit on the network and the client (client side
state) or a much larger hit on the application server's RAM/heap
(server side state)

Managed, aka Backing, Bean state

The JSF spec supports request and session, and a few 3rd parties have
added the support for an in between state of conversation. These beans
live in the faces context. For request beans, the beans are stored
into the request object and therefore released and eventually garbage
collected once the request is finished. They are created the first
time that someone accesses them using the VariableMapper (EL
expression).

Session beans are stored into the HttpSession and therefore should
implement java.io.Serializable so that in a clustered environment,
these beans can be used from each server within the cluster. The
beans, like request beans, are created on first use, but since they
are stored in the HttpSession, they are never re-created (except of
course when the user's session expires due to programmatic termination
or inactivity timeout, or the user deletes the cookie).

Okay, with all that iterated through, let me try to address your comments...

> I will list 2 scenarios: I have a jsf-portlet application for non
> authenticated users, say something like user registeration. Then for obvious
> reasons, I cannot have anything in session scope.

You can use session scope just fine, why do you think you can't? At
least with servlets, an HttpSession is keyed by a session ID that is
typically saved in an in-memory cookie in the browser. The user does
not have to be in HTTPS or authenticated for servlet sessions to work
(it is tied to the browser window, not the user). I use conversation
scope beans with JBossSeam with user registration wizards (I use
conversations, because I prefer to have my beans released after the
user has finished registering instead of waiting until the HttpSession
is destroyed -- otherwise my user's session would take up too much
RAM).

FYI, If the user has turned cookies off, the HttpSession key is
appended to the query string.

> I have to have javax.faces.STATE_SAVING_METHOD as client. Otherwise I do not
> get user entered values.

Why? If you used server state, the view is tied to the user's http
session (cookie based remember). Either client or server would work
here. The only time you loose the form's data (user entered
information) is if you do not submit the form (like <h:outputLink/>
instead of using <h:commandLink/>)

> Second- For a similar application for logged in users, I can have
> javax.faces.STATE_SAVING_METHOD as server. And when it is server, jsf
> implementation is storing the state information in session on server.

Same answer as before, doesn't matter if the user is logged on or not,
sessions are browser window based, not user based.

> Now it puzzles me when they say that they save the state in session. If it
> is so, then why would JSF reinitialize when a request comes from outside jsf
> application, and it is neither initial nor postback request.

Session backing beans are stored in the session. The serialized views
are also stored there only if you are using server side state. When
the user does a POST back to the server (with server side state
saving), the JSF engine attempts to see if that is a submit back to
the same view. If so, it attempts to restore the view from the
HttpSession of the user. If the view is not found, a new view is
created (I'm pretty sure, but I didn't double check with the spec).

-Andrew

Re: How to Re-Initialize JSF Backing Bean on Form Submit

Posted by bchinc <bc...@hotmail.com>.
Will it work even with request scope? For me it is not working.

I will list 2 scenarios: I have a jsf-portlet application for non
authenticated users, say something like user registeration. Then for obvious
reasons, I cannot have anything in session scope. Registration is  3 step
process, and to be able to get user entered values across all 3 views, I
have to have javax.faces.STATE_SAVING_METHOD as client. Otherwise I do not
get user entered values.

Second- For a similar application for logged in users, I can have
javax.faces.STATE_SAVING_METHOD as server. And when it is server, jsf
implementation is storing the state information in session on server. 

Now it puzzles me when they say that they save the state in session. If it
is so, then why would JSF reinitialize when a request comes from outside jsf
application, and it is neither initial nor postback request.

Is this a bug with my jsf-portlet implementation? Or my wrong understanding
of JSF lifecycle?
-- 
View this message in context: http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11216276
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: How to Re-Initialize JSF Backing Bean on Form Submit

Posted by Andrew Robinson <an...@gmail.com>.
"So when i move back & forth (i.e. navigate) from Current JSF page to Other
page and comeback to Current JSF page it should re-initialize/re-create bean"

That is what request scope is

On 6/14/07, bansi <ma...@yahoo.com> wrote:
>
> The requirement is to have backing bean in session scope.
> So when i move back & forth (i.e. navigate) from Current JSF page to Other
> page and comeback to Current JSF page it should re-initialize/re-create bean
> instance and ofcourse it has to clear of the data on form fields of current
> page
>
> I tried doing this in my PhaseListener but it doesnt re-create/re-initialize
> the bean instance (i.e. it doesn't call the constructor in the bean)
>
> public void afterPhase(PhaseEvent pe)
>
> {
>
> FacesContext facesContext = pe.getFacesContext();
>
> String viewId = pe.getFacesContext().getViewRoot().getViewId();
>
> String managedBeanName = getManagedBeanNameFromView(viewId);
>
> String BackingBeanName = managedBeanName.substring(0,1).toUpperCase() +
> managedBeanName.substring(1,managedBeanName.length());
>
> String tempBeanStr = "new "+BackingBeanName+"()";
>
> FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put"\""+managedBeanName+"\"",
> tempBeanStr);
>
> }
>
>
>
>
>
>
>
>
>
> Andrew Robinson-5 wrote:
> >
> > I didn't mean to clear the form, but clear the backing bean properties.
> >
> > public class MyBean {
> >   private MyObject myObject;
> >
> >   public String save() {
> >     // EntityManager or hibernate session save here
> >     // clear the properties:
> >     clearState();
> >   }
> >
> >   public void onCancel(ActionEvent evt) {
> >     clearState();
> >   }
> >
> >   private void clearState() {
> >     myObject = null;
> >   }
> > }
> >
> > On 6/13/07, bansi <ma...@yahoo.com> wrote:
> >>
> >> I figured out that i can do something like this ...
> >> In session scope, only one instance of the backing bean will be used
> >> during
> >> the whole browser session. When you want to recreate the managed bean
> >> inside
> >> the backing bean during session, then do
> >> FacesContext
> >>    .getCurrentInstance()
> >>       .getExternalContext()
> >>          .getSessionMap()
> >>             .put("myBean", new MyBean());
> >>
> >> BUT i am not sure where to put this snippet of code.
> >>
> >>
> >>
> >> bansi wrote:
> >> >
> >> > Andrew
> >> > I totally agree with you on "its the desired behavior of a session bean
> >> --
> >> > one instance for the
> >> > user's session"
> >> > But is their a way to recreate the instance of backing bean  in
> >> following
> >> > situations
> >> > 1) Whenever a new record is inserted into database. The reason i
> >> mention
> >> > this is my backing bean instantiates the pojo and for subsequent save
> >> into
> >> > database the backing bean holds onto the old instance of pojo having
> >> same
> >> > identifier (ID) value. This is exactly the reason Hibernate throws
> >> > Detached Object Exception passed to Persist
> >> >
> >> > 2) Whenever i  navigate between JSF pages , i wanna backing bean to be
> >> > re-initialized i.e. re-created with new instance
> >> >
> >> > Please note as suggested by you i am not looking to clear off the
> >> fields
> >> > on the form whereas i want to recreate the whole backing bean itself
> >> >
> >> > Any pointers/suggestions highly appreciated
> >> >
> >> > Regards
> >> > Bansi
> >> >
> >> >
> >> > Andrew Robinson-5 wrote:
> >> >>
> >> >> That is the desired behavior of a session bean -- one instance for the
> >> >> user's session. If you want to use session, and have it be able to be
> >> >> cleared, then you will want to create a clear action or action
> >> >> listener method that clears all of the member variables when executed.
> >> >>
> >> >> I would instead recommend using conversational scope from JBoss-Seam
> >> >> or MyFaces or request scope and use saveState as needed to persist
> >> >> values across pages.
> >> >>
> >> >> -Andrew
> >> >>
> >> >> On 6/13/07, bansi <ma...@yahoo.com> wrote:
> >> >>>
> >> >>> We have backing bean defined in "session" scope
> >> >>> So whenever we do a submit on JSF Form, it holds onto same backing
> >> bean.
> >> >>> This is not desirable as
> >> >>> -> The Form will have  different set of values each time it does a
> >> >>> submit
> >> >>> -> The Backing bean has variable defined to instantiate a POJO
> >> >>> i.e.private
> >> >>> MyPojo pojo = new MyPojo();
> >> >>> So every time JSF form submits to the backing bean, it holds onto the
> >> >>> same
> >> >>> instance of POJO which eventually results in insertion problems into
> >> >>> database i.e. having same Identifier (ID) value
> >> >>> -> The same problem occurs if i navigate to different page and come
> >> back
> >> >>> to
> >> >>> original page
> >> >>>
> >> >>> Is their a way to re-initialize the Backing Bean ???
> >> >>> --
> >> >>> View this message in context:
> >> >>>
> >> http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11110517
> >> >>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
> >> >>>
> >> >>>
> >> >>
> >> >>
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >> http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11111042
> >> Sent from the MyFaces - Users mailing list archive at Nabble.com.
> >>
> >>
> >
> >
>
> --
> View this message in context: http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11124339
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
>

Re: How to Re-Initialize JSF Backing Bean on Form Submit

Posted by bansi <ma...@yahoo.com>.
The requirement is to have backing bean in session scope.
So when i move back & forth (i.e. navigate) from Current JSF page to Other
page and comeback to Current JSF page it should re-initialize/re-create bean
instance and ofcourse it has to clear of the data on form fields of current
page

I tried doing this in my PhaseListener but it doesnt re-create/re-initialize
the bean instance (i.e. it doesn't call the constructor in the bean)

public void afterPhase(PhaseEvent pe)

{ 

FacesContext facesContext = pe.getFacesContext();

String viewId = pe.getFacesContext().getViewRoot().getViewId();

String managedBeanName = getManagedBeanNameFromView(viewId); 

String BackingBeanName = managedBeanName.substring(0,1).toUpperCase() +
managedBeanName.substring(1,managedBeanName.length());

String tempBeanStr = "new "+BackingBeanName+"()";

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put"\""+managedBeanName+"\"",
tempBeanStr);

}









Andrew Robinson-5 wrote:
> 
> I didn't mean to clear the form, but clear the backing bean properties.
> 
> public class MyBean {
>   private MyObject myObject;
> 
>   public String save() {
>     // EntityManager or hibernate session save here
>     // clear the properties:
>     clearState();
>   }
> 
>   public void onCancel(ActionEvent evt) {
>     clearState();
>   }
> 
>   private void clearState() {
>     myObject = null;
>   }
> }
> 
> On 6/13/07, bansi <ma...@yahoo.com> wrote:
>>
>> I figured out that i can do something like this ...
>> In session scope, only one instance of the backing bean will be used
>> during
>> the whole browser session. When you want to recreate the managed bean
>> inside
>> the backing bean during session, then do
>> FacesContext
>>    .getCurrentInstance()
>>       .getExternalContext()
>>          .getSessionMap()
>>             .put("myBean", new MyBean());
>>
>> BUT i am not sure where to put this snippet of code.
>>
>>
>>
>> bansi wrote:
>> >
>> > Andrew
>> > I totally agree with you on "its the desired behavior of a session bean
>> --
>> > one instance for the
>> > user's session"
>> > But is their a way to recreate the instance of backing bean  in
>> following
>> > situations
>> > 1) Whenever a new record is inserted into database. The reason i
>> mention
>> > this is my backing bean instantiates the pojo and for subsequent save
>> into
>> > database the backing bean holds onto the old instance of pojo having
>> same
>> > identifier (ID) value. This is exactly the reason Hibernate throws
>> > Detached Object Exception passed to Persist
>> >
>> > 2) Whenever i  navigate between JSF pages , i wanna backing bean to be
>> > re-initialized i.e. re-created with new instance
>> >
>> > Please note as suggested by you i am not looking to clear off the
>> fields
>> > on the form whereas i want to recreate the whole backing bean itself
>> >
>> > Any pointers/suggestions highly appreciated
>> >
>> > Regards
>> > Bansi
>> >
>> >
>> > Andrew Robinson-5 wrote:
>> >>
>> >> That is the desired behavior of a session bean -- one instance for the
>> >> user's session. If you want to use session, and have it be able to be
>> >> cleared, then you will want to create a clear action or action
>> >> listener method that clears all of the member variables when executed.
>> >>
>> >> I would instead recommend using conversational scope from JBoss-Seam
>> >> or MyFaces or request scope and use saveState as needed to persist
>> >> values across pages.
>> >>
>> >> -Andrew
>> >>
>> >> On 6/13/07, bansi <ma...@yahoo.com> wrote:
>> >>>
>> >>> We have backing bean defined in "session" scope
>> >>> So whenever we do a submit on JSF Form, it holds onto same backing
>> bean.
>> >>> This is not desirable as
>> >>> -> The Form will have  different set of values each time it does a
>> >>> submit
>> >>> -> The Backing bean has variable defined to instantiate a POJO
>> >>> i.e.private
>> >>> MyPojo pojo = new MyPojo();
>> >>> So every time JSF form submits to the backing bean, it holds onto the
>> >>> same
>> >>> instance of POJO which eventually results in insertion problems into
>> >>> database i.e. having same Identifier (ID) value
>> >>> -> The same problem occurs if i navigate to different page and come
>> back
>> >>> to
>> >>> original page
>> >>>
>> >>> Is their a way to re-initialize the Backing Bean ???
>> >>> --
>> >>> View this message in context:
>> >>>
>> http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11110517
>> >>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>> >>>
>> >>>
>> >>
>> >>
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11111042
>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11124339
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: How to Re-Initialize JSF Backing Bean on Form Submit

Posted by Werner Punz <we...@gmail.com>.
bansi schrieb:
> Thanks Andrew. As very correctly suggested by you i already have the code in
> backing bean to clear the backing bean properties. But the problem is when i
> move back and forth (i.e. navigate) from Current page to Other pages and 
> come back to Current page, i want the backing bean associated with the
> current page to be re-created i.e. new instance of backing bean.
> Ofcourse i wanna clear the data on form fields
> 
Shales view controller might help you out, afair the init callback is
called only
when the page is reentered from outside...

Give it a try,
otherwise you can achieve it via orchestra as well via the valueBound
callback in its conversation controllers, but Shale for now probably is
the easier solution to get up and running.


Re: How to Re-Initialize JSF Backing Bean on Form Submit

Posted by Iskandar Zaynutdinov <iz...@gmail.com>.
On 6/14/07, bansi <ma...@yahoo.com> wrote:
>
>
> Thanks Andrew. As very correctly suggested by you i already have the code
> in
> backing bean to clear the backing bean properties. But the problem is when
> i
> move back and forth (i.e. navigate) from Current page to Other pages and
> come back to Current page, i want the backing bean associated with the
> current page to be re-created i.e. new instance of backing bean.
> Ofcourse i wanna clear the data on form fields
>
>
>
>
> Andrew Robinson-5 wrote:
> >
> > I didn't mean to clear the form, but clear the backing bean properties.
> >
> > public class MyBean {
> >   private MyObject myObject;
> >
> >   public String save() {
> >     // EntityManager or hibernate session save here
> >     // clear the properties:
> >     clearState();
> >   }
> >
> >   public void onCancel(ActionEvent evt) {
> >     clearState();
> >   }
> >
> >   private void clearState() {
> >     myObject = null;
> >   }
> > }
> >
> > On 6/13/07, bansi <ma...@yahoo.com> wrote:
> >>
> >> I figured out that i can do something like this ...
> >> In session scope, only one instance of the backing bean will be used
> >> during
> >> the whole browser session. When you want to recreate the managed bean
> >> inside
> >> the backing bean during session, then do
> >> FacesContext
> >>    .getCurrentInstance()
> >>       .getExternalContext()
> >>          .getSessionMap()
> >>             .put("myBean", new MyBean());
> >>
> >> BUT i am not sure where to put this snippet of code.
> >>
> >>
> >>
> >> bansi wrote:
> >> >
> >> > Andrew
> >> > I totally agree with you on "its the desired behavior of a session
> bean
> >> --
> >> > one instance for the
> >> > user's session"
> >> > But is their a way to recreate the instance of backing bean  in
> >> following
> >> > situations
> >> > 1) Whenever a new record is inserted into database. The reason i
> >> mention
> >> > this is my backing bean instantiates the pojo and for subsequent save
> >> into
> >> > database the backing bean holds onto the old instance of pojo having
> >> same
> >> > identifier (ID) value. This is exactly the reason Hibernate throws
> >> > Detached Object Exception passed to Persist
> >> >
> >> > 2) Whenever i  navigate between JSF pages , i wanna backing bean to
> be
> >> > re-initialized i.e. re-created with new instance
> >> >
> >> > Please note as suggested by you i am not looking to clear off the
> >> fields
> >> > on the form whereas i want to recreate the whole backing bean itself
> >> >
> >> > Any pointers/suggestions highly appreciated
> >> >
> >> > Regards
> >> > Bansi
> >> >
> >> >
> >> > Andrew Robinson-5 wrote:
> >> >>
> >> >> That is the desired behavior of a session bean -- one instance for
> the
> >> >> user's session. If you want to use session, and have it be able to
> be
> >> >> cleared, then you will want to create a clear action or action
> >> >> listener method that clears all of the member variables when
> executed.
> >> >>
> >> >> I would instead recommend using conversational scope from JBoss-Seam
> >> >> or MyFaces or request scope and use saveState as needed to persist
> >> >> values across pages.
> >> >>
> >> >> -Andrew
> >> >>
> >> >> On 6/13/07, bansi <ma...@yahoo.com> wrote:
> >> >>>
> >> >>> We have backing bean defined in "session" scope
> >> >>> So whenever we do a submit on JSF Form, it holds onto same backing
> >> bean.
> >> >>> This is not desirable as
> >> >>> -> The Form will have  different set of values each time it does a
> >> >>> submit
> >> >>> -> The Backing bean has variable defined to instantiate a POJO
> >> >>> i.e.private
> >> >>> MyPojo pojo = new MyPojo();
> >> >>> So every time JSF form submits to the backing bean, it holds onto
> the
> >> >>> same
> >> >>> instance of POJO which eventually results in insertion problems
> into
> >> >>> database i.e. having same Identifier (ID) value
> >> >>> -> The same problem occurs if i navigate to different page and come
> >> back
> >> >>> to
> >> >>> original page
> >> >>>
> >> >>> Is their a way to re-initialize the Backing Bean ???
> >> >>> --
> >> >>> View this message in context:
> >> >>>
> >>
> http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11110517
> >> >>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
> >> >>>
> >> >>>
> >> >>
> >> >>
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11111042
> >> Sent from the MyFaces - Users mailing list archive at Nabble.com.
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11113434
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
> Don't use session beans! Use request scoped beans

Re: How to Re-Initialize JSF Backing Bean on Form Submit

Posted by bansi <ma...@yahoo.com>.
Thanks Andrew. As very correctly suggested by you i already have the code in
backing bean to clear the backing bean properties. But the problem is when i
move back and forth (i.e. navigate) from Current page to Other pages and 
come back to Current page, i want the backing bean associated with the
current page to be re-created i.e. new instance of backing bean.
Ofcourse i wanna clear the data on form fields


  

Andrew Robinson-5 wrote:
> 
> I didn't mean to clear the form, but clear the backing bean properties.
> 
> public class MyBean {
>   private MyObject myObject;
> 
>   public String save() {
>     // EntityManager or hibernate session save here
>     // clear the properties:
>     clearState();
>   }
> 
>   public void onCancel(ActionEvent evt) {
>     clearState();
>   }
> 
>   private void clearState() {
>     myObject = null;
>   }
> }
> 
> On 6/13/07, bansi <ma...@yahoo.com> wrote:
>>
>> I figured out that i can do something like this ...
>> In session scope, only one instance of the backing bean will be used
>> during
>> the whole browser session. When you want to recreate the managed bean
>> inside
>> the backing bean during session, then do
>> FacesContext
>>    .getCurrentInstance()
>>       .getExternalContext()
>>          .getSessionMap()
>>             .put("myBean", new MyBean());
>>
>> BUT i am not sure where to put this snippet of code.
>>
>>
>>
>> bansi wrote:
>> >
>> > Andrew
>> > I totally agree with you on "its the desired behavior of a session bean
>> --
>> > one instance for the
>> > user's session"
>> > But is their a way to recreate the instance of backing bean  in
>> following
>> > situations
>> > 1) Whenever a new record is inserted into database. The reason i
>> mention
>> > this is my backing bean instantiates the pojo and for subsequent save
>> into
>> > database the backing bean holds onto the old instance of pojo having
>> same
>> > identifier (ID) value. This is exactly the reason Hibernate throws
>> > Detached Object Exception passed to Persist
>> >
>> > 2) Whenever i  navigate between JSF pages , i wanna backing bean to be
>> > re-initialized i.e. re-created with new instance
>> >
>> > Please note as suggested by you i am not looking to clear off the
>> fields
>> > on the form whereas i want to recreate the whole backing bean itself
>> >
>> > Any pointers/suggestions highly appreciated
>> >
>> > Regards
>> > Bansi
>> >
>> >
>> > Andrew Robinson-5 wrote:
>> >>
>> >> That is the desired behavior of a session bean -- one instance for the
>> >> user's session. If you want to use session, and have it be able to be
>> >> cleared, then you will want to create a clear action or action
>> >> listener method that clears all of the member variables when executed.
>> >>
>> >> I would instead recommend using conversational scope from JBoss-Seam
>> >> or MyFaces or request scope and use saveState as needed to persist
>> >> values across pages.
>> >>
>> >> -Andrew
>> >>
>> >> On 6/13/07, bansi <ma...@yahoo.com> wrote:
>> >>>
>> >>> We have backing bean defined in "session" scope
>> >>> So whenever we do a submit on JSF Form, it holds onto same backing
>> bean.
>> >>> This is not desirable as
>> >>> -> The Form will have  different set of values each time it does a
>> >>> submit
>> >>> -> The Backing bean has variable defined to instantiate a POJO
>> >>> i.e.private
>> >>> MyPojo pojo = new MyPojo();
>> >>> So every time JSF form submits to the backing bean, it holds onto the
>> >>> same
>> >>> instance of POJO which eventually results in insertion problems into
>> >>> database i.e. having same Identifier (ID) value
>> >>> -> The same problem occurs if i navigate to different page and come
>> back
>> >>> to
>> >>> original page
>> >>>
>> >>> Is their a way to re-initialize the Backing Bean ???
>> >>> --
>> >>> View this message in context:
>> >>>
>> http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11110517
>> >>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>> >>>
>> >>>
>> >>
>> >>
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11111042
>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11113434
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: How to Re-Initialize JSF Backing Bean on Form Submit

Posted by Andrew Robinson <an...@gmail.com>.
I didn't mean to clear the form, but clear the backing bean properties.

public class MyBean {
  private MyObject myObject;

  public String save() {
    // EntityManager or hibernate session save here
    // clear the properties:
    clearState();
  }

  public void onCancel(ActionEvent evt) {
    clearState();
  }

  private void clearState() {
    myObject = null;
  }
}

On 6/13/07, bansi <ma...@yahoo.com> wrote:
>
> I figured out that i can do something like this ...
> In session scope, only one instance of the backing bean will be used during
> the whole browser session. When you want to recreate the managed bean inside
> the backing bean during session, then do
> FacesContext
>    .getCurrentInstance()
>       .getExternalContext()
>          .getSessionMap()
>             .put("myBean", new MyBean());
>
> BUT i am not sure where to put this snippet of code.
>
>
>
> bansi wrote:
> >
> > Andrew
> > I totally agree with you on "its the desired behavior of a session bean --
> > one instance for the
> > user's session"
> > But is their a way to recreate the instance of backing bean  in following
> > situations
> > 1) Whenever a new record is inserted into database. The reason i mention
> > this is my backing bean instantiates the pojo and for subsequent save into
> > database the backing bean holds onto the old instance of pojo having same
> > identifier (ID) value. This is exactly the reason Hibernate throws
> > Detached Object Exception passed to Persist
> >
> > 2) Whenever i  navigate between JSF pages , i wanna backing bean to be
> > re-initialized i.e. re-created with new instance
> >
> > Please note as suggested by you i am not looking to clear off the fields
> > on the form whereas i want to recreate the whole backing bean itself
> >
> > Any pointers/suggestions highly appreciated
> >
> > Regards
> > Bansi
> >
> >
> > Andrew Robinson-5 wrote:
> >>
> >> That is the desired behavior of a session bean -- one instance for the
> >> user's session. If you want to use session, and have it be able to be
> >> cleared, then you will want to create a clear action or action
> >> listener method that clears all of the member variables when executed.
> >>
> >> I would instead recommend using conversational scope from JBoss-Seam
> >> or MyFaces or request scope and use saveState as needed to persist
> >> values across pages.
> >>
> >> -Andrew
> >>
> >> On 6/13/07, bansi <ma...@yahoo.com> wrote:
> >>>
> >>> We have backing bean defined in "session" scope
> >>> So whenever we do a submit on JSF Form, it holds onto same backing bean.
> >>> This is not desirable as
> >>> -> The Form will have  different set of values each time it does a
> >>> submit
> >>> -> The Backing bean has variable defined to instantiate a POJO
> >>> i.e.private
> >>> MyPojo pojo = new MyPojo();
> >>> So every time JSF form submits to the backing bean, it holds onto the
> >>> same
> >>> instance of POJO which eventually results in insertion problems into
> >>> database i.e. having same Identifier (ID) value
> >>> -> The same problem occurs if i navigate to different page and come back
> >>> to
> >>> original page
> >>>
> >>> Is their a way to re-initialize the Backing Bean ???
> >>> --
> >>> View this message in context:
> >>> http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11110517
> >>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
> >>>
> >>>
> >>
> >>
> >
> >
>
> --
> View this message in context: http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11111042
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
>

Re: How to Re-Initialize JSF Backing Bean on Form Submit

Posted by bansi <ma...@yahoo.com>.
I figured out that i can do something like this ...
In session scope, only one instance of the backing bean will be used during
the whole browser session. When you want to recreate the managed bean inside
the backing bean during session, then do
FacesContext
   .getCurrentInstance()
      .getExternalContext()
         .getSessionMap()
            .put("myBean", new MyBean());

BUT i am not sure where to put this snippet of code.



bansi wrote:
> 
> Andrew
> I totally agree with you on "its the desired behavior of a session bean --
> one instance for the
> user's session" 
> But is their a way to recreate the instance of backing bean  in following
> situations
> 1) Whenever a new record is inserted into database. The reason i mention
> this is my backing bean instantiates the pojo and for subsequent save into
> database the backing bean holds onto the old instance of pojo having same
> identifier (ID) value. This is exactly the reason Hibernate throws
> Detached Object Exception passed to Persist
> 
> 2) Whenever i  navigate between JSF pages , i wanna backing bean to be
> re-initialized i.e. re-created with new instance
> 
> Please note as suggested by you i am not looking to clear off the fields
> on the form whereas i want to recreate the whole backing bean itself
> 
> Any pointers/suggestions highly appreciated
> 
> Regards
> Bansi
> 
> 
> Andrew Robinson-5 wrote:
>> 
>> That is the desired behavior of a session bean -- one instance for the
>> user's session. If you want to use session, and have it be able to be
>> cleared, then you will want to create a clear action or action
>> listener method that clears all of the member variables when executed.
>> 
>> I would instead recommend using conversational scope from JBoss-Seam
>> or MyFaces or request scope and use saveState as needed to persist
>> values across pages.
>> 
>> -Andrew
>> 
>> On 6/13/07, bansi <ma...@yahoo.com> wrote:
>>>
>>> We have backing bean defined in "session" scope
>>> So whenever we do a submit on JSF Form, it holds onto same backing bean.
>>> This is not desirable as
>>> -> The Form will have  different set of values each time it does a
>>> submit
>>> -> The Backing bean has variable defined to instantiate a POJO
>>> i.e.private
>>> MyPojo pojo = new MyPojo();
>>> So every time JSF form submits to the backing bean, it holds onto the
>>> same
>>> instance of POJO which eventually results in insertion problems into
>>> database i.e. having same Identifier (ID) value
>>> -> The same problem occurs if i navigate to different page and come back
>>> to
>>> original page
>>>
>>> Is their a way to re-initialize the Backing Bean ???
>>> --
>>> View this message in context:
>>> http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11110517
>>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>>>
>>>
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11111042
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: How to Re-Initialize JSF Backing Bean on Form Submit

Posted by bansi <ma...@yahoo.com>.
Andrew
I totally agree with you on "its the desired behavior of a session bean --
one instance for the
user's session" 
But is their a way to recreate the instance of backing bean  in following
situations
1) Whenever a new record is inserted into database. The reason i mention
this is my backing bean instantiates the pojo and for subsequent save into
database the backing bean holds onto the old instance of pojo having same
identifier (ID) value. This is exactly the reason Hibernate throws Detached
Object Exception passed to Persist

2) Whenever i  navigate between JSF pages , i wanna backing bean to be
re-initialized i.e. re-created with new instance

Please note as suggested by you i am not looking to clear off the fields on
the form whereas i want to recreate the whole backing bean itself

Any pointers/suggestions highly appreciated

Regards
Bansi


Andrew Robinson-5 wrote:
> 
> That is the desired behavior of a session bean -- one instance for the
> user's session. If you want to use session, and have it be able to be
> cleared, then you will want to create a clear action or action
> listener method that clears all of the member variables when executed.
> 
> I would instead recommend using conversational scope from JBoss-Seam
> or MyFaces or request scope and use saveState as needed to persist
> values across pages.
> 
> -Andrew
> 
> On 6/13/07, bansi <ma...@yahoo.com> wrote:
>>
>> We have backing bean defined in "session" scope
>> So whenever we do a submit on JSF Form, it holds onto same backing bean.
>> This is not desirable as
>> -> The Form will have  different set of values each time it does a submit
>> -> The Backing bean has variable defined to instantiate a POJO
>> i.e.private
>> MyPojo pojo = new MyPojo();
>> So every time JSF form submits to the backing bean, it holds onto the
>> same
>> instance of POJO which eventually results in insertion problems into
>> database i.e. having same Identifier (ID) value
>> -> The same problem occurs if i navigate to different page and come back
>> to
>> original page
>>
>> Is their a way to re-initialize the Backing Bean ???
>> --
>> View this message in context:
>> http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11110517
>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11110875
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: How to Re-Initialize JSF Backing Bean on Form Submit

Posted by Andrew Robinson <an...@gmail.com>.
That is the desired behavior of a session bean -- one instance for the
user's session. If you want to use session, and have it be able to be
cleared, then you will want to create a clear action or action
listener method that clears all of the member variables when executed.

I would instead recommend using conversational scope from JBoss-Seam
or MyFaces or request scope and use saveState as needed to persist
values across pages.

-Andrew

On 6/13/07, bansi <ma...@yahoo.com> wrote:
>
> We have backing bean defined in "session" scope
> So whenever we do a submit on JSF Form, it holds onto same backing bean.
> This is not desirable as
> -> The Form will have  different set of values each time it does a submit
> -> The Backing bean has variable defined to instantiate a POJO i.e.private
> MyPojo pojo = new MyPojo();
> So every time JSF form submits to the backing bean, it holds onto the same
> instance of POJO which eventually results in insertion problems into
> database i.e. having same Identifier (ID) value
> -> The same problem occurs if i navigate to different page and come back to
> original page
>
> Is their a way to re-initialize the Backing Bean ???
> --
> View this message in context: http://www.nabble.com/How-to-Re-Initialize-JSF-Backing-Bean-on-Form-Submit-tf3918359.html#a11110517
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
>