2015-01-30 2 views
0

하나의 BigDecimal 필드가있는 POJO 객체가 있습니다. 합계입니다. 컨트롤러에서
내가 같은 형태로이 POJO 객체를 추가봄 BigDecimal 입력

MyForm form = new MyForm(); 
model.addAttribute("command", form); 

내 JSP :

내가 initbinder 추가 컨트롤러에서
<form:input path="sum" size="27"/> 

:

binder.registerCustomEditor(BigDecimal.class, new SumEditor()); 

내 SumEditor 클래스의 일부 :

@Override 
    public void setAsText(String text) throws IllegalArgumentException { 
     setValue(parseMoney(text)); 
    } 
    private BigDecimal parseMoney(String str) { 
     try { 
       return new BigDecimal(str); 
      } catch (Exception e) { 
       logger.error("error", e); 
      } 
     return null; 
    } 

하지만 JSP보기에서 (입력 필드에서) | null ________ |
해결 방법 나는 필요로한다 : | ___________ |

당신은 단순히 null 값에 대해 빈 문자열 ("")을 반환하도록 SumEditorgetText 메소드를 오버라이드 (override)

답변

2

:

@Override 
public String getAsText() { 
    if (getValue == null) { 
     return ""; 
    } 
    BigDecimal val = (BigDecimal) getValue(); 
    return val.toStr(); // or whatever conversion you need 
}