SpringMVC에서 날짜와 같은 유형은 콩으로 정의됩니다. 처리가 완료되지 않으면 날짜와 두 배를 묶을 수 없습니다.
솔루션은 Spring MVC에서 제공 한 @initbinder 태그를 사용하는 것입니다.
내 프로젝트에서 BaseController에 Method Initbinder를 추가하고 @initbinder 주석을 사용합니다. 그런 다음 Spring MVC는 양식을 바인딩하기 전에 이러한 편집기를 등록합니다. 물론 귀찮게하지 않으면 각 컨트롤러에 별도로 쓸 수도 있습니다. 나머지 컨트롤러는이 클래스를 상속합니다. Spring 자체는 CustomDateEditor, CustomBooleEditor, CustomNumberEditor 등과 같은 많은 구현 클래스를 기본적으로 제공합니다.
물론, 우리는 우리 자신과 함께 제공되는 편집자 클래스를 사용할 수 없습니다. 그들 중 몇 명을 직접 만들어 봅시다.
org.springframework.beans.propertyeditors.propertieseditor; 공개 클래스 Doubleeditor는 PropertiesEditor {@override public void settasext (String Text)를 불법 행위를 던진다. if (text == null || text.equals ( "")) {text = "0"; } setValue (double.parsEdouble (텍스트)); } @override public String getTasext () {return getValue (). toString (); }} org.springframework.beans.propertyeditors.propertieseditor; Public Class IntegerEditor는 PropertiesEditor {@override public void settasext (String Text)를 불법 행위를 던진다. (text == null || text.equals ( "")) {text = "0"; } setValue (integer.parseint (텍스트)); } @override public String getTasext () {return getValue (). toString (); }} org.springframework.beans.propertyeditors.propertieseditor; 공개 클래스 부동 소는 propertiesEditor {@override public void settasext (문자열 text)를 불법 행위를 던진다 (text == null || text.equals ( "")) {text = "0"; } setValue (float.parsefloat (text)); } @override public String getTasext () {return getValue (). toString (); }} org.springframework.beans.propertyeditors.propertieseditor; Public Class Longeditor는 PropertiesEditor {@override public void settasext (String Text)를 불법 행위를 던진다. if (text == null || text.equals ( "")) {text = "0"; } setValue (long.parselong (텍스트)); } @override public String getTasext () {return getValue (). toString (); }}베이스 컨트롤러에서
@initbinder protected void initbinder (webdatabinder binder) {binder.registerCustomEditor (date.class, new CustomDateEditor (새 simpledateformat ( "yyyy-mm-dd hh : mm : ss"))); / binder.registerCustomEditor (int.class, new CustomNumberEditor (int.class, true)); binder.registerCustomEditor (int.class, new IntegerEditor ()); / binder.registerCustomEditor (long.class, 새 CustomNumberEditor (long.class, true)); binder.registerCustomEditor (long.class, new longeditor ()); binder.registerCustomEditor (double.class, new doubleeditor ()); binder.registerCustomEditor (float.class, new floateditor ()); } 코드 사본은 다음과 같습니다.
공개 클래스 org.springframework.beans.propertyeditors.propertieseditor actends java.beans.propertyeditorsupport {
보다? 편집기 클래스가 PropertyEditorSupport를 직접 상속하는 경우에도 괜찮습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.