craigmcc 01/02/10 15:31:51 Modified: src/doc struts-html.xml src/share/org/apache/struts/taglib/html LocalStrings.properties MultiboxTag.java web/test html-multibox.jsp Log: Enhance the <html:multibox> tag so that you can determine the value to be returned to the server dynamically. Thus, you can specify either: <html:multibox property="myprop" value="myvalue"/> or (for example) <html:multibox property="myprop"> <bean:write name="mybean" property="valueprop"/> </html:multibox> This change is different from the suggestions on Bugzilla bug report number 402, but is consistent with the way other HTML tags operate. PR: Bugzilla Bug Report #402 Revision Changes Path 1.8 +11 -0 jakarta-struts/src/doc/struts-html.xml Index: struts-html.xml =================================================================== RCS file: /home/cvs/jakarta-struts/src/doc/struts-html.xml,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- struts-html.xml 2001/02/09 19:33:10 1.7 +++ struts-html.xml 2001/02/10 23:31:49 1.8 @@ -2200,6 +2200,17 @@ array-valued property instead of multiple boolean properties. This tag is only valid when nested inside a form tag body. </p> + + <p>The value to be returned to the server, if this checkbox is + selected, must be defined by one of the following methods:</p> + <ul> + <li>Specify a <code>value</code> attribute, whose contents will + be used literally as the value to be returned.</li> + <li>Specify no <code>value</code> attribute, and the nested + body content of this tag will be used as the value to be + returned.</li> + </ul> + </info> <attribute> 1.5 +1 -0 jakarta-struts/src/share/org/apache/struts/taglib/html/LocalStrings.properties Index: LocalStrings.properties =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/LocalStrings.properties,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- LocalStrings.properties 2001/02/07 23:10:44 1.4 +++ LocalStrings.properties 2001/02/10 23:31:50 1.5 @@ -26,6 +26,7 @@ linkTag.type1=Object must be of type Dictionary messageTag.message=Missing message for key {0} messageTag.resources=Missing resources attribute {0} +multiboxTag.value=You must specify the value attribute or nested tag content optionTag.select=Option tag must be nested in a Select tag optionsTag.enumeration=Cannot create enumeration for {0} optionsTag.iterator=Cannot create iterator for {0} 1.3 +58 -33 jakarta-struts/src/share/org/apache/struts/taglib/html/MultiboxTag.java Index: MultiboxTag.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/MultiboxTag.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- MultiboxTag.java 2001/02/09 20:50:18 1.2 +++ MultiboxTag.java 2001/02/10 23:31:50 1.3 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/MultiboxTag.java,v 1.2 2001/02/09 20:50:18 craigmcc Exp $ - * $Revision: 1.2 $ - * $Date: 2001/02/09 20:50:18 $ + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/MultiboxTag.java,v 1.3 2001/02/10 23:31:50 craigmcc Exp $ + * $Revision: 1.3 $ + * $Date: 2001/02/10 23:31:50 $ * * ==================================================================== * @@ -68,6 +68,7 @@ import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.JspWriter; +import org.apache.struts.action.Action; import org.apache.struts.util.BeanUtils; import org.apache.struts.util.MessageResources; @@ -81,7 +82,7 @@ * * @author Ralph Schaer * @author Craig R. McClanahan - * @version $Revision: 1.2 $ $Date: 2001/02/09 20:50:18 $ + * @version $Revision: 1.3 $ $Date: 2001/02/10 23:31:50 $ */ public class MultiboxTag extends BaseHandlerTag { @@ -91,6 +92,13 @@ /** + * The constant String value to be returned when this checkbox is + * selected and the form is submitted. + */ + protected String constant = null; + + + /** * The message resources for this package. */ protected static MessageResources messages = @@ -175,12 +183,43 @@ /** - * Generate the required input tag. + * Process the beginning of this tag. * * @exception JspException if a JSP exception has occurred */ public int doStartTag() throws JspException { + // Defer processing until the end of this tag is encountered + return (EVAL_BODY_TAG); + + } + + + + /** + * Save the body contents of this tag as the constant that we will + * be returning. + * + * @exception JspException if a JSP exception has occurred + */ + public int doAfterBody() throws JspException { + + if (bodyContent != null) + this.constant = bodyContent.getString().trim(); + if ("".equals(this.constant)) + this.constant = null; + return (SKIP_PAGE); + + } + + + /** + * Render an input element for this tag. + * + * @exception JspException if a JSP exception has occurred + */ + public int doEndTag() throws JspException { + // Create an appropriate "input" element based on our parameters StringBuffer results = new StringBuffer("<input type=\"checkbox\""); results.append(" name=\""); @@ -197,7 +236,17 @@ results.append("\""); } results.append(" value=\""); - results.append(this.value); + String value = this.value; + if (value == null) + value = this.constant; + if (value == null) { + JspException e = new JspException + (messages.getMessage("multiboxTag.value")); + pageContext.setAttribute(Action.EXCEPTION_KEY, e, + PageContext.REQUEST_SCOPE); + throw e; + } + results.append(BeanUtils.filter(value)); results.append("\""); Object bean = pageContext.findAttribute(name); String values[] = null; @@ -229,36 +278,11 @@ results.append(prepareEventHandlers()); results.append(prepareStyles()); results.append(">"); - - // Print this field 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_BODY_TAG); - - } - - - - /** - * Optionally render the associated label from the body content. - * - * @exception JspException if a JSP exception has occurred - */ - public int doEndTag() throws JspException { - - if (bodyContent == null) - return (EVAL_PAGE); + // Render this element to our response JspWriter writer = pageContext.getOut(); try { - writer.println(bodyContent.getString().trim()); + writer.println(results.toString()); } catch (IOException e) { throw new JspException (messages.getMessage("common.io", e.toString())); @@ -276,6 +300,7 @@ public void release() { super.release(); + constant = null; name = Constants.BEAN_KEY; property = null; value = null; 1.3 +30 -10 jakarta-struts/web/test/html-multibox.jsp Index: html-multibox.jsp =================================================================== RCS file: /home/cvs/jakarta-struts/web/test/html-multibox.jsp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- html-multibox.jsp 2001/01/10 01:54:22 1.2 +++ html-multibox.jsp 2001/02/10 23:31:51 1.3 @@ -27,7 +27,9 @@ <tr> <th align="right">String 0</th> <td align="left"> - <html:multibox property="stringMultibox" value="String 0"/> + <html:multibox property="stringMultibox"> + String 0 + </html:multibox> </td> <th align="right">(nested) String 0</th> <td align="left"> @@ -38,7 +40,9 @@ <tr> <th align="right">String 1</th> <td align="left"> - <html:multibox property="stringMultibox" value="String 1"/> + <html:multibox property="stringMultibox"> + String 1 + </html:multibox> </td> <th align="right">(nested) String 1</th> <td align="left"> @@ -49,7 +53,9 @@ <tr> <th align="right">String 2</th> <td align="left"> - <html:multibox property="stringMultibox" value="String 2"/> + <html:multibox property="stringMultibox"> + String 2 + </html:multibox> </td> <th align="right">(nested) String 2</th> <td align="left"> @@ -60,7 +66,9 @@ <tr> <th align="right">String 3</th> <td align="left"> - <html:multibox property="stringMultibox" value="String 3"/> + <html:multibox property="stringMultibox"> + String 3 + </html:multibox> </td> <th align="right">(nested) String 3</th> <td align="left"> @@ -71,7 +79,9 @@ <tr> <th align="right">String 4</th> <td align="left"> - <html:multibox property="stringMultibox" value="String 4"/> + <html:multibox property="stringMultibox"> + String 4 + </html:multibox> </td> <th align="right">(nested) String 4</th> <td align="left"> @@ -90,7 +100,9 @@ </td> <th align="right">(nested) 0</th> <td align="left"> - <html:multibox property="nested.intMultibox" value="0"/> + <html:multibox property="nested.intMultibox"> + 0 + </html:multibox> </td> </tr> @@ -101,7 +113,9 @@ </td> <th align="right">(nested) 10</th> <td align="left"> - <html:multibox property="nested.intMultibox" value="10"/> + <html:multibox property="nested.intMultibox"> + 10 + </html:multibox> </td> </tr> @@ -112,7 +126,9 @@ </td> <th align="right">(nested) 20</th> <td align="left"> - <html:multibox property="nested.intMultibox" value="20"/> + <html:multibox property="nested.intMultibox"> + 20 + </html:multibox> </td> </tr> @@ -123,7 +139,9 @@ </td> <th align="right">(nested) 30</th> <td align="left"> - <html:multibox property="nested.intMultibox" value="30"/> + <html:multibox property="nested.intMultibox"> + 30 + </html:multibox> </td> </tr> @@ -134,7 +152,9 @@ </td> <th align="right">(nested) 40</th> <td align="left"> - <html:multibox property="nested.intMultibox" value="40"/> + <html:multibox property="nested.intMultibox"> + 40 + </html:multibox> </td> </tr>