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);
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
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With