Here's the code and the .tld.

At the moment the SubmitTag class is in our own package (com.RuntimeCollective.struts)
but it would be trivial to move it.

(I didn't integrate it into the struts:html tag library, cos I wasn't sure if it was appropriate)

Joe Faith
http://www.runtime-collective.com
T: (+44) 01273 234294
M: (+44) 07968 292064


Andy Noble wrote:

Hi Joe,

I would certainly be interested in your code for this tag, as I have decided
to make javascript a prerequisite for my project.

Regards
Andy

----- Original Message -----
From: Joe Faith <[EMAIL PROTECTED]>
To: Struts Developers List <[EMAIL PROTECTED]>
Sent: Thursday, November 15, 2001 10:08 AM
Subject: Re: [SUBMIT] LookupDispatchAction - how to handle multiple
html:submit buttons

> We have also implemented a multiple submit button system; but in our
> case we wrote a custom tag that generates javascript that sets a property
> to a specified value and submits the form when you hit the button.
>
> The tags look something like:
>
>     <my_submit property="formAction" value="delete" label="Delete this
> record"/>
>     <my_submit property="formAction" value="save" label="Save changes"/>
>     <my_submit property="formAction" value="copy" label="Copy this
record"/>
>
> etc
>
> A single action object can then use the value of formAction to decide what
to
> do in each case.
> This has the side-effect of reducing the number of Action classes needed.
>
> However, the tags generate javascript and I wasn't sure if this fitted the
> struts philosophy; so I
> haven't submitted the code changes. But if anyone was interested I could
make
> it available.
>
> Joe Faith
>
> Runtime Collective, Ltd
> T: (+44) 01273 234294
>
> Erik Hatcher wrote:
>
> > As promised earlier today, here is my contribution to the multiple
> > html:submit button saga (Ted, time to update your FAQ! :)....
>

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
  "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd";>

<taglib>

  <tag>
    <name>submit</name>
    <tagclass>com.RuntimeCollective.struts.tag.SubmitTag</tagclass>
    <bodycontent>empty</bodycontent>
    <info>

        Produces a submit button for which it is possible to define both the label and 
submit value seperately

        Attributes:

        property  - [optional] Name of the property whose value will be submitted. 
[defaults to "action"]
        name      - The name of the form bean that holds the above property.
        label     - The label to write on the button.
        value     - [optional] The value to set 'property' to [defaults to use the 
corresponding bean property value]
        htmlJSInput - [optional] You must set this to true if the form contains an 
'htmlJSInput' tag

    </info>
    <attribute>
      <name>label</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>name</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>value</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>property</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>htmlJSInput</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
   <attribute>
      <name>name</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>value</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>property</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>htmlJSInput</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

</taglib>
package com.RuntimeCollective.struts.tag;

import java.io.IOException;
import java.io.PrintStream;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.apache.struts.action.Action;
import org.apache.struts.util.BeanUtils;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

/** A custom JSP tag that produces a submit button whose label is different from the submitted value. This tag takes the following attributes:
 * <ul>
 * <li> <code> name </code> - The name of the current form. [Mandatory]. </li>
 * <li> <code> label </code> - The label of the submit button. [Mandatory]. </li>
 * <li> <code> value </code> - The value of property to submit. Defaults to property's current value.  [Optional]. </li>
 * <li> <code> property </code> - The property to be set to value. Defaults to "formAction". [Optional]. </li>
 * <li> <code> htmlJSInput </code> - You must set this to true if the form contains an <code>htmlJSInput</code> tag [Optional]. </li>

 * </ul>
 * This allows you to either specify the value explicitly, or take it from a bean's property.
 * <p><strong>Note</strong> a javascript bug means that property must <strong>not</strong> be set to "submit" or "action".
 * <p><strong>Note</strong> this tag must be used inside a form.
 * <p><b>Examples:</b>
 * <blockquote><code>&lt;rs:submit name="lessonForm" label="Save changes to this lesson"/&gt;</code></blockquote>
 * The submitted form will contain a parameter "formAction" (the default) that has the existing value of <code>lessonForm</code>'s "formAction" property.
 * <p>
 * <blockquote><code>&lt;rs:submit name="lessonForm" label="Delete this lesson" value="delete"/&gt;</code></blockquote>
 * The submitted form will contain a parameter "formAction" (the default) that has the value "delete".
 * 
 * @version $Id: SubmitTag.java,v 1.11 2001/10/30 17:48:45 joe Exp $
 */
public class SubmitTag extends TagSupport {

    // == Properties =================================================== 


    /** The label of the submit button. */ 
    protected String label = ""; 
    /** Get the label of the submit button. */ 
    public String getLabel() { return this.label; } 
    /** Set the label of the submit button. */ 
    public void setLabel(String label) { this.label = label; } 

    /** The value of property to submit. */ 
    protected String value = ""; 
    /** Get the value of property to submit. */ 
    public String getValue() { return this.value; } 
    /** Set the value of property to submit. */ 
    public void setValue(String value) { this.value = value; } 

    /** The property to be set to value. */ 
    protected String property = "formAction"; 
    /** Get the property to be set to value. */ 
    public String getProperty() { return this.property; } 
    /** Set the property to be set to value. */ 
    public void setProperty(String property) { this.property = property; } 

    /** The name of the form. */ 
    protected String name = ""; 
    /** Get the name of the form. */ 
    public String getName() { return this.name; } 
    /** Set the name of the form. */ 
    public void setName(String name) { this.name = name; } 

    /** Whether this form contains an htmlJSInput tag. */ 
    protected String htmlJSInput = "false"; 
    /** Get whether this form contains an htmlJSInput tag. */ 
    public String getHtmlInput() { return this.htmlJSInput; } 
    /** Set whether this form contains an htmlJSInput tag. */ 
    public void setHtmlInput(String htmlJSInput) { this.htmlJSInput = htmlJSInput; } 


    // == Tag methods ==================================

    public int doStartTag() throws JspException {


	// If the value is null, try and get it from
	// the named bean's property
	if ( value.equals("") ) {
	    Object bean = pageContext.findAttribute(name);
	    if (bean == null)
		throw new JspException ("Can't access bean! "+name);
	    try {
		String beanValue = BeanUtils.getSimpleProperty(bean, property);
		System.out.println("From BeanUtils.getSimpleProperty, we get out: "+beanValue);
		if (beanValue == null)
		    beanValue = "";
		setValue(beanValue);
	    } catch (Exception e) {
		throw new JspException ("Error in submit tag: "+e);
	    }
	}


	//results.append(ResponseUtils.filter(value));


	String html = "";

	// first check whether this is the first submit button for this form property
	if ( pageContext.getAttribute("rs_submit_"+name+"_"+property)==null ) {
	    
	    // create the javascript for the submit function for this form.
	    html = "<script language=javascript> function rs_submit_"+name+"_"+property+"(newValue) { \n"
		+ "document."+name+"."+property+".value = newValue;\n";

	    // If this form contains an htmlJSInput tag, we also need to call its JavaScript method
	    if (htmlJSInput.equals("true")) {
		html = html + "getHTMLEditorAppletValue()\n";
	    }

	    html = html	+ "document."+name+".submit();\n"
		+" };\n"
		+ "</script>\n";

	    // Add the attribute we're changing, with a dummy value
	    html = html + "<input type=\"hidden\" name=\""+property+"\" value=\"x\">\n";
	}

	// add the button code.
	html = html + "<input type=\"button\" value=\""+label+"\" onclick=\"rs_submit_"+name+"_"+property+"('"+value+"');\">";

	// write the code.
	try {
	    pageContext.getOut().println( html );
	} catch (IOException e) {
	    throw new JspTagException("I/O Exception " + e.getMessage() );
	}

	// and add the page context flag indicating that the javascript for this property is available

	pageContext.setAttribute("rs_submit_"+name+"_"+property, "true");

	return SKIP_BODY;
    }


    public void release() {
	super.release();
	property = "formAction";
	name = "";
	label = "";
	value = "";
    }
}





--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to