OmniFaces - ConverterManager 操作模式
OmniFaces - ConverterManager operating mode
我有一个 JSF 项目,我在其中使用 OmniFaces 在 @FacesConverter
中启用 @EJB
注入。使用 showcase 中的代码,效果很好。
但我想了解更多,表面下发生了什么,我希望在转换器中从 OmniFaces 导入一些内容,但只有 javax.faces.*
的导入
为什么不需要导入 OmniFaces 以及如何告知框架使用 ConverterManager
?是否只是将 OmniFaces 库添加到构建路径?
抱歉,如果这是一个愚蠢的问题。感谢您的任何解释。
转换器实例在通过 Application#createConverter()
创建的幕后。
Converter converter = facesContext.getApplication().createConverter(idOrClass);
喜欢many abstract artifacts in JSF API, the Application
follows the decorator (wrapper) pattern, like as you can find over all place in java.io.*
. You can provide a custom one via a <factory>
in faces-config.xml
as OmniFaces did below in its /META-INF/faces-config.xml
:
<factory>
<application-factory>org.omnifaces.application.OmniApplicationFactory</application-factory>
</factory>
应用程序工厂实现归结为:
public class OmniApplicationFactory extends ApplicationFactory {
private final ApplicationFactory wrapped;
public OmniApplicationFactory(ApplicationFactory wrapped) {
this.wrapped = wrapped;
}
@Override
public Application getApplication() {
return new OmniApplication(wrapped.getApplication());
}
@Override
public void setApplication(Application application) {
wrapped.setApplication(application);
}
@Override
public ApplicationFactory getWrapped() {
return wrapped;
}
}
(actual implementation 更复杂一些,以解决来自旧版本的第 3 方库(如 Seam、Weld 和 Spring WebFlow 的损坏的应用程序工厂(顺便说一句,它们已经长期固定在他们身边)).
OmniApplication
实现归结为:
public class OmniApplication extends ApplicationWrapper {
private final Application wrapped;
private final ConverterManager converterManager;
public OmniApplication(Application wrapped) {
this.wrapped = wrapped;
converterManager = BeanManager.INSTANCE.getReference(ConverterManager.class);
}
@Override
public Converter createConverter(String converterId) {
Converter converter = converterManager.createConverter(getWrapped(), converterId);
return (converter != null) ? converter : super.createConverter(converterId);
}
@Override
public Application getWrapped() {
return wrapped;
}
}
你看,ConverterManager
的委派正在进行中。
我有一个 JSF 项目,我在其中使用 OmniFaces 在 @FacesConverter
中启用 @EJB
注入。使用 showcase 中的代码,效果很好。
但我想了解更多,表面下发生了什么,我希望在转换器中从 OmniFaces 导入一些内容,但只有 javax.faces.*
为什么不需要导入 OmniFaces 以及如何告知框架使用 ConverterManager
?是否只是将 OmniFaces 库添加到构建路径?
抱歉,如果这是一个愚蠢的问题。感谢您的任何解释。
转换器实例在通过 Application#createConverter()
创建的幕后。
Converter converter = facesContext.getApplication().createConverter(idOrClass);
喜欢many abstract artifacts in JSF API, the Application
follows the decorator (wrapper) pattern, like as you can find over all place in java.io.*
. You can provide a custom one via a <factory>
in faces-config.xml
as OmniFaces did below in its /META-INF/faces-config.xml
:
<factory>
<application-factory>org.omnifaces.application.OmniApplicationFactory</application-factory>
</factory>
应用程序工厂实现归结为:
public class OmniApplicationFactory extends ApplicationFactory {
private final ApplicationFactory wrapped;
public OmniApplicationFactory(ApplicationFactory wrapped) {
this.wrapped = wrapped;
}
@Override
public Application getApplication() {
return new OmniApplication(wrapped.getApplication());
}
@Override
public void setApplication(Application application) {
wrapped.setApplication(application);
}
@Override
public ApplicationFactory getWrapped() {
return wrapped;
}
}
(actual implementation 更复杂一些,以解决来自旧版本的第 3 方库(如 Seam、Weld 和 Spring WebFlow 的损坏的应用程序工厂(顺便说一句,它们已经长期固定在他们身边)).
OmniApplication
实现归结为:
public class OmniApplication extends ApplicationWrapper {
private final Application wrapped;
private final ConverterManager converterManager;
public OmniApplication(Application wrapped) {
this.wrapped = wrapped;
converterManager = BeanManager.INSTANCE.getReference(ConverterManager.class);
}
@Override
public Converter createConverter(String converterId) {
Converter converter = converterManager.createConverter(getWrapped(), converterId);
return (converter != null) ? converter : super.createConverter(converterId);
}
@Override
public Application getWrapped() {
return wrapped;
}
}
你看,ConverterManager
的委派正在进行中。