Sle*_*led 6 annotations spring-mvc propertyeditor javabeans
我想知道是否有一种方法可以在Spring MVC 3.0之内全局注册PropertyEditor.在他们的文档文档中,他们展示了如何使用注释来基于每个控制器自定义bean PropertyEditor,并且 - 在我看来 - 就像我在全球范围内这样做的XML方式.所以我想知道,有没有办法,只需使用注释为所有控制器注册PropertyEditors,而无需为每个控制器执行@InitBinder方法.使用@InitBinder方法创建公共超类也是不可取的.
在其他问题上这个问题被问春3.0发布之前.
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));
}
}
Run Code Online (Sandbox Code Playgroud)
在您的应用程序上下文或调度程序-servlet中
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.projectr.web.CommonBindingInitializer"/>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
注释相当于上面的代码.请注意,这@ControllerAdvice是在Spring 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));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1874 次 |
| 最近记录: |