You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Caroline Jen <ji...@yahoo.com> on 2004/10/14 20:36:29 UTC

ActionForm Does Not Pick Up the Value of Check Boxes

I have a web page that displays check boxes for user
to make multiple selection: 
[code]
<html-el:form action="/message/SelectAndMail">
......
......
<td>
<html-el:checkbox
property="recipients[${status.index}].selected" />
</td>
<td>
<html-el:hidden
property="recipients[${status.index}].emailAddress" />
<c:out value="${recipientBean.emailAddress}" />
</td>
......
......
</html:el>
[/code]

For testing purpose, I made 6 selections of e-mail
addresses among all check boxes displayed, Those 6
e-mail addresses are my friends'.

Then, the SelectRecipientForm.java extends the
ActionForm class.  In the ActionForm class, the
multiple selection of check boxes by users are
supposed to populate a String Array.

However, the values of the selected check boxes are
not picked up.  Here is the code of the
SelectRecipientsForm.java:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

public class selectRecipientsForm extends ActionForm
{
    // Map of RecipientField objects.
    private Map recipientFieldMap = new HashMap();

    private String[] recipients = new String[0];
    private String[] selectedEmailAddresses;

    public RecipientField getRecipients( int index )
    {
       RecipientField recipientField = (
RecipientField ) recipientFieldMap.get( new Integer(
index ) );
       if ( recipientField == null )
       {
         recipientField = new RecipientField();
         recipientFieldMap.put( new Integer( index ),
recipientField );
       }
       return recipientField;
    }

    public String[] getSelectedEmailAddresses()
    {
       List theEmailAddressList = new ArrayList();

       Iterator i =
recipientFieldMap.values().iterator();
       while( i.hasNext() )
       {
          RecipientField recipientField = (
RecipientField ) i.next();
          if ( recipientField.isSelected() )
          {
             theEmailAddressList.add( "\"" +
recipientField.getEmailAddress() + "\"" );
          }
       }
       return ( String[] )theEmailAddressList.toArray(
new String[theEmailAddressList.size()] );
   }

   public void reset( ActionMapping mapping,
HttpServletRequest request)
   {
       recipients = new String[0];
       selectedEmailAddresses = new String[0];
   }
} 

Of course, I have this RecipientField.java:

public class RecipientField
{
   private String emailAddress = "";

   public String getEmailAddress()
   {
      return emailAddress;
   }

   public void setEmailAddress( String newValue )
   {
      emailAddress = newValue;
   }

   private boolean selected = false;

   public boolean isSelected()
   {
      return selected;
   }

   public void setSelected( boolean newValue )
   {
      selected = newValue;
   }
}
 



		
__________________________________
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
http://promotions.yahoo.com/new_mail 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ActionForm Does Not Pick Up the Value of Check Boxes

Posted by Hubert Rabago <hr...@gmail.com>.
I haven't tried this yet, but I do know that the Struts docs say you
need to use the multibox tag instead of the checkbox tag if you want
to use a string array.

http://struts.apache.org/userGuide/struts-html.html#multibox


On Thu, 14 Oct 2004 11:36:29 -0700 (PDT), Caroline Jen
<ji...@yahoo.com> wrote:
> I have a web page that displays check boxes for user
> to make multiple selection:
> [code]
> <html-el:form action="/message/SelectAndMail">
> ......
> ......
> <td>
> <html-el:checkbox
> property="recipients[${status.index}].selected" />
> </td>
> <td>
> <html-el:hidden
> property="recipients[${status.index}].emailAddress" />
> <c:out value="${recipientBean.emailAddress}" />
> </td>
> ......
> ......
> </html:el>
> [/code]
> 
> For testing purpose, I made 6 selections of e-mail
> addresses among all check boxes displayed, Those 6
> e-mail addresses are my friends'.
> 
> Then, the SelectRecipientForm.java extends the
> ActionForm class.  In the ActionForm class, the
> multiple selection of check boxes by users are
> supposed to populate a String Array.
> 
> However, the values of the selected check boxes are
> not picked up.  Here is the code of the
> SelectRecipientsForm.java:
> 
> import java.util.ArrayList;
> import java.util.HashMap;
> import java.util.Iterator;
> import java.util.List;
> import java.util.Map;
> import javax.servlet.http.HttpServletRequest;
> import org.apache.struts.action.ActionMapping;
> import org.apache.struts.action.ActionForm;
> 
> public class selectRecipientsForm extends ActionForm
> {
>    // Map of RecipientField objects.
>    private Map recipientFieldMap = new HashMap();
> 
>    private String[] recipients = new String[0];
>    private String[] selectedEmailAddresses;
> 
>    public RecipientField getRecipients( int index )
>    {
>       RecipientField recipientField = (
> RecipientField ) recipientFieldMap.get( new Integer(
> index ) );
>       if ( recipientField == null )
>       {
>         recipientField = new RecipientField();
>         recipientFieldMap.put( new Integer( index ),
> recipientField );
>       }
>       return recipientField;
>    }
> 
>    public String[] getSelectedEmailAddresses()
>    {
>       List theEmailAddressList = new ArrayList();
> 
>       Iterator i =
> recipientFieldMap.values().iterator();
>       while( i.hasNext() )
>       {
>          RecipientField recipientField = (
> RecipientField ) i.next();
>          if ( recipientField.isSelected() )
>          {
>             theEmailAddressList.add( "\"" +
> recipientField.getEmailAddress() + "\"" );
>          }
>       }
>       return ( String[] )theEmailAddressList.toArray(
> new String[theEmailAddressList.size()] );
>   }
> 
>   public void reset( ActionMapping mapping,
> HttpServletRequest request)
>   {
>       recipients = new String[0];
>       selectedEmailAddresses = new String[0];
>   }
> }
> 
> Of course, I have this RecipientField.java:
> 
> public class RecipientField
> {
>   private String emailAddress = "";
> 
>   public String getEmailAddress()
>   {
>      return emailAddress;
>   }
> 
>   public void setEmailAddress( String newValue )
>   {
>      emailAddress = newValue;
>   }
> 
>   private boolean selected = false;
> 
>   public boolean isSelected()
>   {
>      return selected;
>   }
> 
>   public void setSelected( boolean newValue )
>   {
>      selected = newValue;
>   }
> }
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail Address AutoComplete - You start. We finish.
> http://promotions.yahoo.com/new_mail
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org