dev@javaserverfaces.java.net

Seeking Review jsfri-63: back button and commandLink

From: Ed Burns <Ed.Burns_at_Sun.COM>
Date: Thu, 21 Oct 2004 07:14:23 -0700

Here's one fix that works. I'm not sure if it's clean though.

Issue: jsf-ri 63

This fix introduces a dependency between ButtonRenderer and
CommandLinkRenderer. The buttonRenderer now must clear out the hidden
field value rendered by the commandLink renderer.

M src/com/sun/faces/renderkit/html_basic/ButtonRenderer.java

- use methods newly located to superclass HtmlBasicRenderer to cause
  onclick javascript to be generated that sets the empty string as the
  value of the hidden field used by commandLink entries in this form.
  This is necessary to prevent the value from a previous viewing and
  clicking from being submitted along with the button's value, causing
  *two* ActionEvents to be queued.

M src/com/sun/faces/renderkit/html_basic/CommandLinkRenderer.java
M src/com/sun/faces/renderkit/html_basic/HtmlBasicRenderer.java

- move getMyForm() and getHiddenFieldName() up to HtmlBasicRenderer()

Index: src/com/sun/faces/renderkit/html_basic/ButtonRenderer.java
===================================================================
RCS file:
/cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/renderkit/html_basic/ButtonRenderer.java,v
retrieving revision 1.78
diff -u -r1.78 ButtonRenderer.java
--- src/com/sun/faces/renderkit/html_basic/ButtonRenderer.java 12 Oct 2004
14:39:51 -0000 1.78
+++ src/com/sun/faces/renderkit/html_basic/ButtonRenderer.java 20 Oct 2004
21:56:45 -0000
@@ -17,6 +17,7 @@
 
 import javax.faces.component.UICommand;
 import javax.faces.component.UIComponent;
+import javax.faces.component.UIForm;
 import javax.faces.context.FacesContext;
 import javax.faces.context.ResponseWriter;
 import javax.faces.event.ActionEvent;
@@ -171,7 +172,30 @@
             writer.writeAttribute("value", label, "value");
         }
 
- Util.renderPassThruAttributes(writer, component);
+ // write out some javascript to overwrite any latent commandLink
+ // script fields.
+ UIForm uiform = getMyForm(context, component);
+ if ( uiform == null ) {
+ if (log.isErrorEnabled()) {
+ log.error("component " + component.getId() +
+ " must be enclosed inside a form ");
+ }
+ return;
+ }
+ String formClientId = uiform.getClientId(context);
+
+ StringBuffer sb = new StringBuffer();
+ sb.append("document.forms[");
+ sb.append("'");
+ sb.append(formClientId);
+ sb.append("'");
+ sb.append("]['");
+ sb.append(getHiddenFieldName(context, component));
+ sb.append("'].value='';");
+ writer.writeAttribute("onclick", sb.toString(), null);
+
+ Util.renderPassThruAttributes(writer, component,
+ new String[]{"onclick"});
         Util.renderBooleanPassThruAttributes(writer, component);
 
         if (null != (styleClass = (String)
Index: src/com/sun/faces/renderkit/html_basic/CommandLinkRenderer.java
===================================================================
RCS file:
/cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/renderkit/html_basic/CommandLinkRenderer.java,v
retrieving revision 1.23
diff -u -r1.23 CommandLinkRenderer.java
--- src/com/sun/faces/renderkit/html_basic/CommandLinkRenderer.java 12 Oct 2004
14:39:52 -0000 1.23
+++ src/com/sun/faces/renderkit/html_basic/CommandLinkRenderer.java 20 Oct 2004
21:56:46 -0000
@@ -118,28 +118,6 @@
         return;
     }
 
-
- protected UIForm getMyForm(FacesContext context, UICommand command) {
- UIComponent parent = command.getParent();
- while (parent != null) {
- if (parent instanceof UIForm) {
- break;
- }
- parent = parent.getParent();
- }
- return (UIForm) parent;
- }
-
- protected String getHiddenFieldName(FacesContext context,
- UICommand command) {
- UIForm uiform = getMyForm(context, command);
- String formClientId = uiform.getClientId(context);
- return (formClientId + NamingContainer.SEPARATOR_CHAR +
- UIViewRoot.UNIQUE_ID_PREFIX + "cl");
- }
-
-
-
     public boolean getRendersChildren() {
         return true;
     }
Index: src/com/sun/faces/renderkit/html_basic/HtmlBasicRenderer.java
===================================================================
RCS file:
/cvs/javaserverfaces-sources/jsf-ri/src/com/sun/faces/renderkit/html_basic/HtmlBasicRenderer.java,v
retrieving revision 1.86
diff -u -r1.86 HtmlBasicRenderer.java
--- src/com/sun/faces/renderkit/html_basic/HtmlBasicRenderer.java 12 Oct 2004
14:39:52 -0000 1.86
+++ src/com/sun/faces/renderkit/html_basic/HtmlBasicRenderer.java 20 Oct 2004
21:56:46 -0000
@@ -19,6 +19,7 @@
 import javax.faces.component.NamingContainer;
 import javax.faces.component.UIComponent;
 import javax.faces.component.UIInput;
+import javax.faces.component.UIForm;
 import javax.faces.component.UIParameter;
 import javax.faces.component.UIViewRoot;
 import javax.faces.component.ValueHolder;
@@ -561,5 +562,27 @@
             return value;
         }
     }
+
+ protected UIForm getMyForm(FacesContext context, UIComponent component) {
+ UIComponent parent = component.getParent();
+ while (parent != null) {
+ if (parent instanceof UIForm) {
+ break;
+ }
+ parent = parent.getParent();
+ }
+ return (UIForm) parent;
+ }
+
+ protected String getHiddenFieldName(FacesContext context,
+ UIComponent component) {
+ UIForm uiform = getMyForm(context, component);
+ String formClientId = uiform.getClientId(context);
+ return (formClientId + NamingContainer.SEPARATOR_CHAR +
+ UIViewRoot.UNIQUE_ID_PREFIX + "cl");
+ }
+
+
+
 
 } // end of class HtmlBasicRenderer
-- 
| ed.burns_at_sun.com  | {home: 407 294 2468, office: 408 884 9519 OR x31640}
| homepage:         | http://javaweb.sfbay.sun.com/~edburns/
| aim: edburns0sunw | iim: ed.burns_at_sun.com
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe_at_javaserverfaces.dev.java.net
For additional commands, e-mail: dev-help_at_javaserverfaces.dev.java.net