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


Reply via email to