package com.projectr.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
/**
* Shared WebBindingInitializer for custom property editors.
*
* @author aramirez
*
*/
public class CommonBindingInitializer implements WebBindingInitializer {
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, null, true));
binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", request.getLocale());
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
}
}
अपना एप्लिकेशन-प्रसंग या डिस्पैचर-सर्वलेट ऊपर कोड की
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.projectr.web.CommonBindingInitializer"/>
</property>
</bean>
एनोटेशन बराबर में
। ध्यान दें कि @ControllerAdvice
स्प्रिंग 3.2.x
@ControllerAdvice
public class CommonBindingInitializer {
@InitBinder
public void registerCustomEditors(WebDataBinder binder, WebRequest request) {
binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, null, true));
binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", request.getLocale());
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
}
}
में शुरू की है यह देखते हुए कि अपने उदाहरण all_ _at एनोटेशन का उपयोग नहीं करता है मैं नहीं दिख रहा है कि यह कैसे प्रश्न का उत्तर है। – ArtB
मेरी माफ़ी। उपरोक्त xml कोड के समतुल्य एनोटेशन [@ControllerAdvice] है (http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/bind/annotation/ControllerAdvice.html) @InitBinder के साथ। लेकिन मुझे लगता है कि आप पहले से ही मेरे देर के जवाब के कारण पता चला है। – ramirezag
अगर मैंने किया तो याद मत करो। एक उदाहरण को एक अतिरिक्त उत्तर के रूप में पोस्ट करना चाहते हैं और मैं इसे स्वीकार करूंगा? \ – ArtB