You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Rizwan Merchant <ri...@sysconet.ca> on 2006/05/07 00:17:07 UTC

Issue with checkboxes!

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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Issue with checkboxes!

Posted by 王曾wang_zeng <wa...@gmail.com>.
you can also override the reset() method like this:

public void reset(ActionMapping mapping, HttpServletRequest request){
       this.initialize();
 }

in  the dynamic form class ,there is a initialize() method which will clear
all the properties. The initialize() is called only when the bean is
instantiated.

Re: Issue with checkboxes!

Posted by Richard Yee <ry...@cruzio.com>.
Rizwan,
Have you tried using a source level debugger to watch the value of the 
boolean in the reset method?

You should also view the source of your page to make sure the checkbox 
has the right name.

-Richard


Rizwan Merchant wrote:
>
> Thanks Rick.
> I have 6 checkboxes in that session scope form and 5 of them behave 
> fine (I can set or unset them), only one of them acts funny and cannot 
> be reset to false.
>
> Any idea why this would be happening..just curious!
>
> Rick Reumann wrote:
>> Rizwan Merchant wrote:
>>
>>> and in ExtendedDynaValidatorForm I would have a reset() method as 
>>> follows
>>>
>>> public void reset(ActionMapping mapping, HttpServletRequest request){
>>>    super.reset();
>>>    //not sure what goes here
>>> }
>>>
>>> So my next question is..how to I reset all the booleans in the form? 
>>> Do I have to explicitly reset each of them, or is there a generic 
>>> way to iterate through all of them and reset them? If possible, can 
>>> I see some sample code please?
>>
>> The code above is correct. I just manually set them to false..
>>
>> public void reset(ActionMapping mapping, HttpServletRequest request){
>>     myBoolProperty = false;
>>         myOtherBoolProperty = false;
>> super.reset();
>>
>>   }
>>
>> I suppose if you had a ton of boolean properties you could do some 
>> sort of reflection to look for booleans and set them to false, but I 
>> haven't bothered with that.
>>
>> Also remember, you only need to do this if your ActionForm has 
>> session scope.
>>
>
>
> ---------------------------------------------------------------------
> 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


Re: Issue with checkboxes!

Posted by Rizwan Merchant <ri...@sysconet.ca>.
Thanks Rick.
I have 6 checkboxes in that session scope form and 5 of them behave fine 
(I can set or unset them), only one of them acts funny and cannot be 
reset to false.

Any idea why this would be happening..just curious!

Rick Reumann wrote:
> Rizwan Merchant wrote:
>
>> and in ExtendedDynaValidatorForm I would have a reset() method as 
>> follows
>>
>> public void reset(ActionMapping mapping, HttpServletRequest request){
>>    super.reset();
>>    //not sure what goes here
>> }
>>
>> So my next question is..how to I reset all the booleans in the form? 
>> Do I have to explicitly reset each of them, or is there a generic way 
>> to iterate through all of them and reset them? If possible, can I see 
>> some sample code please?
>
> The code above is correct. I just manually set them to false..
>
> public void reset(ActionMapping mapping, HttpServletRequest request){
>     myBoolProperty = false;
>         myOtherBoolProperty = false;
> super.reset();
>
>   }
>
> I suppose if you had a ton of boolean properties you could do some 
> sort of reflection to look for booleans and set them to false, but I 
> haven't bothered with that.
>
> Also remember, you only need to do this if your ActionForm has session 
> scope.
>


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


Re: Issue with checkboxes!

Posted by Rick Reumann <ri...@gmail.com>.
Rizwan Merchant wrote:

> and in ExtendedDynaValidatorForm I would have a reset() method as follows
> 
> public void reset(ActionMapping mapping, HttpServletRequest request){
>    super.reset();
>    //not sure what goes here
> }
> 
> So my next question is..how to I reset all the booleans in the form? Do 
> I have to explicitly reset each of them, or is there a generic way to 
> iterate through all of them and reset them? If possible, can I see some 
> sample code please?

The code above is correct. I just manually set them to false..

public void reset(ActionMapping mapping, HttpServletRequest request){
	myBoolProperty = false;
         myOtherBoolProperty = false;
super.reset();

   }

I suppose if you had a ton of boolean properties you could do some sort 
of reflection to look for booleans and set them to false, but I haven't 
bothered with that.

Also remember, you only need to do this if your ActionForm has session 
scope.

-- 
Rick

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


Re: Issue with checkboxes!

Posted by Rizwan Merchant <ri...@sysconet.ca>.
Thanks for the responses guys! Please see end of message for follow up 
question :)

So I get most of what you said about overriding the ActionForm. Since I 
am using DynaValidatorForm I will need to extend that class.
Currently my form-bean definition is as follows
        <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>

That would change to

        <form-bean name="userForm" 
type="com.mycompany.mydomain.form.ExtendedDynaValidatorForm">
            <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>

and in ExtendedDynaValidatorForm I would have a reset() method as follows

public void reset(ActionMapping mapping, HttpServletRequest request){
    super.reset();
    //not sure what goes here
}

So my next question is..how to I reset all the booleans in the form? Do 
I have to explicitly reset each of them, or is there a generic way to 
iterate through all of them and reset them? If possible, can I see some 
sample code please?

Thanks again!

-Riz.

Rick Reumann wrote:
> 王曾wang_zeng wrote:
>> The reason is that the form bean is configed to be saved in session 
>> scope,
>> and the reset() method is not called when the form is submitted.
>
> Rizwan,
>
> Meaning... you need to override the ActionForm reset method and set 
> any of your ActionForm booleans to false. Then you'll be all set. 
> (Remember in http, only true checkboxes values are sent with the 
> Request so if your backing ActionForm has session scope and some were 
> already true, there is nothing to set it to false - which is why you 
> need the reset method for session-scoped form beans and boolean values.
>


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


Re: Issue with checkboxes!

Posted by Rick Reumann <ri...@gmail.com>.
王曾wang_zeng wrote:
> The reason is that the form bean is configed to be saved in session scope,
> and the reset() method is not called when the form is submitted.

Rizwan,

Meaning... you need to override the ActionForm reset method and set any 
of your ActionForm booleans to false. Then you'll be all set. (Remember 
in http, only true checkboxes values are sent with the Request so if 
your backing ActionForm has session scope and some were already true, 
there is nothing to set it to false - which is why you need the reset 
method for session-scoped form beans and boolean values.

-- 
Rick


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


Re: Issue with checkboxes!

Posted by 王曾wang_zeng <wa...@gmail.com>.
The reason is that the form bean is configed to be saved in session scope,
and the reset() method is not called when the form is submitted.
>
>
>       <action path="/UserManager"
>            type="org.springframework.web.struts.DelegatingActionProxy"
>            parameter="method"
>            scope="session"
>            name="userForm"
>            validate="false"
>        >
>
>


--
Wang Zeng