Author: musachy
Date: Thu Jun 14 11:35:04 2007
New Revision: 547350

URL: http://svn.apache.org/viewvc?view=rev&rev=547350
Log:
WW-1350 Autofocus element of a form, the id of the element to get the focus is 
specified in the "focusElement" attribute of the "form" tag

Added:
    
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-12.txt
Modified:
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Form.java
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/ui/FormTag.java
    
struts/struts2/trunk/core/src/main/resources/template/css_xhtml/form-close.ftl
    struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close.ftl
    struts/struts2/trunk/core/src/site/resources/tags/form.html
    
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Form.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Form.java?view=diff&rev=547350&r1=547349&r2=547350
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Form.java 
(original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Form.java 
Thu Jun 14 11:35:04 2007
@@ -110,7 +110,8 @@
     protected String portletMode;
     protected String windowState;
     protected String acceptcharset;
-
+    protected String focusElement;
+    
     protected boolean enableDynamicMethodInvocation = true;
     protected Configuration configuration;
     protected ObjectFactory objectFactory;
@@ -201,6 +202,10 @@
             // we have this if check so we don't do this twice (on open and 
close of the template)
             addParameter("tagNames", new ArrayList());
         }
+        
+        if (focusElement != null) {
+            addParameter("focusElement", findString(focusElement));
+        }
     }
 
     /**
@@ -484,5 +489,10 @@
     @StrutsTagAttribute(description="The accepted charsets for this form. The 
values may be comma or blank delimited.")
     public void setAcceptcharset(String acceptcharset) {
         this.acceptcharset = acceptcharset;
+    }
+
+    @StrutsTagAttribute(description="Id of element that will receive the focus 
when page loads.")
+    public void setFocusElement(String focusElement) {
+        this.focusElement = focusElement;
     }
 }

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/ui/FormTag.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/ui/FormTag.java?view=diff&rev=547350&r1=547349&r2=547350
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/ui/FormTag.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/jsp/ui/FormTag.java
 Thu Jun 14 11:35:04 2007
@@ -48,7 +48,8 @@
     protected String portletMode;
     protected String windowState;
     protected String acceptcharset;
-
+    protected String focusElement;
+    
     public Component getBean(ValueStack stack, HttpServletRequest req, 
HttpServletResponse res) {
         return new Form(stack, req, res);
     }
@@ -66,6 +67,7 @@
         form.setPortletMode(portletMode);
         form.setWindowState(windowState);
         form.setAcceptcharset(acceptcharset);
+        form.setFocusElement(focusElement);
     }
 
 
@@ -107,5 +109,9 @@
 
     public void setAcceptcharset(String acceptcharset) {
         this.acceptcharset = acceptcharset;
+    }
+
+    public void setFocusElement(String focusElement) {
+        this.focusElement = focusElement;
     }
 }

Modified: 
struts/struts2/trunk/core/src/main/resources/template/css_xhtml/form-close.ftl
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/template/css_xhtml/form-close.ftl?view=diff&rev=547350&r1=547349&r2=547350
==============================================================================
--- 
struts/struts2/trunk/core/src/main/resources/template/css_xhtml/form-close.ftl 
(original)
+++ 
struts/struts2/trunk/core/src/main/resources/template/css_xhtml/form-close.ftl 
Thu Jun 14 11:35:04 2007
@@ -1,3 +1,13 @@
 <#include "/${parameters.templateDir}/css_xhtml/control-close.ftl" />
 <#include "/${parameters.templateDir}/simple/form-close.ftl" />
 <#include "/${parameters.templateDir}/xhtml/form-close-validate.ftl" />
+<#if parameters.focusElement?if_exists != "">
+<script type="text/javascript">
+    StrutsUtils.addOnLoad(function() {
+        var element = 
document.getElementById("${parameters.focusElement?html}");
+        if(element) {
+            element.focus();
+        }
+    });
+</script>
+</#if>

Modified: 
struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close.ftl
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close.ftl?view=diff&rev=547350&r1=547349&r2=547350
==============================================================================
--- struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close.ftl 
(original)
+++ struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close.ftl 
Thu Jun 14 11:35:04 2007
@@ -1,3 +1,13 @@
 <#include "/${parameters.templateDir}/xhtml/control-close.ftl" />
 <#include "/${parameters.templateDir}/simple/form-close.ftl" />
 <#include "/${parameters.templateDir}/xhtml/form-close-validate.ftl" />
+<#if parameters.focusElement?if_exists != "">
+<script type="text/javascript">
+    StrutsUtils.addOnLoad(function() {
+        var element = 
document.getElementById("${parameters.focusElement?html}");
+        if(element) {
+            element.focus();
+        }
+    });
+</script>
+</#if>

Modified: struts/struts2/trunk/core/src/site/resources/tags/form.html
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/site/resources/tags/form.html?view=diff&rev=547350&r1=547349&r2=547350
==============================================================================
--- struts/struts2/trunk/core/src/site/resources/tags/form.html (original)
+++ struts/struts2/trunk/core/src/site/resources/tags/form.html Thu Jun 14 
11:35:04 2007
@@ -84,6 +84,14 @@
                                        <td align="left" valign="top">HTML form 
enctype attribute</td>
                                </tr>
                                <tr>
+                                       <td align="left" 
valign="top">focusElement</td>
+                                       <td align="left" valign="top">false</td>
+                                       <td align="left" valign="top"></td>
+                                       <td align="left" valign="top">true</td>
+                                       <td align="left" 
valign="top">String</td>
+                                       <td align="left" valign="top">Id of 
element that will receive the focus when page loads.</td>
+                               </tr>
+                               <tr>
                                        <td align="left" valign="top">id</td>
                                        <td align="left" valign="top">false</td>
                                        <td align="left" valign="top"></td>

Modified: 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java?view=diff&rev=547350&r1=547349&r2=547350
==============================================================================
--- 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java
 (original)
+++ 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java
 Thu Jun 14 11:35:04 2007
@@ -61,13 +61,23 @@
         tag.setEnctype("myEncType");
         tag.setTitle("mytitle");
         tag.setOnsubmit("submitMe()");
-
         tag.doStartTag();
         tag.doEndTag();
 
         verify(FormTag.class.getResource("Formtag-9.txt"));
     }
 
+    public void testFormWithFocusElement() throws Exception {
+        FormTag tag = new FormTag();
+        tag.setTheme("xhtml");
+        tag.setPageContext(pageContext);
+        tag.setAction("testAction");
+        tag.setFocusElement("felement");
+        tag.doStartTag();
+        tag.doEndTag();
+
+        verify(FormTag.class.getResource("Formtag-12.txt"));
+    }
 
     public void 
testFormWithActionAttributeContainingBothActionAndMethodAndNamespace() throws 
Exception {
         FormTag tag = new FormTag();

Added: 
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-12.txt
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-12.txt?view=auto&rev=547350
==============================================================================
--- 
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-12.txt
 (added)
+++ 
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-12.txt
 Thu Jun 14 11:35:04 2007
@@ -0,0 +1,13 @@
+<form id="testAction" name="testAction" onsubmit="return true;" 
action="/testAction.action" method="post">
+<table class="wwFormTable">
+</table>
+</form>
+
+<script type="text/javascript">
+    StrutsUtils.addOnLoad(function() {
+        var element = document.getElementById("felement");
+        if(element) {
+            element.focus();
+        }
+    });
+</script>
\ No newline at end of file


Reply via email to