You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Albert L. Sapp" <as...@uiuc.edu> on 2006/09/01 16:50:59 UTC

Re: Dynamically Initializing Checkbox Value

Anthony,

See my comments below.  Hope this clears things up a little and I see 
Laurie has made a worthwhile comment about looking at the examples 
available.

HTH,

Al


Anthony N. Frasso wrote:
> 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">
>       <tr>
>         <th>Name</th>
>         <th>Description</th>
>       </tr>
>       <c:forEach items="${roles}" var="role" step="1">
>         <tr>
>           <td><c:out value="${role.name}" /></td>
>           <td>
>             <c:out value="${role.description}" />
>           </td>
>           <td>
>             <html:link href="(some javascript)">
>               Edit
>             </html:link>
>           </td>
>           <td>
>             <html:link href="(some javascript)">
>               Delete
>             </html:link>
>           </td>
>         </tr>
>       </c:forEach>
>     </table>
>   </c:if>
> </html:form>
>
> 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.
>   
Uh, do you do a lot in the javascript besides set the hidden fields?  
Here is an example of how I pass similar information.  Of course, I 
don't have a action to pass as each possible operation has it's own 
action.  Actual 2.  One to prepare the form data to be displayed to the 
user to update and then one to process the data after the user submits 
the form.

 <a  href="<html:rewrite 
href="preUpdateInventoryItem?inventoryID=${inventoryItem.inventoryID}"/>">
        <html:img pageKey="images.updateIcon" border="0"/>
</a>

> When the form is submitted, the form bean is
> populated.  Here is the form bean
> (EditRolesForm.java):
>
> public class EditRolesForm extends ActionForm {
>
>     private static final long serialVersionUID = 0;
>
>     public static final String ADD_ACTION_CODE =
> "add";
>     public static final String DELETE_ACTION_CODE =
> "delete";
>     public static final String EDIT_ACTION_CODE =
> "edit";
>
>     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 (EditRolesAction.java):
>
> public class EditRolesAction extends TransactionAction
> {
>
>     public ActionForward execute(ActionMapping
> mapping,
>                                  ActionForm form,
>                                  HttpServletRequest
>                                      request,
>                                  HttpServletResponse
>                                      response)
>         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);
>   
Ok, instead of request.setAttribute("role", role); try this.

EditRoleForm editRoleForm = new EditRoleForm();
editRoleForm.setID(role.getID);
.
. Set the rest of your form fields.
.
request.setAttribute("role", editRoleForm);

Your jsp, then, has the information it needs to set the initial value of 
all your fields and checkboxes.  This is the way I do it for my edit and 
delete confirmation jsps.
>         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">
>   <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>blah</td>
>       <td><html:checkbox ???? /></td>
>     </tr>
>     <tr>
>       <td>&nbsp;</td>
>       <td><html:submit value="Submit" /></td>
>   </table>
> </html:form>
>
> 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
> role).
>
> 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
> operation."
>
> 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
> helpful.
>
> Thanks again, your help is greatly appreciated.
> 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
>
>
>   


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