craigmcc 01/02/21 18:53:33 Modified: src/share/org/apache/struts/taglib/html OptionTag.java OptionsTag.java SelectTag.java src/test/org/apache/struts/test TestBean.java web/test index.jsp web/test/WEB-INF struts-config.xml Added: web/test html-select.jsp Log: Modify the <html:select multiple="true"> implementation so that it correctly marks all of the previously selected options to be marked "selected" when the form is displayed. Add a page to the struts-test application to exercise <html:select> both with and without the multiple="true" setting. Submitted by: Bruno Antunes <[EMAIL PROTECTED]> PR: Bugzilla #260 Revision Changes Path 1.4 +5 -5 jakarta-struts/src/share/org/apache/struts/taglib/html/OptionTag.java Index: OptionTag.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/OptionTag.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- OptionTag.java 2001/02/20 01:48:46 1.3 +++ OptionTag.java 2001/02/22 02:53:30 1.4 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/OptionTag.java,v 1.3 2001/02/20 01:48:46 craigmcc Exp $ - * $Revision: 1.3 $ - * $Date: 2001/02/20 01:48:46 $ + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/OptionTag.java,v 1.4 2001/02/22 02:53:30 craigmcc Exp $ + * $Revision: 1.4 $ + * $Date: 2001/02/22 02:53:30 $ * * ==================================================================== * @@ -82,7 +82,7 @@ * the server if this option is selected. * * @author Craig R. McClanahan - * @version $Revision: 1.3 $ $Date: 2001/02/20 01:48:46 $ + * @version $Revision: 1.4 $ $Date: 2001/02/22 02:53:30 $ */ public class OptionTag extends BodyTagSupport { @@ -230,7 +230,7 @@ results.append("<option value=\""); results.append(value); results.append("\""); - if (value.equals(selectTag.getMatch())) + if (selectTag.isMatched(value)) results.append(" selected"); results.append(">"); String text = text(); 1.3 +10 -8 jakarta-struts/src/share/org/apache/struts/taglib/html/OptionsTag.java Index: OptionsTag.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/OptionsTag.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- OptionsTag.java 2001/01/08 21:36:08 1.2 +++ OptionsTag.java 2001/02/22 02:53:30 1.3 @@ -3,7 +3,7 @@ * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999 The Apache Software Foundation. All rights + * Copyright (c) 1999-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,7 +25,7 @@ * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * - * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software + * 4. The names "The Jakarta Project", "Struts", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] @@ -184,7 +184,6 @@ if (selectTag == null) throw new JspException (messages.getMessage("optionsTag.select")); - String match = selectTag.getMatch(); StringBuffer sb = new StringBuffer(); // If a collection was specified, use that mode to render options @@ -219,7 +218,9 @@ (messages.getMessage("getter.method", property, collection)); } - addOption(sb, value.toString(), label.toString(), match); + String stringValue = value.toString(); + addOption(sb, stringValue, label.toString(), + selectTag.isMatched(stringValue)); } } @@ -240,7 +241,8 @@ String label = value; if (labelsIterator.hasNext()) label = (String) labelsIterator.next(); - addOption(sb, value, label, match); + addOption(sb, value, label, + selectTag.isMatched(value)); } } @@ -283,15 +285,15 @@ * @param sb StringBuffer accumulating our results * @param value Value to be returned to the server for this option * @param label Value to be shown to the user for this option - * @param match Match value that will cause this option to be selected + * @param matched Should this value be marked as selected? */ protected void addOption(StringBuffer sb, String value, String label, - String match) { + boolean matched) { sb.append("<option value=\""); sb.append(value); sb.append("\""); - if (match.equals(value)) + if (matched) sb.append(" selected"); sb.append(">"); sb.append(label); 1.3 +66 -35 jakarta-struts/src/share/org/apache/struts/taglib/html/SelectTag.java Index: SelectTag.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/SelectTag.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- SelectTag.java 2001/01/08 21:36:11 1.2 +++ SelectTag.java 2001/02/22 02:53:30 1.3 @@ -1,13 +1,13 @@ /* - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/SelectTag.java,v 1.2 2001/01/08 21:36:11 craigmcc Exp $ - * $Revision: 1.2 $ - * $Date: 2001/01/08 21:36:11 $ + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/SelectTag.java,v 1.3 2001/02/22 02:53:30 craigmcc Exp $ + * $Revision: 1.3 $ + * $Date: 2001/02/22 02:53:30 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999 The Apache Software Foundation. All rights + * Copyright (c) 1999-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,7 +29,7 @@ * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * - * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software + * 4. The names "The Jakarta Project", "Struts", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] @@ -71,6 +71,8 @@ import javax.servlet.jsp.PageContext; import org.apache.struts.util.BeanUtils; import org.apache.struts.util.MessageResources; +import org.apache.struts.util.RequestUtils; +import org.apache.struts.util.ResponseUtils; /** @@ -79,7 +81,7 @@ * inside a form tag. * * @author Craig R. McClanahan - * @version $Revision: 1.2 $ $Date: 2001/01/08 21:36:11 $ + * @version $Revision: 1.3 $ $Date: 2001/02/22 02:53:30 $ */ public class SelectTag extends BaseHandlerTag { @@ -89,9 +91,9 @@ /** - * The actual value we will match against, calculated in doStartTag(). + * The actual values we will match against, calculated in doStartTag(). */ - protected String match = null; + protected String match[] = null; /** @@ -137,6 +139,12 @@ /** + * The saved body content of this tag. + */ + protected String saveBody = null; + + + /** * How many available options should be displayed when this element * is rendered? */ @@ -161,11 +169,19 @@ /** - * Return the actual match value (only valid from nested tags). + * Does the specified value match one of those we are looking for? + * + * @param value Value to be compared */ - public String getMatch() { + public boolean isMatched(String value) { - return (this.match); + if ((match == null) || (value == null)) + return (false); + for (int i = 0; i < match.length; i++) { + if (value.equals(match[i])) + return (true); + } + return (false); } @@ -252,39 +268,40 @@ results.append(">"); // Print this field to our output writer - JspWriter writer = pageContext.getOut(); - try { - writer.println(results.toString()); - } catch (IOException e) { - throw new JspException - (messages.getMessage("common.io", e.toString())); - } + ResponseUtils.write(pageContext, results.toString()); // Store this tag itself as a page attribute pageContext.setAttribute(Constants.SELECT_KEY, this); - // Calculate the match value we will actually be using + // Calculate the match values we will actually be using if (value != null) { - match = value; + match = new String[1]; + match[0] = value; } else { Object bean = pageContext.findAttribute(name); - if (bean == null) - throw new JspException + if (bean == null) { + JspException e = new JspException (messages.getMessage("getter.bean", name)); + RequestUtils.saveException(pageContext, e); + throw e; + } try { - match = BeanUtils.getProperty(bean, property); + match = BeanUtils.getArrayProperty(bean, property); if (match == null) - match = ""; + match = new String[0]; } catch (IllegalAccessException e) { - throw new JspException + RequestUtils.saveException(pageContext, e); + throw new JspException (messages.getMessage("getter.access", property, name)); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); + RequestUtils.saveException(pageContext, t); throw new JspException (messages.getMessage("getter.result", property, t.toString())); } catch (NoSuchMethodException e) { - throw new JspException + RequestUtils.saveException(pageContext, e); + throw new JspException (messages.getMessage("getter.method", property, name)); } } @@ -297,6 +314,25 @@ /** + * Save any body content of this tag, which will generally be the + * option(s) representing the values displayed to the user. + * + * @exception JspException if a JSP exception has occurred + */ + public int doAfterBody() throws JspException { + + if (bodyContent != null) { + String value = bodyContent.getString(); + if (value == null) + value = ""; + saveBody = value.trim(); + } + return (SKIP_BODY); + + } + + + /** * Render the end of this form. * * @exception JspException if a JSP exception has occurred @@ -308,18 +344,12 @@ // Render a tag representing the end of our current form StringBuffer results = new StringBuffer(); - if (bodyContent != null) - results.append(bodyContent.getString()); + if (saveBody != null) + results.append(saveBody); results.append("</select>"); // Print this value to our output writer - JspWriter writer = pageContext.getOut(); - try { - writer.println(results.toString()); - } catch (IOException e) { - throw new JspException - (messages.getMessage("common.io", e.toString())); - } + ResponseUtils.write(pageContext, results.toString()); // Continue processing this page return (EVAL_PAGE); @@ -337,6 +367,7 @@ multiple = null; name = Constants.BEAN_KEY; property = null; + saveBody = null; size = null; value = null; 1.6 +33 -4 jakarta-struts/src/test/org/apache/struts/test/TestBean.java Index: TestBean.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/test/org/apache/struts/test/TestBean.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- TestBean.java 2001/01/28 02:22:47 1.5 +++ TestBean.java 2001/02/22 02:53:31 1.6 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-struts/src/test/org/apache/struts/test/TestBean.java,v 1.5 2001/01/28 02:22:47 craigmcc Exp $ - * $Revision: 1.5 $ - * $Date: 2001/01/28 02:22:47 $ + * $Header: /home/cvs/jakarta-struts/src/test/org/apache/struts/test/TestBean.java,v 1.6 2001/02/22 02:53:31 craigmcc Exp $ + * $Revision: 1.6 $ + * $Date: 2001/02/22 02:53:31 $ * * ==================================================================== * @@ -72,7 +72,7 @@ * General purpose test bean for Struts custom tag tests. * * @author Craig R. McClanahan - * @version $Revision: 1.5 $ $Date: 2001/01/28 02:22:47 $ + * @version $Revision: 1.6 $ $Date: 2001/02/22 02:53:31 $ */ public class TestBean extends ActionForm { @@ -186,6 +186,21 @@ /** + * A multiple-String SELECT element. + */ + private String[] multipleSelect = { "Multiple 3", "Multiple 5", + "Multiple 7" }; + + public String[] getMultipleSelect() { + return (this.multipleSelect); + } + + public void setMultipleSelect(String multipleSelect[]) { + this.multipleSelect = multipleSelect; + } + + + /** * A nested reference to another test bean (populated as needed). */ private TestBean nested = null; @@ -222,6 +237,20 @@ public void setShortProperty(short shortProperty) { this.shortProperty = shortProperty; + } + + + /** + * A single-String value for a SELECT element. + */ + private String singleSelect = "Single 5"; + + public String getSingleSelect() { + return (this.singleSelect); + } + + public void setSingleSelect(String singleSelect) { + this.singleSelect = singleSelect; } 1.6 +1 -0 jakarta-struts/web/test/index.jsp Index: index.jsp =================================================================== RCS file: /home/cvs/jakarta-struts/web/test/index.jsp,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- index.jsp 2001/02/13 01:56:08 1.5 +++ index.jsp 2001/02/22 02:53:32 1.6 @@ -23,6 +23,7 @@ <h3>HTML Tags</h3> <ul> <li><a href="html-multibox.jsp"><html:multibox></a></li> +<li><a href="html-select.jsp"><html:select></a></li> <li><a href="html-setters.jsp">Scalar Setters</a></li> </ul> 1.1 jakarta-struts/web/test/html-select.jsp Index: html-select.jsp =================================================================== <%@ page language="java"%> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <html:html> <head> <title>Test html:select Tag</title> <% String multipleValues[] = { "Multiple 0", "Multiple 1", "Multiple 2", "Multiple 3", "Multiple 4", "Multiple 5", "Multiple 6", "Multiple 7", "Multiple 8", "Multiple 9" }; pageContext.setAttribute("multipleValues", multipleValues); %> </head> <body bgcolor="white"> <div align="center"> <h1>Test struts-html Select Tag</h1> </div> Whatever changes you make to properties should be reflected when the page is redisplayed. Press "Save" to update, or "Cancel" to return to the main menu. <html:form action="html-select.do"> <table border="0" width="100%"> <tr> <th align="right">Single Select Allowed:</th> <td align="left"> <html:select property="singleSelect" size="10"> <html:option value="Single 0">Single 0</html:option> <html:option value="Single 1">Single 1</html:option> <html:option value="Single 2">Single 2</html:option> <html:option value="Single 3">Single 3</html:option> <html:option value="Single 4">Single 4</html:option> <html:option value="Single 5">Single 5</html:option> <html:option value="Single 6">Single 6</html:option> <html:option value="Single 7">Single 7</html:option> <html:option value="Single 8">Single 8</html:option> <html:option value="Single 9">Single 9</html:option> </html:select> </td> </tr> <tr> <th align="right">Multiple Select Allowed:</th> <td align="left"> <html:select property="multipleSelect" size="10" multiple="true"> <html:options name="multipleValues" labelName="multipleValues"/> </html:select> </td> </tr> <tr> <td align="right"> <html:submit>Save</html:submit> </td> <td align="left"> <html:reset>Reset</html:reset> <html:cancel>Cancel</html:cancel> </td> </tr> </table> </html:form> </html:html> 1.3 +10 -0 jakarta-struts/web/test/WEB-INF/struts-config.xml Index: struts-config.xml =================================================================== RCS file: /home/cvs/jakarta-struts/web/test/WEB-INF/struts-config.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- struts-config.xml 2001/01/08 22:21:00 1.2 +++ struts-config.xml 2001/02/22 02:53:33 1.3 @@ -16,6 +16,7 @@ </global-forwards> <action-mappings> + <action path="/html-multibox" type="org.apache.struts.test.HtmlSettersAction" name="testbean" @@ -23,6 +24,15 @@ validate="false"> <forward name="input" path="/html-multibox.jsp"/> </action> + + <action path="/html-select" + type="org.apache.struts.test.HtmlSettersAction" + name="testbean" + scope="session" + validate="false"> + <forward name="input" path="/html-select.jsp"/> + </action> + <action path="/html-setters" type="org.apache.struts.test.HtmlSettersAction" name="testbean"