dev@javaserverfaces.java.net

seeking review 1663

From: Sheetal Vartak <sheetal.vartak_at_oracle.com>
Date: Thu, 26 Aug 2010 17:01:31 -0700

Hello,

The intent of 1663 is to catch the typical user error of putting a button component on a page, but forgetting to wrap them within an <h:form> element.
The check basically is to catch the case where the child is of instance HtmlCommandButton and its parent is UIViewRoot. But I was wondering if this is also an issue when there is a hierarchy such as UIViewRoot ->UIPanel -> HtmlCommandButton ( note the absence of the HtmlForm between UIViewRoot and UIPanel). Please advise on this case.

The changes are as follows :

https://javaserverfaces.dev.java.net/issues/show_bug.cgi?id=1663


SECTION: Modified Files
----------------------------

M jsf-ri/src/main/java/com/sun/faces/facelets/tag/jsf/ComponentSupport.java
M jsf-ri/src/main/java/com/sun/faces/util/MessageUtils.java
M jsf-ri/src/main/resources/com/sun/faces/resources/Messages.properties


SECTION: Diffs
Index: jsf-ri/src/main/java/com/sun/faces/facelets/tag/jsf/ComponentSupport.java
===================================================================
--- jsf-ri/src/main/java/com/sun/faces/facelets/tag/jsf/ComponentSupport.java (revision 8575)
+++ jsf-ri/src/main/java/com/sun/faces/facelets/tag/jsf/ComponentSupport.java (working copy)
@@ -56,15 +56,20 @@
 
 import com.sun.faces.context.StateContext;
 import com.sun.faces.facelets.tag.jsf.core.FacetHandler;
+import com.sun.faces.util.MessageUtils;
 
 import javax.faces.FacesException;
 import javax.faces.component.UIComponent;
 import javax.faces.component.UIPanel;
 import javax.faces.component.UIViewRoot;
+import javax.faces.component.html.HtmlCommandButton;
 import javax.faces.context.FacesContext;
 import javax.faces.view.facelets.FaceletContext;
 import javax.faces.view.facelets.TagAttribute;
 import javax.faces.view.facelets.TagAttributeException;
+import javax.faces.application.FacesMessage;
+
 import java.io.IOException;
 import java.util.Collection;
 import java.util.HashMap;
@@ -406,7 +411,16 @@
      * does not yet exist, make the child the facet.</p>
      */
     public static void addComponent(FaceletContext ctx, UIComponent parent, UIComponent child) {
-
+
+ if (child instanceof HtmlCommandButton &&
+ (parent instanceof UIViewRoot)) {
+ String key = MessageUtils.MISSING_FORM_FOR_BUTTON;
+ Object[] params = new Object[] {};
+
+ FacesMessage m = MessageUtils.getExceptionMessage(key, params);
+ m.setSeverity(FacesMessage.SEVERITY_WARN);
+ ctx.getFacesContext().addMessage(null, m);
+ }
         String facetName = getFacetName(parent);
         if (facetName == null) {
             parent.getChildren().add(child);
Index: jsf-ri/src/main/java/com/sun/faces/util/MessageUtils.java
===================================================================
--- jsf-ri/src/main/java/com/sun/faces/util/MessageUtils.java (revision 8575)
+++ jsf-ri/src/main/java/com/sun/faces/util/MessageUtils.java (working copy)
@@ -334,8 +334,9 @@
             "com.sun.faces.MISSING_COMPONENT_FACET";
     public static final String MISSING_COMPONENT_METADATA =
             "com.sun.faces.MISSING_COMPONENT_METADATA";
+ public static final String MISSING_FORM_FOR_BUTTON =
+ "com.sun.faces.MISSING_FORM_FOR_BUTTON";
 
-
     // ------------------------------------------------------------ Constructors
 
 
Index: jsf-ri/src/main/resources/com/sun/faces/resources/Messages.properties
===================================================================
--- jsf-ri/src/main/resources/com/sun/faces/resources/Messages.properties (revision 8575)
+++ jsf-ri/src/main/resources/com/sun/faces/resources/Messages.properties (working copy)
@@ -180,3 +180,4 @@
 com.sun.faces.MISSING_COMPONENT_ATTRIBUTE_VALUE=The following attribute(s) are required, but no values have been supplied for them: ''{0}''.
 com.sun.faces.MISSING_COMPONENT_FACET=The following facets(s) are required, but no facets have been supplied for them: ''{0}''.
 com.sun.faces.MISSING_COMPONENT_METADATA=The composite component metadata for component with clientid ''{0}'' cannot be found.
+com.sun.faces.MISSING_FORM_FOR_BUTTON=The button component needs to have a Form as its parent. Please add <h:form>.