2011-03-10 6 views
6

मैं सोच रहा था कि क्या स्प्रिंग एमवीसी 3.0 के बाद वैश्विक स्तर पर प्रॉपर्टी एडिटर पंजीकृत करने का कोई तरीका था। प्रलेखन their documentation में वे दिखाते हैं कि प्रति-नियंत्रक आधार पर बीन PropertyEditor को कस्टमाइज़ करने के लिए एनोटेशन का उपयोग कैसे करें, और मुझे लगता है कि यह वैश्विक रूप से ऐसा करने का एक XML तरीका है। तो मैं सोच रहा था, क्या प्रत्येक तरीका के लिए @InitBinder विधि किए बिना सभी नियंत्रकों के लिए PropertyEditors को पंजीकृत करने के लिए केवल एनोटेशन का उपयोग करके एक तरीका है। @InitBinder विधि के साथ एक सामान्य सुपर-क्लास बनाना या तो वांछनीय नहीं है।क्या स्प्रिंग एमवीसी 3.0 में वैश्विक स्तर पर PropertyEditors को पंजीकृत करने का एक एनोटेशन-आधारित तरीका है?

इस विषय पर other question स्प्रिंग 3.0 जारी होने से पहले पूछा गया था।

उत्तर

5
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)); 
    } 
} 
+0

में शुरू की है यह देखते हुए कि अपने उदाहरण all_ _at एनोटेशन का उपयोग नहीं करता है मैं नहीं दिख रहा है कि यह कैसे प्रश्न का उत्तर है। – ArtB

+0

मेरी माफ़ी। उपरोक्त xml कोड के समतुल्य एनोटेशन [@ControllerAdvice] है (http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/bind/annotation/ControllerAdvice.html) @InitBinder के साथ। लेकिन मुझे लगता है कि आप पहले से ही मेरे देर के जवाब के कारण पता चला है। – ramirezag

+0

अगर मैंने किया तो याद मत करो। एक उदाहरण को एक अतिरिक्त उत्तर के रूप में पोस्ट करना चाहते हैं और मैं इसे स्वीकार करूंगा? \ – ArtB

संबंधित मुद्दे