0

I need help. I'm new to JSF and im using JSF2 and richfaces in my project.

I want to clear the form for which I'm using <f:ajax render="@form"/> in refresh button. I have an ADD button on that screen which adds one record and I hit refresh then it's going to the default page. But when I once again go to enter a record then those values which I entered earlier remain in the form fields.

Can anyone please help me out with this issue?

Chad
  • 7,279
  • 2
  • 24
  • 34
user1771540
  • 103
  • 2
  • 4
  • 11

1 Answers1

2

Assuming that you mean the browser's refresh button when you say "I hit refresh", then that can happen if you've incorrectly placed the bean holding view scoped data in the session scope. You're then basically reusing the very same bean as the form is previously been submitted to. Putting the bean in the view scope instead of the session scope should fix this problem. The view scope ends when you navigate to a different page or fires a new request (as by hitting browser's refresh button).

See also:


Update if you're due to bad design restricted to using session scope, then you might want to hack this around by a

<f:event type="preRenderView" listener="#{sessionScopedBeanWhichShouldActuallyBeViewScoped.resetModel}" />

with

public void resetModel() { 
    if (!FacesContext.getCurrentInstance().isPostback()) {
        model = null;
    }
}

This will clear the model on every GET request. However, regardless of this hack, you'll still run into serious problems when the enduser opens the same view in a different browser tab/window within the same session.

The right solution is to put the bean in the view scope instead of the session scope, as said earlier.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • im using sessionscope for other purpose if i use view scope it will break all other code – user1771540 Oct 24 '12 at 15:09
  • Then you've bigger problems with the design. Sorry, that can't be solved with a single answer. Breakdown the new problems in new questions. Anyway, you should now understand the cause of your current problem: you used the wrong scope for the data. – BalusC Oct 24 '12 at 15:11
  • it working with @viewscope for that issue but when i use that i can able to switch from one tab to anyother when i add something and without hitting the refresh action if i switch to other tabs its hangs. I'm not able to do any furture actions. Can you help me out in this issue. – user1771540 Oct 24 '12 at 15:58
  • This issue is unrelated to the current question/problem. Press `Ask Question` button with a detailed description of the problem, along with an SSCCE. – BalusC Oct 24 '12 at 16:01
  • its because of that its behaving like this then how can it not be related to this issue. – user1771540 Oct 24 '12 at 16:12
  • You have just severe design problems. The symptoms which you initially described in the question (clearing the form) are to be solved using the proper scope. If that causes **other** problems, then you've simply *another* problem which also needs to be fixed separately. It's complete nonsense that there would be 1 holy grail solution for *all* problems. – BalusC Oct 24 '12 at 16:14
  • I will explain you what im doing here. I have one webpage in that there are 5 tabs are in one index.xhtml page. when i try to add anything and want to refresh then its going to the defaults settings of that particular tab but its not clearing the input field what ever i entered. i need to clear that. – user1771540 Oct 24 '12 at 16:14
  • As said, press `Ask Question` button with a detailed description of the problem, along with an SSCCE. It's not the fault of `@ViewScoped`. It's just a different design problem elsewhere in your webapp. – BalusC Oct 24 '12 at 16:17
  • yes you are right. That issue was not with viewscoped when i used sessionscoped and try the same its getting the same error there is on .js error. I have to fix that ASAP. – user1771540 Oct 24 '12 at 20:17