Tuesday, September 17, 2013

JSF 2.x Tip of the Day: Resetting the Current UIViewRoot

I needed to clear the UIViewRoot for something I was working on. In the process, I created a simple method that replaces the current view with a new view using the same view ID. I figured I would publish this little gem before I deleted the proof of concept I was working on.

I also thought I should include an example where I am using it to navigate to another page.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   public void replaceCurrentView() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ViewHandler viewHandler = application.getViewHandler();
        // Use the same view ID as the old view.
        UIViewRoot uivr = viewHandler.createView(facesContext, facesContext.getViewRoot().getViewId());
        facesContext.setViewRoot(uivr);
//        facesContext.renderResponse();
    }
 
public String navigate() {
        replaceCurrentView();
        FacesContext.getCurrentInstance().getApplication().getStateManager().saveView(FacesContext.getCurrentInstance());
        return "nextpage";
    }

0 comments :

Popular Posts