2011年9月5日星期一

@InitBinder 日期转换

@Controller
public class AbstractController {
    private  String message;
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Integer.class, null,
                                    new CustomNumberEditor(Integer.class, null, true));
        binder.registerCustomEditor(Long.class, null,
                                    new CustomNumberEditor(Long.class, null, true));
        binder.registerCustomEditor(byte[].class,
                                    new ByteArrayMultipartFileEditor());
        binder.registerCustomEditor(Date.class,new PropertyEditorSupport() {
        public void setAsText(String value) {
            try {
                setValue(new SimpleDateFormat(Constants.DATE_FORMAT).parse(value));
            } catch(ParseException e) {
                setValue(null);
            }
        }

        public String getAsText() {
            return new SimpleDateFormat(Constants.DATE_FORMAT).format((Date) getValue());
        }

    });
    }


}

没有评论:

发表评论