You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "Paul Benedict (JIRA)" <ji...@apache.org> on 2007/08/20 07:13:34 UTC

[jira] Updated: (STR-1489) [taglib] Need mechanism that lets more than one forms points to the same form bean instance in a HTML page

     [ https://issues.apache.org/struts/browse/STR-1489?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Paul Benedict updated STR-1489:
-------------------------------

    Fix Version/s: 1.4.0
         Assignee:     (was: Struts Developers)

> [taglib] Need mechanism that lets more than one forms points to the same form bean instance in a HTML page
> ----------------------------------------------------------------------------------------------------------
>
>                 Key: STR-1489
>                 URL: https://issues.apache.org/struts/browse/STR-1489
>             Project: Struts 1
>          Issue Type: Improvement
>          Components: Core
>    Affects Versions: 1.1 RC1
>         Environment: Operating System: other
> Platform: All
>            Reporter: charlie
>            Priority: Minor
>             Fix For: 1.4.0
>
>         Attachments: FormTag.java
>
>
> Sometimes in a HTML page, we have more than one HTML forms, where the forms 
> must have different name attribute value, otherwise our javascripts cannot 
> work on those forms; On the other hand, we need to let those forms' form bean 
> point to the same bean instance in seesion scope or request scope. Is there 
> any way we can achieve that goal?
> We tried to use name and "attribute" attribute in action tag of struts-
> config.xml, but we get the HTML form name as the attribute value of action 
> tag, so it make no sense.
> Here is our version of FormTag, but I don't think that's a good way, and also 
> it might affect validator funcation. Hope you guys have a good mechanism.
> //--------------------------------------------------------------------
> package com.aon.chor.org.apache.struts.taglib.html;
> import java.io.IOException;
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
> import javax.servlet.http.HttpSession;
> import javax.servlet.jsp.JspException;
> import javax.servlet.jsp.JspWriter;
> import javax.servlet.jsp.PageContext;
> import javax.servlet.jsp.tagext.TagSupport;
> import org.apache.struts.Globals;
> import org.apache.struts.action.ActionForm;
> import org.apache.struts.action.ActionMapping;
> import org.apache.struts.action.ActionServlet;
> import org.apache.struts.config.FormBeanConfig;
> import org.apache.struts.config.ModuleConfig;
> import org.apache.struts.util.MessageResources;
> import org.apache.struts.util.RequestUtils;
> import org.apache.struts.util.ResponseUtils;
> import org.apache.struts.taglib.html.Constants;
> /**
>  * Custom tag that represents an input form, associated with a bean whose
>  * properties correspond to the various fields of the form.
>  *
>  * @author Craig R. McClanahan
>  * @author Martin Cooper
>  * @author James Turner
>  * @version $Revision: 1.43 $ $Date: 2003/01/28 05:18:03 $
>  */
> public class FormTag extends org.apache.struts.taglib.html.FormTag {
>     // ----------------------------------------------------- Instance Variables
>     /**
>      * The message resources for this package.
>      */
>     protected static MessageResources messages =
>         MessageResources.getMessageResources(Constants.Package 
> + ".LocalStrings");
>     // --------------------------------------------------------- Public Methods
>     /**
>      * Render the beginning of this form.
>      *
>      * @exception JspException if a JSP exception has occurred
>      */
>     public int doStartTag() throws JspException {
>         // Look up the form bean name, scope, and type if necessary
>         lookup();
>         // Create an appropriate "form" element based on our parameters
>         HttpServletResponse response = (HttpServletResponse) 
> pageContext.getResponse();
>         StringBuffer results = new StringBuffer("<form");
>         results.append(" name=\"");
>         
>         //---------------------------------------------------------------------
>         //In a HTML page, we have more than one HTML forms, which must have 
> different name attribute value for 
>         //html:form tag as rule of part HTML spec; however, we need let those 
> three forms' form bean point to 
>         //the same bean in seesion scope or request scope.
>         //We tried to use name and attribute attribute in action tag of struts-
> config.xml, but we get the HTML form
>         //name as the attribute value of action tag
>         //---------------------------------------------------------------------
> 		//results.append(beanName);
>         results.append(mapping.getName());
>         
>         results.append("\"");
>          results.append(" method=\"");
>          results.append(method == null ? "post" : method);
>          results.append("\" action=\"");
>          results.append(response.encodeURL(RequestUtils.getActionMappingURL
> (action, pageContext)));
>          results.append("\"");
>          if (styleClass != null) {
>              results.append(" class=\"");
>             results.append(styleClass);
>             results.append("\"");
>         }
>         if (enctype != null) {
>             results.append(" enctype=\"");
>             results.append(enctype);
>             results.append("\"");
>         }
>         if (onreset != null) {
>             results.append(" onreset=\"");
>             results.append(onreset);
>             results.append("\"");
>         }
>         if (onsubmit != null) {
>             results.append(" onsubmit=\"");
>             results.append(onsubmit);
>             results.append("\"");
>         }
>         if (style != null) {
>             results.append(" style=\"");
>             results.append(style);
>             results.append("\"");
>         }
>         if (styleId != null) {
>             results.append(" id=\"");
>             results.append(styleId);
>             results.append("\"");
>         }
>         if (target != null) {
>             results.append(" target=\"");
>             results.append(target);
>             results.append("\"");
>         }
>         results.append(">");
>         // Add a transaction token (if present in our session)
>         HttpSession session = pageContext.getSession();
>         if (session != null) {
>             String token = (String) session.getAttribute
> (Globals.TRANSACTION_TOKEN_KEY);
>             if (token != null) {
>                 results.append("<input type=\"hidden\" name=\"");
>                 results.append(Constants.TOKEN_KEY);
>                 results.append("\" value=\"");
>                 results.append(token);
>                 if (this.isXhtml()) {
>                     results.append("\" />");
>                 } else {
>                     results.append("\">");
>                 }
>             }
>         }
>         // Print this field to our output writer
>         ResponseUtils.write(pageContext, results.toString());
>         // Store this tag itself as a page attribute
>         pageContext.setAttribute(Constants.FORM_KEY, this, 
> PageContext.REQUEST_SCOPE);
>         // Locate or create the bean associated with our form
>         int scope = PageContext.SESSION_SCOPE;
>         if ("request".equals(beanScope)) {
>             scope = PageContext.REQUEST_SCOPE;
>         }
>         Object bean = pageContext.getAttribute(beanName, scope);
>         if (bean == null) {
>             if (type != null) {
>                 // Backwards compatibility - use explicitly specified values
>                 try {
>                     bean = RequestUtils.applicationInstance(beanType);
>                     if (bean != null) {
>                         ((ActionForm) bean).setServlet(servlet);
>                     }
>                 } catch (Exception e) {
>                     throw new JspException(
>                         messages.getMessage("formTag.create", type, e.toString
> ()));
>                 }
>             } else {
>                 // New and improved - use the values from the action mapping
>                 bean =
>                     RequestUtils.createActionForm(
>                         (HttpServletRequest) pageContext.getRequest(),
>                         mapping,
>                         moduleConfig,
>                         servlet);
>             }
>             if (bean instanceof ActionForm) {
>                 ((ActionForm) bean).reset(mapping, (HttpServletRequest) 
> pageContext.getRequest());
>             }
>             if (bean == null) {
>                 throw new JspException(messages.getMessage("formTag.create", 
> beanType));
>             }
>             pageContext.setAttribute(beanName, bean, scope);
>         }
>         pageContext.setAttribute(Constants.BEAN_KEY, bean, 
> PageContext.REQUEST_SCOPE);
>         // Continue processing this page
>         return (EVAL_BODY_INCLUDE);
>     }
>     /**
>      * Render the end of this form.
>      *
>      * @exception JspException if a JSP exception has occurred
>      */
>     public int doEndTag() throws JspException {
>         // Remove the page scope attributes we created
>         pageContext.removeAttribute(Constants.BEAN_KEY, 
> PageContext.REQUEST_SCOPE);
>         pageContext.removeAttribute(Constants.FORM_KEY, 
> PageContext.REQUEST_SCOPE);
>         // Render a tag representing the end of our current form
>         StringBuffer results = new StringBuffer("</form>");
>         // Render JavaScript to set the input focus if required
>         if (this.focus != null) {
>             results.append("\r\n");
>             results.append(this.getJsStartElement());
>             // xhtml script content shouldn't use the browser hiding trick
>             if (!this.isXhtml()) {
>                 results.append("  <!--\r\n");
>             }
>             // Construct the control name that will receive focus.
>             // This does not include any index.
> 	        //-------------------------------------------------------------
> --------
> 	        //In a HTML page, we have more than one HTML forms, which must 
> have different name attribute value for 
> 	        //html:form tag as rule of part HTML spec; however, we need 
> let those three forms' form bean point to 
> 	        //the same bean in seesion scope or request scope.
> 	        //We tried to use name and attribute attribute in action tag 
> of struts-config.xml, but we get the HTML form
> 	        //name as the attribute value of action tag
> 	        //-------------------------------------------------------------
> --------
>             //String focusControl =
>             //    "document.forms[\"" + beanName + "\"].elements[\"" + 
> this.focus + "\"]";
>             String focusControl =
>                 "document.forms[\"" + mapping.getName() + "\"].elements[\"" + 
> this.focus + "\"]";
>             results.append("  var focusControl = " + focusControl 
> + ";\r\n\r\n");
>             results.append("  if (focusControl.type != \"hidden\") {\r\n");
>             // Construct the index if needed and insert into focus statement
>             String index = "";
>             if (this.focusIndex != null) {
>                 index = "[" + this.focusIndex + "]";
>             }
>             results.append("     focusControl" + index + ".focus();\r\n  } 
> \r\n");
>             if (!this.isXhtml()) {
>                 results.append("  // -->\r\n");
>             }
>             results.append("</script>\r\n");
>         }
>         // Print this value to our output writer
>         JspWriter writer = pageContext.getOut();
>         try {
>             writer.print(results.toString());
>         } catch (IOException e) {
>             throw new JspException(messages.getMessage("common.io", e.toString
> ()));
>         }
>         // Continue processing this page
>         return (EVAL_PAGE);
>     }
>     /**
>      * Returns the starting javascript element formatted for xhtml if needed.
>      */
>     private String getJsStartElement() {
>         String start = "<script type=\"text/javascript\"";
>         if (!this.isXhtml()) {
>             start += " language=\"JavaScript\"";
>         }
>         start += ">\r\n";
>         return start;
>     }
>     /**
>      * Returns true if this tag should render as xhtml.
>      */
>     private boolean isXhtml() {
>         String xhtml =
>             (String) this.pageContext.getAttribute(Globals.XHTML_KEY, 
> this.pageContext.PAGE_SCOPE);
>         return ("true".equalsIgnoreCase(xhtml));
>     }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.