Popular Posts

Monday, January 18, 2016

Rendering View in JavaServer Faces (JSF)

Those who works with JavaServer Faces will agree with me that it's quite a difficult job to clear out the current view if the current view is stuck in validation phase. I came across a scenario in which I need to display errors to client and move forward only with his request if the client has fixed all errors, normal flow, right? But what if the client wants to cancel the first step and wants to forward to the next step without fixing errors. So then in this scenario instead of clearing the errors one by one JSF allows you to create a whole new view by replacing the current view and allows the request to proceed further.

If you need to clear the current view, take a look at this clear function below:

1:   public void createCurrentView()   
2:   {  
3:     FacesContext context = FacesContext.getCurrentInstance();  
4:     Application application = context.getApplication();  
5:     ViewHandler viewHandler = application.getViewHandler();  
6:     UIViewRoot viewRoot =   
7:            viewHandler.createView(context, context.getViewRoot().getViewId());

8:     context.setViewRoot(viewRoot);  
9:     context.renderResponse();  
10:   }  

The above function will try to get the current view from the context, creates a new view and replace the current with the new one in the context.

You should only clear the whole current view in rare cases as creating view again and again might impact your performance and it is also not recommended by industry experts to use it for frequent web flows.

Please share, How it goes!
Post a Comment