I have some numeric fields(but not compulsory) in my jsp form eg. kkqty, sogoqty and sesaqty. so when user not giving any input in those fields I m receiving Null at my controller side hence getting null property exception while saving these form data
My POJO (OutletInfo.java)
private Double kkqty;
private Double sogoqty;
private Double sesaqty;
//getter & setters
@Column(name = "KKqty", nullable = false, precision = 10)
public Double getKkqty() {
return this.kkqty;
}
public void setKkqty(Double kkqty) {
this.kkqty = kkqty;
}
@Column(name = "sogoqty", nullable = false, precision = 10)
public Double getSogoqty() {
return this.sogoqty;
}
public void setSogoqty(Double sogoqty) {
this.sogoqty = sogoqty;
}
@Column(name = "sesaqty", nullable = false, precision = 10)
public Double getSesaqty() {
return this.sesaqty;
}
My Controller
@RequestMapping(value = "saveOutletInfo", method = RequestMethod.POST)
public @ResponseBody String saveOutletInfo(OutletInfo outletInfo,HttpServletRequest request){
System.out.print("KKQTY:"+outletInfo.getKkqty());
return this.getMasterService().saveOutletInfo(outletInfo);
}
In controller when I m trying to print all numeric fields , I am receiving null here,hence not able to save .
I need to convert instead of null to default value 0.0 one way I know that i need to check all fields if null then set it to 0.0 but this is pretty much hard coded ,so i want automatic conversion in this scenario.
I gone through some post and came across about @InitBinder but i m unable to use it in such case.
somthing like
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, true));
}
can anyone suggest how i can automatically convert to all my numeric fields to 0.0 when it is null.