You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sean Gay <se...@modus.com.au> on 2002/11/11 06:27:13 UTC

HTML:checkbox question

Hey all,

The problem I am having is setting the default state of a checkbox and
having the state retained over form submission.

If I set the default state to be selected (true in the underlying
actionform) when the form is submitted the checkbox remains selected even if
it has been de-selected before form submission.

If I set the default state to be not selected (false in the underlying
actionform) the state before submission is retained.

This wouldn't be a problem if the default state I required is to be not
selected, however Murphy's law does prohibit this and I need the checkbox to
be selected upon form entry. Code examples are provided below. If anyone can
help on this matter I would be most greatful as I can then stop banging my
head on the desk repeatedly. :)

I have altered the reset method, the initial value etc etc, but I just cant
seem to get this working.

Form:
public class KeywordSearchForm extends ActionForm
{
    /**
     * Member variables
     */
    private String businessType     = null;
    private String businessName     = null;
    private String location         = null;
    private boolean serviceArea     = true;    //when this is set to false
everything works correctly.
    private String suburbPostcode   = null;

    /**
     * Getter and Setters
     */
    public String getBusinessType()     { return businessType; }
    public String getBusinessName()     { return businessName; }
    public String getLocation()         { return location; }
    public String getSuburbPostcode()   { return suburbPostcode; }
    public boolean isServiceArea()      { return serviceArea; }

    public void setBusinessType(String businessType)        {
this.businessType = businessType; }
    public void setBusinessName(String businessName)        {
this.businessName = businessName; }
    public void setLocation(String location)                { this.location
= location; }
    public void setServiceArea(boolean serviceArea)         {
this.serviceArea = serviceArea; }
    public void setSuburbPostcode(String suburbPostcode)    {
this.suburbPostcode = suburbPostcode; }

    /**
     * Validation Methods
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest
request)
    {
        ActionErrors errors = new ActionErrors();

        validateBusinessParams(errors);
        validateLocationParams(errors);

        return errors;
    }

    //checks to ensure valid business information has been entered.
    private void validateBusinessParams(ActionErrors errors)
    {
        if((getBusinessName().length() == 0) && (getBusinessType().length()
== 0))
        {
            errors.add("NoBusinessInfo", new
ActionError("error.businessInfo.required"));
        }
    }

    //checks to ensure valid location information
    private void validateLocationParams(ActionErrors errors)
    {
        if(("-1".equals(getLocation())) && (getSuburbPostcode().length() ==
0))
        {
            errors.add("NoLocationInfo", new
ActionError("error.locationInfo.required"));
        }
    }

    public void reset(ActionMapping mapping, HttpServletRequest request)
    {
        this.businessType   = null;
        this.businessName   = null;
        this.location       = null;
        this.serviceArea    = false;
        this.suburbPostcode = null;
    }
}

Action:
Nothing is going on here at all. Just a stub at the moment - processing to
be inserted later.

JSP:
<html:form action="/search/performKeywordSearch.do" focus="businessType">
    <html:text property="businessType" styleClass="inputTxt"
style="width:215px" />
    <html:text property="businessName" styleClass="inputTxt"
style="width:215px" />
    <html:text property="location" styleClass="inputTxt" style="width:215px"
/>
    <html:text property="suburbPostcode" styleClass="inputTxt"
style="width:215px" />
    <html:checkbox property="serviceArea" />
</html:form>


Thanks in advance,
Sean



RE: HTML:checkbox question

Posted by Andrew Hill <an...@gridnode.com>.
G'day Sean,
The way a checkbox in the browser 'works' is as follows:
If the checkbox is not ticked NOTHING is submitted for that field (if true,
of course the value is submitted as usual).
What this means is that when the request hits struts and the contents of
your ActionForm instance are updated, the setter method for the checkbox
field will NOT be called unless the box is ticked. (This isn't struts fault.
Since there is nothing submitted for that field it cant know to call the
setter).
This is where the reset() method comes in. Nonsubmitting checkboxes are the
primary reason for the existence of the reset() method. In your reset method
you set the field to false. Then when the form is populated - which happens
after reset() is called - the checkbox is set back to true if the user had
ticked the box. If the user had not ticked the box the setter is not called
at all - but thats ok cos we set already it to false back in reset() in
anticipation of this possibility...
Now in this case you need to preset the value of that field to true on entry
to the page - but NOT on submit. I reckon theres a couple of ways you could
go about it.
The easiest is probably to have an action that sets it to true before
forwarding to your view.

If you dont go through an action before the view read on...

I dont use JSP myself (cos its evil) so I cant remember exactly what happens
if you go to the view first (all my rendering always has an action invoked
before the view is rendered).
I think the <html:form> tag will create an instance of the form if there
isnt one already. I dont know if it will call reset() at this point or not.
(I suspect it does but cant remember for sure so you will need to look it up
and see which is correct!)
You could probably do something rather convulted in your form code to set
the field to true the *first* time reset is called and false later.
ie:
public class KeywordSearchForm extends ActionForm
{
  private boolean firstTime = true;
  private boolean serviceArea = true;

  public void reset(ActionMapping mapping, HttpServletRequest request)
  {
    if(firstTime)
    {
      serviceArea = true;
      firstTime = false;
    }
    else
    {
      serviceArea = false;
    }
  }
  //etc...
}
If reset() is called after the form is instantiated the above should work
(though its a bit clumsy and Im sure theres a much more elegant way!). If
reset() isnt called when the form is instantiated then you dont need all the
firstTime nonsense. Just set the initial value to true and have the reset()
method always set it to false.

-----Original Message-----
From: Sean Gay [mailto:sean.gay@modus.com.au]
Sent: Monday, November 11, 2002 13:27
To: Struts-Users
Subject: HTML:checkbox question


Hey all,

The problem I am having is setting the default state of a checkbox and
having the state retained over form submission.

If I set the default state to be selected (true in the underlying
actionform) when the form is submitted the checkbox remains selected even if
it has been de-selected before form submission.

If I set the default state to be not selected (false in the underlying
actionform) the state before submission is retained.

This wouldn't be a problem if the default state I required is to be not
selected, however Murphy's law does prohibit this and I need the checkbox to
be selected upon form entry. Code examples are provided below. If anyone can
help on this matter I would be most greatful as I can then stop banging my
head on the desk repeatedly. :)

I have altered the reset method, the initial value etc etc, but I just cant
seem to get this working.

Form:
public class KeywordSearchForm extends ActionForm
{
    /**
     * Member variables
     */
    private String businessType     = null;
    private String businessName     = null;
    private String location         = null;
    private boolean serviceArea     = true;    //when this is set to false
everything works correctly.
    private String suburbPostcode   = null;

    /**
     * Getter and Setters
     */
    public String getBusinessType()     { return businessType; }
    public String getBusinessName()     { return businessName; }
    public String getLocation()         { return location; }
    public String getSuburbPostcode()   { return suburbPostcode; }
    public boolean isServiceArea()      { return serviceArea; }

    public void setBusinessType(String businessType)        {
this.businessType = businessType; }
    public void setBusinessName(String businessName)        {
this.businessName = businessName; }
    public void setLocation(String location)                { this.location
= location; }
    public void setServiceArea(boolean serviceArea)         {
this.serviceArea = serviceArea; }
    public void setSuburbPostcode(String suburbPostcode)    {
this.suburbPostcode = suburbPostcode; }

    /**
     * Validation Methods
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest
request)
    {
        ActionErrors errors = new ActionErrors();

        validateBusinessParams(errors);
        validateLocationParams(errors);

        return errors;
    }

    //checks to ensure valid business information has been entered.
    private void validateBusinessParams(ActionErrors errors)
    {
        if((getBusinessName().length() == 0) && (getBusinessType().length()
== 0))
        {
            errors.add("NoBusinessInfo", new
ActionError("error.businessInfo.required"));
        }
    }

    //checks to ensure valid location information
    private void validateLocationParams(ActionErrors errors)
    {
        if(("-1".equals(getLocation())) && (getSuburbPostcode().length() ==
0))
        {
            errors.add("NoLocationInfo", new
ActionError("error.locationInfo.required"));
        }
    }

    public void reset(ActionMapping mapping, HttpServletRequest request)
    {
        this.businessType   = null;
        this.businessName   = null;
        this.location       = null;
        this.serviceArea    = false;
        this.suburbPostcode = null;
    }
}

Action:
Nothing is going on here at all. Just a stub at the moment - processing to
be inserted later.

JSP:
<html:form action="/search/performKeywordSearch.do" focus="businessType">
    <html:text property="businessType" styleClass="inputTxt"
style="width:215px" />
    <html:text property="businessName" styleClass="inputTxt"
style="width:215px" />
    <html:text property="location" styleClass="inputTxt" style="width:215px"
/>
    <html:text property="suburbPostcode" styleClass="inputTxt"
style="width:215px" />
    <html:checkbox property="serviceArea" />
</html:form>


Thanks in advance,
Sean




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>