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);

+3


source to share


4 answers


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.

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.

0


source


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

Using a scope bean in request



bean scopes tutorial

+1


source


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());

      

+1


source


It's really that simple: just comment with @RequestScoped and a button:

@ManagedBean
@RequestScoped
public class MyBean{

public void resetEvent(ActionEvent actionEv) {
    }

}

      

0


source







All Articles