You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by "Zheng, Xiahong" <Xi...@FMR.COM> on 2008/01/08 17:57:58 UTC

How to distinguish between Form loading and submiting in JSF?

Hi all,

I have a form that is wired with a backing bean. On the initial loading,
I need to load the backing properties from the DB to be displayed on the
page, then persist into the DB on submission after user edited the form.

I only want to preload the bean from DB on the initial form loading not
on the form submission. Is there a way for the backing bean to know
whether it is created as a result of initial request or a form submit so
it can act accordingly.

Thanks,
Xiaohong

RE: How to distinguish between Form loading and submiting in JSF?

Posted by "Zheng, Xiahong" <Xi...@FMR.COM>.
Thanks for your quick response. I will try the 2nd approach. The first
approach, I am also aware of,  is not acceptable as I try to avoid
Servlet API in my bean class. 


  _____  

	From: Andrew Robinson [mailto:andrew.rw.robinson@gmail.com] 
	Sent: Tuesday, January 08, 2008 12:09 PM
	To: MyFaces Discussion
	Subject: Re: How to distinguish between Form loading and
submiting in JSF?
	
	
	Not sure about a way with portlets, but this should work with a
servlet environment:
	
	
"POST".equals(((HttpServletRequest)FacesContext.getCurrentInstance().get
ExternalContext().getRequest()).getMethod())
	
	Note that all command* components post, so this will not tell
you if the page is new or not.
	
	Another way is to store an attribute in the UIViewRoot during
rendering. If it is present, it is the 2nd (or more) time that the page
has been renderer, and if it is not set then it is the first. 
	
	
FacesContext.getCurrentInstance().getViewRoot().getAttributes().get("myp
roperty") != null
	...
	
FacesContext.getCurrentInstance().getViewRoot().getAttributes().put("myp
roperty", true)
	
	-Andrew
	
	
	On Jan 8, 2008 9:57 AM, Zheng, Xiahong <Xi...@fmr.com>
wrote:
	

		Hi all,
		
		I have a form that is wired with a backing bean. On the
initial loading,
		I need to load the backing properties from the DB to be
displayed on the
		page, then persist into the DB on submission after user
edited the form. 
		
		I only want to preload the bean from DB on the initial
form loading not
		on the form submission. Is there a way for the backing
bean to know
		whether it is created as a result of initial request or
a form submit so 
		it can act accordingly.
		
		Thanks,
		Xiaohong
		



Re: How to distinguish between Form loading and submiting in JSF?

Posted by Andrew Robinson <an...@gmail.com>.
Not sure about a way with portlets, but this should work with a servlet
environment:

"POST".equals(((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getMethod())

Note that all command* components post, so this will not tell you if the
page is new or not.

Another way is to store an attribute in the UIViewRoot during rendering. If
it is present, it is the 2nd (or more) time that the page has been renderer,
and if it is not set then it is the first.

FacesContext.getCurrentInstance().getViewRoot().getAttributes().get("myproperty")
!= null
...
FacesContext.getCurrentInstance().getViewRoot().getAttributes().put("myproperty",
true)

-Andrew

On Jan 8, 2008 9:57 AM, Zheng, Xiahong <Xi...@fmr.com> wrote:

> Hi all,
>
> I have a form that is wired with a backing bean. On the initial loading,
> I need to load the backing properties from the DB to be displayed on the
> page, then persist into the DB on submission after user edited the form.
>
> I only want to preload the bean from DB on the initial form loading not
> on the form submission. Is there a way for the backing bean to know
> whether it is created as a result of initial request or a form submit so
> it can act accordingly.
>
> Thanks,
> Xiaohong
>