You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2014/05/13 14:15:17 UTC

svn commit: r1594209 - in /myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared: config/MyfacesConfig.java renderkit/html/HtmlFormRendererBase.java

Author: lu4242
Date: Tue May 13 12:15:17 2014
New Revision: 1594209

URL: http://svn.apache.org/r1594209
Log:
MYFACES-3880 Allow view state to be rendered in the beginning of the form

Modified:
    myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/config/MyfacesConfig.java
    myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlFormRendererBase.java

Modified: myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/config/MyfacesConfig.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/config/MyfacesConfig.java?rev=1594209&r1=1594208&r2=1594209&view=diff
==============================================================================
--- myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/config/MyfacesConfig.java (original)
+++ myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/config/MyfacesConfig.java Tue May 13 12:15:17 2014
@@ -455,6 +455,17 @@ public class MyfacesConfig
     public static final String INIT_PARAM_STRICT_JSF_2_FACELETS_COMPATIBILITY = 
             "org.apache.myfaces.STRICT_JSF_2_FACELETS_COMPATIBILITY";
     public final static boolean INIT_PARAM_STRICT_JSF_2_FACELETS_COMPATIBILITY_DEFAULT = false;    
+    
+    /**
+     * This param makes h:form component to render the view state and other hidden fields
+     * at the beginning of the form. This also includes component resources with target="form",
+     * but it does not include legacy 1.1 myfaces specific hidden field adition.
+     */
+    @JSFWebConfigParam(since = "2.2.4", defaultValue = "false", expectedValues = "true,false",
+            group="render")
+    public static final String INIT_PARAM_RENDER_FORM_VIEW_STATE_AT_BEGIN =
+            "org.apache.myfaces.RENDER_FORM_VIEW_STATE_AT_BEGIN";
+    public final static boolean INIT_PARAM_RENDER_FORM_VIEW_STATE_AT_BEGIN_DEFAULT = false;
 
     private boolean _prettyHtml;
     private boolean _detectJavascript;
@@ -492,6 +503,7 @@ public class MyfacesConfig
     private boolean _cdiManagedConvertersEnabled;
     private boolean _cdiManagedValidatorsEnabled;
     private boolean _strictJsf2FaceletsCompatibility;
+    private boolean _renderFormViewStateAtBegin;
 
     private static final boolean TOMAHAWK_AVAILABLE;
     private static final boolean MYFACES_IMPL_AVAILABLE;
@@ -597,6 +609,7 @@ public class MyfacesConfig
         setCdiManagedConvertersEnabled(INIT_PARAM_CDI_MANAGED_CONVERTERS_DEFAULT);
         setCdiManagedValidatorsEnabled(INIT_PARAM_CDI_MANAGED_VALIDATORS_DEFAULT);
         setStrictJsf2FaceletsCompatibility(INIT_PARAM_STRICT_JSF_2_FACELETS_COMPATIBILITY_DEFAULT);
+        setRenderFormViewStateAtBegin(INIT_PARAM_RENDER_FORM_VIEW_STATE_AT_BEGIN_DEFAULT);
     }
 
     private static MyfacesConfig createAndInitializeMyFacesConfig(ExternalContext extCtx)
@@ -719,6 +732,10 @@ public class MyfacesConfig
                 INIT_PARAM_STRICT_JSF_2_FACELETS_COMPATIBILITY, 
                 INIT_PARAM_STRICT_JSF_2_FACELETS_COMPATIBILITY_DEFAULT));
         
+        myfacesConfig.setRenderFormViewStateAtBegin(WebConfigParamUtils.getBooleanInitParameter(extCtx,
+                INIT_PARAM_RENDER_FORM_VIEW_STATE_AT_BEGIN,
+                INIT_PARAM_RENDER_FORM_VIEW_STATE_AT_BEGIN_DEFAULT));
+        
         if (TOMAHAWK_AVAILABLE)
         {
             myfacesConfig.setDetectJavascript(getBooleanInitParameter(extCtx, INIT_PARAM_DETECT_JAVASCRIPT,
@@ -1286,4 +1303,14 @@ public class MyfacesConfig
     {
         this._strictJsf2FaceletsCompatibility = strictJsf2FaceletsCompatibility;
     }
+
+    public boolean isRenderFormViewStateAtBegin()
+    {
+        return _renderFormViewStateAtBegin;
+    }
+
+    public void setRenderFormViewStateAtBegin(boolean renderFormViewStateAtBegin)
+    {
+        this._renderFormViewStateAtBegin = renderFormViewStateAtBegin;
+    }
 }

Modified: myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlFormRendererBase.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlFormRendererBase.java?rev=1594209&r1=1594208&r2=1594209&view=diff
==============================================================================
--- myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlFormRendererBase.java (original)
+++ myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlFormRendererBase.java Tue May 13 12:15:17 2014
@@ -148,6 +148,13 @@ public class HtmlFormRendererBase
         // included for backward compatibility to the 1.1.1 patch (JIRA MYFACES-1276)
         // However, might be needed in the future
         beforeFormElementsStart(facesContext, component);
+        
+        MyfacesConfig config = MyfacesConfig.getCurrentInstance(facesContext.getExternalContext());
+        if (config.isRenderFormViewStateAtBegin())
+        {
+            renderViewStateAndHiddenFields(facesContext, component);
+        }
+        
         afterFormElementsStart(facesContext, component);
     }
 
@@ -195,7 +202,23 @@ public class HtmlFormRendererBase
                                                                          target);
         }
 
-        //write hidden input to determine "submitted" value on decode
+        MyfacesConfig config = MyfacesConfig.getCurrentInstance(facesContext.getExternalContext());
+        if (!config.isRenderFormViewStateAtBegin())
+        {
+            renderViewStateAndHiddenFields(facesContext, component);
+        }
+
+        afterFormElementsEnd(facesContext, component);
+        
+        writer.endElement(HTML.FORM_ELEM);
+    }
+    
+    protected void renderViewStateAndHiddenFields(FacesContext facesContext, UIComponent component)
+            throws IOException
+    {
+        ResponseWriter writer = facesContext.getResponseWriter();
+        
+                //write hidden input to determine "submitted" value on decode
         writer.startElement(HTML.INPUT_ELEM, null); // component);
         writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null);
         writer.writeAttribute(HTML.NAME_ATTR, component.getClientId(facesContext) +
@@ -211,8 +234,6 @@ public class HtmlFormRendererBase
         ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
         viewHandler.writeState(facesContext);
 
-        afterFormElementsEnd(facesContext, component);
-        
         List<UIComponent> componentResources = facesContext.getViewRoot().getComponentResources(facesContext,
             FORM_TARGET);
         
@@ -221,8 +242,6 @@ public class HtmlFormRendererBase
            UIComponent child = componentResources.get(i);
            child.encodeAll (facesContext);
         }
-        
-        writer.endElement(HTML.FORM_ELEM);
     }
 
     private static String getHiddenCommandInputsSetName(FacesContext facesContext, UIComponent form)