Search code examples
javastruts-1

Why struts 1.0 convert empty string to 0L?


I have a form:

public class ActionLogForm extends ActionForm {
    private Long ContractId;
    public Long getContractId() {
        return contractId;
    }

    public void setContractId(Long ContractId) {
        this.contractId= contractId;
    }
}

and in JSP, I have:

<html:hidden property="contractId" styleId="contractId" value="" />

Why actionLogForm.getContractId() in my dao have 0L? How can I change default with this to null?


Solution

  • Struts ActionForm and DynaActionForm documentations clearly specifies.

    If you do not supply an initial attribute, numbers will be initialized to 0 and objects to null.

    I know you're using a Long but (even since before Autoboxing) the following data types are "boxed".

    • Integer/int
    • Double/double
    • Long/long
    • Float/float
    • Boolean/boolean
    • Short/short

    When Struts sees an Number object subtype, instead of its primitive type, it autoboxes it for you, hence why you have a default value of 0. It has its own implementation of the primitive/object type conversion (in BeanUtils). The reason for it, is for backward compatibility with the older Struts 1 versions (which ran on JDK 1.4 and JDK 1.3).

    I hope this helps.