jsf clearing the form

只愿长相守 提交于 2019-12-25 05:25:16

问题


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?


回答1:


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:

  • How to choose the right bean scope?

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.



来源:https://stackoverflow.com/questions/13051842/jsf-clearing-the-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!