3

I am using liferay and jsf. In my portlet I tried to reset after submitting page by setting new values to the backing bean. I got blank fields but when I refreshed the page, I got my old values again which I have submitted earlier and it submit again with old values.

I tried to get new viewroot but it also gave me same result.

public void reset(AjaxBehaviorEvent event){

    FacesContext context = FacesContext.getCurrentInstance();
    Application application = context.getApplication();
    ViewHandler viewHandler = application.getViewHandler();
    UIViewRoot viewRoot = viewHandler.createView(context, context
            .getViewRoot().getViewId());
    context.setViewRoot(viewRoot);
    context.renderResponse(); //Optional

    this.MyBean = new MyBean();
}

P.S. After submit I call this method as reset(null);

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kush Sahu
  • 399
  • 1
  • 13
  • 33

4 Answers4

1

If you want the bean to be reset after each request; in that case instead of using the function you can make use of the scope(s) provided by the jsf.

Using the bean in request scope

tutorial on bean scopes

Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
1

It seems that the scope of your bean is neither request nor viewscope as you want it to be reset even before view is destroyed.

You can make use of ConversationScoped in CDI which will be created when you start the conversation and you can mark it to end by calling conversation.end().

How does JSF 2 ConversationScope work?

Alternatively , if you want to reset the View Scope bean then you can also do it by using below in your Ajax Event,

Map<String, Object> viewMap = FacesContext.getCurrentInstance().
                              getViewRoot().getViewMap();
viewMap.put("MyBean",new MyBean());
Community
  • 1
  • 1
Avinash Singh
  • 3,421
  • 2
  • 20
  • 21
0

It's really that simple: Just annotate with @RequestScoped and a button:

@ManagedBean
@RequestScoped
public class MyBean{

public void resetEvent(ActionEvent actionEv) {
    }

}
Arian
  • 3,183
  • 5
  • 30
  • 58
0

It is not issue with refresh, but its the browser which sends previous submitted data. I found some solutions which are pretty good.

Since I am using Liferay, I have to put only

<action-url-redirect>true</action-url-redirect> 

in liferay-portlet.xml like this

<portlet>
<portlet-name>booksportlet</portlet-name>
<icon>/icon.png</icon>
<action-url-redirect>true</action-url-redirect>
<instanceable>false</instanceable>
</portlet>

So I chose this.

Kush Sahu
  • 399
  • 1
  • 13
  • 33