How to reset in jsf?
I am using liferay and jsf. In my portlet, I tried to reset after page submission by setting new values to bean backup. I got empty fields, but when I refreshed the page, I got my old values again that I presented earlier and they are sent again with the old values.
I tried to get a new viewroot but it also gave me the 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();
}
PS After submitting, I call this method as reset (null);
source to share
It is not a problem with the update, but its browser that sends the previous submitted data. I found some solutions that are pretty good.
-
From the BalusC blog the POST-Redirect-GET template
-
From rohit salecha Liferay report Update bug
Since I am using Liferay I only have to put
<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.
source to share
It looks like the scope of your bean is neither a request nor a viewcope as you want it to be reset even before the view is destroyed.
You can use ConversationScoped
the CDI that will be created when you start the conversation and you can mark it to the end by calling conversation.end()
.
How does JSF 2 ConversationScope work?
Alternatively, if you want to reset the View Scope bean, you can also do this using below in your Ajax event,
Map<String, Object> viewMap = FacesContext.getCurrentInstance().
getViewRoot().getViewMap();
viewMap.put("MyBean",new MyBean());
source to share