0

I have a JSF page with a form that contains multiple textfields (p:inputtext) and a submit button. The page is backed by a ViewScoped backing bean. When the submit button is hit, an action method is called that returns an empty String ("").

According to this answer of BalusC, returning an empty string will refresh the view and recreate the ViewScoped backing bean.

However, when I submit my filled out form, the reloaded page still retains all my text input. How is this possible? Shouldn't the form be empty since the backing bean and view have been recreated?

Community
  • 1
  • 1
Quercus
  • 5
  • 3

2 Answers2

0

@dmatob is right. When you have a JSF page backed by a ViewScoped bean:

  • If the method returns null, the bean won't be recreated (the values stay the same) but the page is reloaded.
  • If the method returns the same or another page, the bean will be recreated (it resets the values) and the page is reloaded.

I was facing the same few hours ago: trying to reset the values when the method is successfully executed. So after reading around and around, I found something that finally worked out:

You have to use action instead of actionListener (Differences here)

<p:commandButton value="Save" action="#{backingBean.save()}" />

So the method must return a String

public String save(){
  if(validations==true){
     return "currentpage.xhtml?faces-redirect=true";
  }
  return null;
}

When everything is okay, it will recreate the bean, refresh the page and reset the values. Otherwise the method returns null so it will refresh the page but the bean.


[EDITED]

If the method is returning null or empty String, the bean isn't recreated: the PostConstruct (init event) isn't being triggered, so that means the values stay the same. On the other case, if it returns a String (redirecting to some page), the init event is called so the values are initialized.

The JSF page is reloaded in both cases: when returning null/empty String or not.


Hope it helps you... Let me know ;-)

Community
  • 1
  • 1
  • The solution is in the correct direction, but you're not explaining anywhere why submitted values seem to stick around even though the view and bean are recreated. This is exactly what the OP was wondering about. I guess he already knew the correct solution of sending a redirect. – BalusC Feb 06 '17 at 07:22
  • Thanks for the response. Indeed i know the solution of redirecting to clear the form. But as BalusC said, I would like to know why submitted form values stick around even though bean and view are recreated upon returning an empty string in the action method (not listener!). – Quercus Feb 06 '17 at 09:21
  • When returning an empty String or null, the submitted values stick around because of the bean isn't recreated (the PostConstruct isn't called), so that means those values aren't initialized. – JairKaulitz89 Feb 06 '17 at 14:29
-1

In a view scoped bean, only when your action method returns null, the bean doesn't initialize again.

If you want the action method to go back to the submitted form and reload the bean, your method must return the name of the page that contains the form.

dmatob
  • 1
  • 2
  • That was not the question. Moreover, this is all already covered by the answer mentioned in the link in the question. – BalusC Feb 06 '17 at 07:21