Hemant Singh
Hemant Singh

Reputation: 167

How to bind empty numeric field to default value 0 in spring mvc

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.

Upvotes: 1

Views: 3330

Answers (1)

Master Slave
Master Slave

Reputation: 28539

You can set up a global init-binder, something like

@ControllerAdvice
public class GlobalBindingInitializer {

 /* global InitBinder  */

 @InitBinder
 public void binder(WebDataBinder binder) {
  binder.registerCustomEditor(Double.class, new CustomDoubleEditor());
 }
}

and register the following editor

public class CustomDoubleEditor extends PropertyEditorSupport {
    public CustomDoubleEditor() {
    }

    public String getAsText() {
        Double d = (Double) getValue();
        return d.toString();
    }

    public void setAsText(String str) {
        if (str == "" || str == null)
            setValue(0);
        else
            setValue(Double.parseDouble(str));
    }
} 

however, in your context, a more appropriate solution seems to be to simply initialize the instance variables or set a default constructor to the value of 0

private Double kkqty = 0.0;
private Double sogoqty = 0.0;
private Double sesaqty = 0.0;

Upvotes: 1

Related Questions