Java Spring3为整个应用程序注册一次propertyeditor
目前,我正在每个控制器中执行此操作,我需要它:Java Spring3为整个应用程序注册一次propertyeditor,java,spring,spring-mvc,javabeans,Java,Spring,Spring Mvc,Javabeans,目前,我正在每个控制器中执行此操作,我需要它: @InitBinder protected void initBinder(WebDataBinder binder){ binder.registerCustomEditor( Country.class, new CountryPropertyEditor(this.countryService)); } 将我的字符串变量转换为country很好,但是,有没有一种方法可以将其注册到所有控制器中?您可以将其放入控制器的超类
@InitBinder
protected void initBinder(WebDataBinder binder){
binder.registerCustomEditor(
Country.class, new CountryPropertyEditor(this.countryService));
}
将我的字符串变量转换为country很好,但是,有没有一种方法可以将其注册到所有控制器中?您可以将其放入控制器的超类中吗?您可以将其放入控制器的超类中吗?您可以在xml配置文件中声明性地注册属性编辑器,以便在表示层中根据需要使用它: 首先注册用于转换的类:
<bean id="sample" class="youpackages.SophisticatedClass">
<property name="type" value="sophisticatedClass"/>
</bean>
然后在应用程序中全局注册它:
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="youpackages.SophisticatedClass" value="yourpackages.SophisticatedClassEditor"/>
</map>
</property>
</bean>
这样,只要需要与复杂类相关的转换,就会使用属性编辑器,因为它已在应用程序上下文中注册
这样,您就不必让您的模型紧跟需要这种转换的事实
希望有帮助 您可以在xml配置文件中声明注册属性编辑器,以便在表示层中根据需要使用它: 首先注册用于转换的类:
<bean id="sample" class="youpackages.SophisticatedClass">
<property name="type" value="sophisticatedClass"/>
</bean>
然后在应用程序中全局注册它:
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="youpackages.SophisticatedClass" value="yourpackages.SophisticatedClassEditor"/>
</map>
</property>
</bean>
这样,只要需要与复杂类相关的转换,就会使用属性编辑器,因为它已在应用程序上下文中注册
这样,您就不必让您的模型紧跟需要这种转换的事实
希望有帮助 要为整个应用程序注册一次自定义属性编辑器,请在配置类中重写WebMVCConfigureAdapter的addFormatters方法 带有Java配置的日期示例
@EnableWebMvc
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {
// ...
@Override
public void addFormatters( final FormatterRegistry registry ) {
registry.addFormatterForFieldType( java.sql.Date.class, new DateFormatter() );
}
// ...
}
并为日期格式化创建自己的类,该类实现格式化程序接口,如下所示
public final class DateFormatter implements Formatter<java.sql.Date> {
private String pattern;
public DateFormatter() {
this( "yyyy-MM-dd" );
}
public DateFormatter( final String pattern ) {
this.pattern = pattern;
}
public String print( final java.sql.Date date, final Locale locale ) {
if ( date == null ) {
return "";
}
return getDateFormat( locale ).format( date );
}
public java.sql.Date parse( final String formatted, final Locale locale ) throws
ParseException {
if ( formatted.length() == 0 ) {
return null;
}
java.util.Date udDate = getDateFormat( locale ).parse( formatted );
return new java.sql.Date( udDate.getTime() );
}
protected DateFormat getDateFormat( final Locale locale ) {
DateFormat dateFormat = new SimpleDateFormat( this.pattern, locale );
dateFormat.setLenient( false );
return dateFormat;
}
}
就是这样。在您的配置类中为整个应用程序注册一次自定义属性编辑器覆盖WebMVCConfigureAdapter的addFormatters方法 带有Java配置的日期示例
@EnableWebMvc
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {
// ...
@Override
public void addFormatters( final FormatterRegistry registry ) {
registry.addFormatterForFieldType( java.sql.Date.class, new DateFormatter() );
}
// ...
}
并为日期格式化创建自己的类,该类实现格式化程序接口,如下所示
public final class DateFormatter implements Formatter<java.sql.Date> {
private String pattern;
public DateFormatter() {
this( "yyyy-MM-dd" );
}
public DateFormatter( final String pattern ) {
this.pattern = pattern;
}
public String print( final java.sql.Date date, final Locale locale ) {
if ( date == null ) {
return "";
}
return getDateFormat( locale ).format( date );
}
public java.sql.Date parse( final String formatted, final Locale locale ) throws
ParseException {
if ( formatted.length() == 0 ) {
return null;
}
java.util.Date udDate = getDateFormat( locale ).parse( formatted );
return new java.sql.Date( udDate.getTime() );
}
protected DateFormat getDateFormat( final Locale locale ) {
DateFormat dateFormat = new SimpleDateFormat( this.pattern, locale );
dateFormat.setLenient( false );
return dateFormat;
}
}
就是这样。问题是重复的,只有当我为每个问题使用一个countryService时,问题才是重复的。通过超级控制地注入countryService,如果我对每一个都使用一个countryService。通过在超级控制器中注入countryService