struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Anthony N. Frasso" <>
Subject Re: Dynamically Initializing Checkbox Value
Date Thu, 31 Aug 2006 19:59:25 GMT
My head hurts. :)  Thanks for everyone's help.  I've
gotten a number of responses, it's just that I'm still
confused.  It's obvious I have something drastically
wrong with my understanding of the workflow of Struts,
so I'd like to clear that up now.

Let's take my list page, ListRoles.jsp:

<html:form action="/EditRoles" method="GET">
  <html:hidden property="actionCode" />
  <html:hidden property="roleId" />
  <c:if test="${fn:length(roles) > 0}">
    <table class="list">
      <c:forEach items="${roles}" var="role" step="1">
          <td><c:out value="${}" /></td>
            <c:out value="${role.description}" />
            <html:link href="(some javascript)">
            <html:link href="(some javascript)">

This form lists all roles in the system.  Next to each
role, there are two links: one to edit the role, and
the other to delete it.  These links simply execute
some javascript that populate the hidden form fields
and then submit the form.

When the form is submitted, the form bean is
populated.  Here is the form bean

public class EditRolesForm extends ActionForm {

    private static final long serialVersionUID = 0;

    public static final String ADD_ACTION_CODE =
    public static final String DELETE_ACTION_CODE =
    public static final String EDIT_ACTION_CODE =

    private String actionCode;
    private int roleId;

    public String getActionCode() {

        return actionCode;

    public int getRoleId() {

        return roleId;

    public void setActionCode(String actionCode)

        // some validation done here, and the action
        // code is then set

    public void setRoleId(int roleId) {

        this.roleId = roleId;

Nice and simple. :)  Now, when the form is submitted,
and as I understand it, *after* the form bean is
populated, the execute method is called for the Action
class.  Here is my the Action class I created for this
action (

public class EditRolesAction extends TransactionAction

    public ActionForward execute(ActionMapping
                                 ActionForm form,
        throws Exception {

        EditRolesForm editRolesForm =
            (EditRolesForm) form;

        // at this point, I believe the form is
        // *already* populated.

        // do some computation, place the correct
        // role in the request scope, and determine
        // the correct forward
        request.setAttribute("role", role);
        return mapping.findForward(...);

So now, assuming the user has asked to do an edit, the
EditRole.jsp is generated:

<html:form action="/EditRole" method="POST">
      <td><html:text property="name"
                     value="${}" /></td>
        <html:textarea property="roleDescription"
                       value="${role.description}" />
      <td><html:checkbox ???? /></td>
      <td><html:submit value="Submit" /></td>

Here is the EditRoleForm form bean:

public class EditRoleForm {

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

    private boolean permissionA;
    private boolean permissionB;
    private boolean permissionN;

So the question is... I *don't* have access to the
EditRoleForm form bean in the EditRolesAction Action
class; there, it's the EditRolesForm form bean (sorry
about the confusing terminology; just node that
EditRoles refers to modifying the roles in some
aspect, and EditRole refers to editing a specific

Albert Sapp Wrote:

"I am puzzled by your comment that you don't have a
form bean before you get to the jsp.  We do this all
the time.  Say I want to display a list of inventory
items for a user.  I retrieve a list of inventory
items matching the query data the user gave me and
create an array of inventory item forms to pass to the
jsp.  I convert information from the list inventory
item beans and place each one into the array.  Then
just set the array as a parameter in request scope and
then use that 
parameter to build my jsp.

I do the same if I am providing a means for a user to
update information on an item.  I initialize the form
in the prep action, display the jsp and read the form
in the processing action.  So I read/write to the same

inventory item form bean and read/write to my DAO
inventory item bean for the backend.  The only reason
I use 2 different beans is it was mandated to separate
the view from the model and I need to convert some
data say the unit of issue ID to a actual unit of
issue for the user."

I guess the question is... sure, I can provide the
request scope with the necessary information.  In this
case, the request scope has the Role object as an
attribute.  Are you saying that at the top of the
EditRole.jsp page I initialize the form bean myself,
using servlets?  Or is this "prep" action something
else?  I guess I'm not sure where to do this, as I am
confused.  If you can edit my code, or at least hint
at where I need to do this initialization, it would be
greatly appreciated.

Laurie Harper Wrote:

"Yes you do, the form is passed in as one of the
parameters to execute()."

Isn't this the form bean that was populated *after* it
has been submitted?  I need the form bean that is
going to be used to create the next JSP page.

She continues:

"The typical pattern is for the action to be invoked
via an action mapping, populate the form bean and
forward to the JSP.  It's during this action
processing that you copy your business data into the
form bean, 
thus initializing it.

The form in the JSP is then bound to the form bean,
both reading from and writing to the same place. When
the form is submitted, another action is invoked and
in that action you copy the appropriate data from the
form to wherever you need it to complete the

So it sounds like I might be missing an action here...
I need two Actions per JSP?  One before the JSP is
generated, and one after?  How do I do this?

"To answer your earlier question, you *can* have a
form which takes its values from a bean other than the
form bean -- I think you've already had that working
for some input types. However, in the case of
checkboxes, that's going to be problematic, since the
input's value is doing double duty -- it's specifying
the value that should be submitted *if* the checkbox
is selected, and also specifying the value that the
checkbox property must have for it to be considered
selected already.

If you don't want to copy the permission data from
your model into the form bean before rendering the
form, "

I would like to do this... just don't know how....

"you can do as previously suggested and put the entire
business object into the form bean; that solution 
actually doesn't prevent the properties being
written/updated. There are problems with this approach
for certain types of inputs, but for boolean 
values rendered as checkboxes it should work fine."

Thanks again for everybody's help.  I'm sorry I just
don't quite understand yet what it is I have to do
(thought this would originally be a simple question!).

I think small code snippets would be the most useful
in me understanding what I have to do, because right
now, I *think* I understand the concepts... just don't
know the syntax, and the web/my book aren't terribly

Thanks again, your help is greatly appreciated.
Anthony Frasso

Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message