Created
July 22, 2017 03:02
-
-
Save jyeary/ccd8f00aa865180343a552d61b358758 to your computer and use it in GitHub Desktop.
Clears the view map if the current ID does not match the new view ID.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2014 John Yeary <[email protected]>. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
/* | |
* $Id: ViewMapSystemEventListener.java,v a954aef6f00c 2014/08/05 01:54:57 jyeary $ | |
*/ | |
package com.bluelotussoftware.jsf.listener; | |
import com.sun.faces.application.view.ViewScopeManager; | |
import java.util.Map; | |
import javax.faces.component.UIViewRoot; | |
import javax.faces.context.FacesContext; | |
import javax.faces.event.AbortProcessingException; | |
import javax.faces.event.SystemEvent; | |
import javax.faces.event.SystemEventListener; | |
/** | |
* | |
* @author John Yeary <[email protected]> | |
* @version 1.0 | |
*/ | |
public class ViewMapSystemEventListener implements SystemEventListener { | |
private String currentId; | |
@Override | |
public void processEvent(SystemEvent event) throws AbortProcessingException { | |
/* | |
* 1. Get the source of the event and its view id. | |
* 2. Check if the currentId is null, if so, set it to the source id. | |
* 3. Check the currentId and the new id. | |
* a. If they are the same, continue. We are on the same page. | |
* b. If they are different, clear the view map, and remove from Session map. | |
*/ | |
UIViewRoot uivr = (UIViewRoot) event.getSource(); | |
String id = uivr.getViewId(); | |
FacesContext fctx = FacesContext.getCurrentInstance(); | |
ViewScopeManager vsm = ViewScopeManager.getInstance(fctx); | |
if (currentId == null) { | |
currentId = id; | |
} | |
if (!currentId.equals(id)) { | |
//Clear map and set currentId to new id. | |
vsm.clear(fctx); | |
String key = (String) uivr.getTransientStateHelper().getTransient(ViewScopeManager.VIEW_MAP_ID); | |
uivr.getViewMap().clear(); | |
Map<String, Object> viewMaps = (Map<String, Object>) fctx.getExternalContext().getSessionMap().get(ViewScopeManager.ACTIVE_VIEW_MAPS); | |
viewMaps.remove(key); | |
currentId = id; | |
} | |
} | |
@Override | |
public boolean isListenerForSource(Object source) { | |
return (source instanceof UIViewRoot); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment