struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Anthony N. Frasso" <afra...@yahoo.com>
Subject Re: Dynamically Initializing Checkbox Value
Date Tue, 29 Aug 2006 21:05:04 GMT
Hello all,

OK.  I admit I am thoroughly confused. :)

> Note that there's a
> special 
> consideration for checkboxes, though: if a checkbox
> is de-selected when 
> the form is submitted, *no* URL parameter will be
> included for it. As a 
> result, Struts has no way to automatically clear
> your form property when 
> the checkbox is de-selected.
> 
> The solution to this is to have your form's reset()
> method clear the 
> form property to whatever value you choose to
> represent the de-selected 
> state (e.g. "no"). Before you display the page, you
> should set the 
> property to "yes" or "no" as appropriate, to get the
> checkbox 
> appropriately selected on the page. The reset()
> method takes care of 
> making sure the property is 'switched off', and the
> normal form submit 
> processing takes care of making sure it's 'switched
> on' again if the 
> checkbox was selected.

I think I understand that.  I've seen that information
on other struts pages, and I think that shouldn't be a
problem.  I'm more concerned about getting the
checkbox value *when the page initially loads*
correct.  At this point, I could care less what the
form looks like when the form is submitted. :)

So let me go through and post all of my code (it's
short), and let's see if anyone can alert me to a
solution:

The following is the definition of a class named Role.
 As one can imagine, it defines a role for a user in
my application:

public class Role {

    private int id;
    private String name;
    private String description;

    private boolean permissionA;
    private boolean permissionB;
    ...
    private boolean permissionN;
}

Each of those properties has getter and setter
methods.  By the way, in the *actual* Role class, the
permission booleans are named such things as
"canAccessProjects" and "canEditCustomers" and other
such actual permissions, which is why I am not using
an array of booleans in this case.  I want to refer to
them by name.

Now I wanted to create a JSP that contained a form,
that allowed a user to edit a role.  Here is the
definition of my form bean (called EditRoleForm):

public class EditRoleForm extends ActionForm {

    private boolean roleId;
    private boolean roleName;
    private boolean roleDescription;

    private boolean rolePermissionA;
    private boolean rolePermissionB;
    ...
    private boolean rolePermissionN;
}

In this instance, the form bean looks quite similar to
the role bean.

In my JSP, I have the following:

<html:form action="/EditRole" method="POST">
  <table>
    <tr>
      <td>Name:</td>
      <td><html:text property="name"
                     value="${role.name}" /></td>
    </tr>
    <tr>
      <td>Description:</td>
      <td><html:textarea property="roleDescription"
                         value="${role.description}"
           /></td>
    </tr>
    <tr>
      <td>Permission A:</td>
      <td><html:checkbox property="rolePermissionA"
                         value="true" /></td>
    </tr>
    <tr>
      <td>Permission B:</td>
      <td><html:checkbox property="rolePermissionB"
                         value="true" /></td>
    </tr>
    ...
    <tr>
      <td>Permission N:</td>
      <td><html:checkbox property="rolePermissionN"
                         value="true" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><html:submit value="Submit" /></td>
  </table>
</html:form>

As you can see, all of the checkboxes are going to be
initialized blank.  I would instead prefer them to be
initialized with the value in the role within the
request scope.  I was able to do this using the
"value" parameter in the text and textarea tags, but
the value parameter is used differently for the
checkbox tag.

I hope this clears it up.

One final note: After reading everyone's responses,
and also reading around on the web, I seem to be
getting an idea that the name of the property in the
Role bean and the Form bean should be identical, and
this is the way it is able to initialize the value in
the checkbox, and also set the correct value in the
form bean when it is submitted.  This seems like a bad
idea.  For one, how do we know that there is a
one-to-one mapping of beans to form beans?  If I'm
modifying a few beans all on the same JSP page, I
certainly don't want there to be confusion.

I hope I've made myself clear, and I look forward to
everyone's response.  I really appreciate the time and
effort everyone has taken in helping me out thus far.

Regards,
Anthony Frasso

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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


Mime
View raw message