You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by gv...@apache.org on 2005/08/28 01:58:23 UTC

svn commit: r248269 - in /struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/faces: ClayViewHandler.java ClayViewHandlerCommand.java

Author: gvanmatre
Date: Sat Aug 27 16:58:10 2005
New Revision: 248269

URL: http://svn.apache.org/viewcvs?rev=248269&view=rev
Log:
Bug#:  36200 [shale] Clay HTML templates doesn't work with myFaces

Added:
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/faces/ClayViewHandlerCommand.java
Modified:
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/faces/ClayViewHandler.java

Modified: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/faces/ClayViewHandler.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/faces/ClayViewHandler.java?rev=248269&r1=248268&r2=248269&view=diff
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/faces/ClayViewHandler.java (original)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/faces/ClayViewHandler.java Sat Aug 27 16:58:10 2005
@@ -113,23 +113,40 @@
     }
 
     /**
-     * <p>If the <code>viewId</code> is suffixed with the Clay template suffix,
-     * rewrite the returned actionUrl with a clay suffix.  The super implementation
-     * will assume ".jsp" or whatever the <code>javax.faces.DEFAULT_SUFFIX</code> is 
-     * set to in the web deployment descriptor.</p>
+     * <p>Changes the suffix of the <code>viewId</code> to that of the 
+     * assumed suffix of a clay template. 
+     */
+    private String normalizeViewId(String viewId) {
+        
+        StringBuffer buff = new StringBuffer(viewId);
+        int i = buff.lastIndexOf(".");
+        if (i > -1) {
+            buff.setLength(i);
+            buff.append(suffix);
+        }
+        
+        return buff.toString();
+      
+    }
+    
+    
+    /**
+     * <p>
+     * If the <code>viewId</code> is suffixed with the Clay template suffix,
+     * rewrite the returned actionUrl with a clay suffix. The super
+     * implementation will assume ".jsp" or whatever the
+     * <code>javax.faces.DEFAULT_SUFFIX</code> is set to in the web deployment
+     * descriptor.
+     * </p>
      */
     public String getActionURL(FacesContext context, String viewId) {
-        StringBuffer actionUrl = new StringBuffer(original.getActionURL(context, viewId));
+        String actionURL = original.getActionURL(context, viewId);
 
-        if (this.isClayTemplate(context, viewId)) {
-           int i = actionUrl.lastIndexOf(".");
-           if (i > -1) {
-              actionUrl.setLength(i);
-              actionUrl.append(suffix);
-           }     
-        } 
+        if (isClayTemplate(context, viewId)) {
+           actionURL = normalizeViewId(actionURL);
+        }
         
-        return actionUrl.toString();
+        return actionURL;
     }
 
     // Specified by ViewHandler
@@ -139,10 +156,13 @@
 
     /**
      * <p>Returns <code>true</code> if the <code>viewId</code> has a
-     * matching <code>suffix</code>.  The <code>suffix</code> is determined
-     * by an initialization parameter in the web deployment descriptor,
-     * <code>clay-template-suffix</code>.  The default suffix, if one
-     * is not specified, is ".clay".
+     * matching <code>suffix</code> or a request parameter exists with
+     * the name of <code>Globals.CLAY_TEMPLATE_SUFFIX</code>.  This
+     * indicator will be cached in the request by the 
+     * {@link ClayViewHandlerCommand} preprocess shale chain (only with myfaces).  
+     * The <code>suffix</code> is determined by an initialization parameter 
+     * in the web deployment descriptor, <code>clay-template-suffix</code>.  
+     * The default suffix, if one is not specified, is ".clay".
      * </p> 
      */
     protected boolean isClayTemplate(FacesContext context, String viewId) {
@@ -154,7 +174,8 @@
             }
         }
         
-        return viewId.endsWith(suffix);
+        return (viewId.endsWith(suffix) 
+                || context.getExternalContext().getRequestMap().containsKey(Globals.CLAY_TEMPLATE_SUFFIX)) ;
     }
 
     /**
@@ -166,13 +187,13 @@
     public UIViewRoot restoreView(FacesContext context, String viewId) {
         UIViewRoot view = null;
         
-        if (this.isClayTemplate(context, viewId)) {
+        if (isClayTemplate(context, viewId)) {
             
             if (log.isInfoEnabled())
                 log.info("Clay template restoreView for " + viewId);
                         
             StateManager stateManager = context.getApplication().getStateManager();
-            view = stateManager.restoreView(context, viewId, calculateRenderKitId(context));
+            view = stateManager.restoreView(context, normalizeViewId(viewId), calculateRenderKitId(context));
         } else
            view = original.restoreView(context, viewId);
         return view;
@@ -208,10 +229,12 @@
     
     /**
      * <p>The <code>viewId</code> of the view will be check to see if it ends with the 
-     * same suffix as the configured clay-template-suffix.  If a match is not found, 
-     * control is passed to the decorated view handler.  Otherwise, a {@link Clay} component 
-     * is instantiated as a single subtree under the view root. The component's <code>id</code>
-     * property is set with a constant, <code>CLAY_VIEW_ID</code>.  The <code>jsfid</code> 
+     * same suffix as the configured clay-template-suffix.  This match might be performed
+     * by the {@link ClayViewHandlerCommand} when using the myfaces jsf implementation.  
+     * If a match is not found, control is passed to the decorated view handler.  
+     * Otherwise, a {@link Clay} component is instantiated as a single subtree under the 
+     * view root. The component's <code>id</code> property is set with a constant, 
+     * <code>CLAY_VIEW_ID</code>.  The <code>jsfid</code> 
      * property is set to the <code>viewId</code>.  The <code>managedBeanName</code> property 
      * is set with the Shale <code>ViewControllerMapper</code>.  A <code>ResponseWriter</code> 
      * is created and rendering is invoked on the component.  This differs from the base implementation. 
@@ -230,10 +253,12 @@
             //look to see if it already exists 
             UIComponent component = view.findComponent(CLAY_VIEW_ID);
             if (component == null) {
-               component = context.getApplication().createComponent(CLAY_COMPONENT_TYPE);
+                
+              String viewId = normalizeViewId(view.getViewId()); 
+              component = context.getApplication().createComponent(CLAY_COMPONENT_TYPE);
               ((Clay) component).setId(CLAY_VIEW_ID);
-              ((Clay) component).setJsfid(view.getViewId());
-              ((Clay) component).setManagedBeanName(getManagedBeanName(context, view.getViewId()));
+              ((Clay) component).setJsfid(viewId);
+              ((Clay) component).setManagedBeanName(getManagedBeanName(context, viewId));
               view.getChildren().add(view.getChildren().size(), component);
             } 
             

Added: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/faces/ClayViewHandlerCommand.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/faces/ClayViewHandlerCommand.java?rev=248269&view=auto
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/faces/ClayViewHandlerCommand.java (added)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/faces/ClayViewHandlerCommand.java Sat Aug 27 16:58:10 2005
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * 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$
+ */
+
+package org.apache.shale.clay.faces;
+
+import org.apache.commons.chain.Command;
+import org.apache.commons.chain.Context;
+import org.apache.shale.clay.config.Globals;
+import org.apache.shale.faces.ShaleWebContext;
+
+/**
+ * This is a Shale "preprocess" command that should be registered 
+ * with the shale chains catalog.  It is only need for implementing 
+ * full html Clay views with myfaces.  The myfaces implementation pays 
+ * strict attention to the javax.faces.DEFAULT_SUFFIX, only allowing viewId's
+ * with this suffix.  This command will place an indicator in request scope 
+ * before the request is processed by the faces servlet.  The {@link ClayViewHandler} 
+ * will use the indicator to determine that the view should be handled by the 
+ * {@link ClayViewHandler}.
+ *
+ */
+public class ClayViewHandlerCommand implements Command {
+
+    /**
+     * <p>Holds the suffix used to identify a Clay HTML full view template.</p>
+     */
+    private String suffix = null;
+    
+    /**
+     * <p>Looks at the request uri to determine if the target page
+     * is a clay template.  If the request's pathInfo matches the
+     * clay template suffix, a flag is added to the request attributes.
+     * This is need for the MyFaces JSF implementation.
+     */
+    public boolean execute(Context context) throws Exception {
+        
+        ShaleWebContext webContext = (ShaleWebContext) context;
+        if (isClayTemplate(webContext)) {
+           webContext.getRequest().setAttribute(Globals.CLAY_TEMPLATE_SUFFIX, Boolean.TRUE);
+        }
+
+        return false;
+    }
+    
+    
+    /**
+     * <p>Returns <code>true</code> if the request's <code>uri</code> has a
+     * matching <code>suffix</code>.  The <code>suffix</code> is determined
+     * by an initialization parameter in the web deployment descriptor,
+     * <code>clay-template-suffix</code>.  The default suffix, if one
+     * is not specified, is ".clay".
+     * </p> 
+     */
+    protected boolean isClayTemplate(ShaleWebContext context) {
+        if (suffix == null) {
+            suffix = (String) context.getContext().getInitParameter(
+                    Globals.CLAY_TEMPLATE_SUFFIX);
+            if (suffix == null) {
+                suffix = Globals.CLAY_DEFAULT_TEMPLATE_SUFFIX;
+            }
+        }
+        String uri = context.getRequest().getRequestURI();
+        return (uri != null) && (uri.endsWith(suffix));
+    }
+
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org