PDA

View Full Version : Default Zero's in Integer Fields (Int) in Spring Forms


rsrch
May 2nd, 2008, 09:53 AM
Hi,

I am relatively new to spring. In my forms, I am trying to accept some integer values for fields. But the problem is, even though I never intialized these fields in bean class, my spring form is displaying these fields with defaults zero. Since in my requirements they are optional fields. So when I press submit the web page these defaults zero in these fields to the server.
I don't want to this happen. I want my form to show just an empty input field
with out '0'. How can I fix this.

Form Code:

<form:form method="post" commandName="informationRecords">
<table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="2">

<tr>
<td align="right" width="20%"><font face="arial" size="2">Processing Step<font color="red">*</font>:</font></td>
<spring:bind path="informationRecords.stepind">
<td width="20%">
<font face="arial" size="2">
<input type="text" name="stepind" value="<c:out value="${status.value}"/>">
</font>
</td>
<td width="60%">
<font face="verdana" size="2" color="red">
<c:out value="${status.errorMessage}"/>
</font>
</td>
</spring:bind>
</tr>
<tr>
<td align="right" width="20%"><font face="arial" size="2">Run Sequence<font color="red">*</font>:</font></td>
<spring:bind path="informationRecords.runseq">
<td width="20%">
<font face="arial" size="2">
<input type="text" name="runseq" value="<c:out value="${status.value}"/>">
</font>
</td>
<td width="60%">
<font face="verdana" size="2" color="red">
<c:out value="${status.errorMessage}"/>
</font>
</td>
</spring:bind>
</tr>

<tr>
<td align="right" width="20%"><font face="arial" size="2">Total Records:</font></td>
<spring:bind path="informationRecords.totalrecords">
<td width="20%">
<font face="arial" size="2">
<input type="text" name="totalrecords" value="<c:out value="${status.value}"/>">
</font>
</td>
<td width="60%">
<font face="verdana" size="2" color="red">
<c:out value="${status.errorMessage}"/>
</font>
</td>
</spring:bind>
</tr>

</table>
<br>
<spring:hasBindErrors name="informationRecords">
<font face="verdana" color="red"><b>Please fix all errors!</b></font>
</spring:hasBindErrors>
<br>
<br>
<input type="submit" value="Submit">
<input type="reset" value="Clear">
</form:form>


JavaBean Code:

public class InformationRecords implements Serializable {

/**
*
*/
private static final long serialVersionUID = -8138305041081613778L;


private int stepind;
private int runseq;
private int totalrecords;

public void setStepind(int stepind) {
this.stepind = stepind;
}

public int getStepind() {
return stepind;
}

public void setRunseq(int runseq) {
this.runseq = runseq;
}

public int getRunseq() {
return runseq;
}

public void setTotalrecords(int totalrecords) {
this.totalrecords = totalrecords;
}

public int getTotalrecords() {
return totalrecords;
}

}


There must be a simple fix for this. May be if this already posted in these forums earlier, please give me the link to the solution.

Your quick response will be really appreciated.

Thanks,
rsrch

noon
May 3rd, 2008, 02:26 AM
One solution is to use Integer wrapper class instead of the int. int default value is 0 and Integer's is null.

It is possible to define null value (empty value) behaviour for Integer's by setting propertyEditors.