JAVA Basic 강의자료] 수치형 바인드 처리

 

JAVA Basic 강의자료] 수치형 바인드 처리

 

 

 

 

실무개발자를위한 실무교육 전문교육센터학원
www.oraclejava.co.kr에 오시면 보다 다양한 강좌를 보실 수 있습니다.

 

 

 

수치형 바인드 처리

 

 

<소스코드>

 

command 클래스 작성
public class CustomizeNumberBindCommand implements Serializable {
   
    /** serialVersionUID */
    private static final long serialVersionUID = 1L;
 
    private Long amount;
   
    private Double average;
   
    public CustomizeNumberBindCommand() {
       
    }
   
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
   
    // setter, getter 생략
   
} 

 

 

======================

 

 

controller 작성
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
 
@Controller
@RequestMapping("/test/customizeNumberBind")
public class CustomizeNumberBindController {    
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
       
        // 이름을 지정하여 바인드 처리
        NumberFormat amountFormat = NumberFormat.getInstance();
        binder.registerCustomEditor(Long.class, "amount",
                new CustomNumberEditor(Long.class, amountFormat, true));
       
        // 타입을 지정하여 바인드 처리
        DecimalFormat doubleFormat = new DecimalFormat("###,###.###");
        binder.registerCustomEditor(Double.class,
                new CustomNumberEditor(Double.class, doubleFormat, true));
                 //DecimalFormat도 사용가능하다
              }
   
    // @RequestMapping 등 생략
   
}

 

 

==================

 

 

JSP 작성
<h4>수치형 바인드 처리</h4>
<form:form modelAttribute="command" action="${appUrl}/test/customizeNumberBind.html" method="post">
   
    <p>
        <form:label path="amount">합계</form:label>
        <form:input path="amount" />
        <form:errors path="amount" cssClass="errors" />
    </p>
   
    <p>
        <form:label path="average">평균</form:label>
        <form:input path="average" />
        <form:errors path="average" cssClass="errors" />
    </p>
   
    <input type="submit"/>
</form:form> 

 

 

* 날짜 바인드 처리 브라우저로 표시(todo) 

+ Recent posts