Hi,
I am having issue using checkboxes with the struts framework and DynaValidatorForm. I have a userForm that contains an object called "user" and another object called "userRestrictions" (which defined permissions for that user.

When I access the page for the first time all permissions are disbabled. If I check on of the boxes and save the user info, the corresponding permission gets set in the db correctly. When I reload the page and try to set the same permission off (or false), it doesnt happen. This is definitely a problem with struts as a debug message in the action shows that the permission was not turned off on the page.

We tried a few things as shown in the code below, but cant get this to work properly. Funny thing is that some checkboxes work and others dont. Please help!!

Thanks,
-Riz.

Here is my form-bean and action-mapping
<form-bean name="userForm" type="org.apache.struts.validator.DynaValidatorForm">
           <form-property name="user" type="com.cwsi.eshipper.model.User"/>
<form-property name="restrictions" type="com.cwsi.eshipper.model.UserRestrictions"/> <form-property name="customer" type="com.cwsi.eshipper.model.Customer"/> <form-property name="creditcard" type="com.cwsi.eshipper.model.CreditCard"/>
       </form-bean>
       <action path="/UserManager"
           type="org.springframework.web.struts.DelegatingActionProxy"
           parameter="method"
           scope="session"
           name="userForm"
           validate="false"
       >

edit and save method in UserAction
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { DynaActionForm userForm = (DynaActionForm)form; userForm.reset(mapping, request);
       String id = request.getParameter("id");
User currentUser = getUser(request); if(currentUser.getType().equals("customer") && !currentUser.getSubType().equals("admin")) {
           return mapping.findForward("auth");
       }
if(id != null) {
           User user = userManager.getUser(id);
if(user == null) {
               ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("user.missing"));
               saveMessages(request,messages);

               return list(mapping,form,request,response);
           }
           if(currentUser.getType().equals("customer")) {
               if(user.getCustomerId() != currentUser.getCustomerId()) {
                   ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("user.missing"));
                   saveMessages(request,messages);
return list(mapping,form,request,response); }
           }
userForm.set("user",user); if(request.getAttribute("newCustomer") != null) {
               //we got forwarded here from the customer manager
Customer customer = (Customer)request.getAttribute("newCustomer");
               user.setCustomerInfo(customer);
               user.setCustomerId(customer.getId().longValue());
           }
if(user.getCustomerId() > 0) {
               userForm.set("customer",user.getCustomerInfo());
           } else {
               userForm.set("customer",new Customer());
           }
       }
if(request.getAttribute("newCustomer") != null) {
           //we got forwarded here from the customer manager
Customer customer = (Customer)request.getAttribute("newCustomer");
           User user = (User)userForm.get("user");
           user.setCustomerInfo(customer);
           user.setCustomerId(customer.getId().longValue());
           userForm.set("customer",customer);
       }
       request.setAttribute("tabs",userManager.getTabs());
if(currentUser.getType().equals("customer")) {
           return mapping.findForward("edit_customer");
       } else {
           return mapping.findForward("edit");
       }
   }
public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
       DynaActionForm userForm = (DynaActionForm)form;
       User user = (User)userForm.get("user");
       Customer customer = (Customer)userForm.get("customer");
User currentUser = getUser(request); if(currentUser.getType().equals("customer") && !currentUser.getSubType().equals("admin")) {
           return mapping.findForward("auth");
       }
if(currentUser.getType().equals("customer") && user.getCustomerId() != currentUser.getCustomerId()) {
             ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("user.missing"));
           saveMessages(request,messages);
return list(mapping,form,request,response);
       }
if(this.isCancelled(request)) {
           logger.debug("cancelled");
           request.getSession().removeAttribute("userForm");
           return list(mapping,form,request,response);
       }
tearUserRestrictions(request,user.getRestrictions());
       if(customer.getId().longValue() >0) {
           user.setCustomerId(customer.getId());
       }
       userManager.saveUser(user,customer);
ActionMessages messages = new ActionMessages(); messages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("user.saved"));
       saveMessages(request,messages);
request.getSession().removeAttribute("userForm"); return list(mapping,form,request,response);
   }
/*
    * HACK
    * There seems to be a bug in the struts framework for html:checkboxes
* which are in a dynavalidatorform. So we tear checkboxes that form manually.
    */
void tearUserRestrictions(HttpServletRequest request, UserRestrictions restrictions) { logger.debug("DEBUGGING::::" + restrictions.getShippingLog() + " " + restrictions.getQuote()); restrictions.setShippingOrder(tearCheckbox(request,"user.restrictions.shippingOrder")); restrictions.setShippingLog(tearCheckbox(request,"user.restrictions.shippingLog")); restrictions.setQuote(tearCheckbox(request,"user.restrictions.quote")); restrictions.setProcedures(tearCheckbox(request,"user.restrictions.procedures")); restrictions.setMaintenance(tearCheckbox(request,"user.restrictions.maintenance")); restrictions.setDefaultTab(tearSelect(request,"user.restrictions.defaultTab"));
   }
boolean tearCheckbox(HttpServletRequest request, String param) {
       String val = request.getParameter(param);
       if(val == null) {return false;}
       return true;
   }

jsp sample code
     <tr>
       <td class="form">Shipping Log</td>
       <td><html:checkbox property="user.restrictions.shippingLog"/></td>
       <td>Quote</td>
       <td><html:checkbox property="user.restrictions.quote"/></td>
     </tr>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to