KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > NamespacingViewRoot


1 package com.icesoft.faces.component;
2
3 import com.icesoft.jasper.Constants;
4 import java.io.Serializable JavaDoc;
5
6 import javax.faces.component.UIViewRoot;
7 import javax.faces.context.ExternalContext;
8 import javax.faces.context.FacesContext;
9 import java.util.Map JavaDoc;
10
11 /**
12  * The Sun RI does not call encodeNamespace when it creates unique ID's whereas
13  * MyFaces does. In order to support portlets, component ids must have the
14  * appropriate namespace applied. This custom UIViewRoot class can be used by
15  * our own custom ViewHandler. It simply overrides the createUniqueId method
16  * and, if necessary, prepends the namespace to each component id.
17  */

18 public class NamespacingViewRoot extends UIViewRoot implements Serializable JavaDoc{
19
20     private ExternalContext extCtxt;
21     private String JavaDoc namespace;
22
23     public NamespacingViewRoot(FacesContext context) {
24         extCtxt = context.getExternalContext();
25         Map JavaDoc requestMap = extCtxt.getRequestMap();
26         namespace = (String JavaDoc)requestMap.get(Constants.NAMESPACE_KEY);
27     }
28
29     /**
30      * Overrides the UIViewRoot.createUniqueId method. The parent method is
31      * called and the resulting id has the namespace pre-pended if:
32      * - a namespace has been made available (e.g. portlets)
33      * - the namespace has not already been prepended (e.g. MyFaces)
34      *
35      * @return a unique component id, potentially with a namespace prepended
36      */

37     public String JavaDoc createUniqueId() {
38         String JavaDoc uniqueID = super.createUniqueId();
39
40         if (namespace == null || uniqueID.startsWith(namespace)) {
41             return uniqueID;
42         }
43
44         return extCtxt.encodeNamespace(uniqueID);
45     }
46 }
47
Popular Tags