【发布时间】:2013-03-13 17:55:35
【问题描述】:
我想在我的Converter 中使用ManagedBean。 ManagedBean 负责从数据库中获取数据。在Converter 中,我想将字符串转换为必须从数据库中获取的对象。
这是我的转换器
@FacesConverter(forClass=Gallery.class, value="galleryConverter")
public class GalleryConverter implements Converter {
// of course this one is null
@ManagedProperty(value="#{galleryContainer}")
private GalleryContainer galleryContainer;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String galleryId) {
return galleryContainer.findGallery(galleryId);
...
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object gallery) {
...
}
}
我知道galleryContainer 将为空,如果我想将ManagedBean 注入Converter,我也可以将其标记为ManagedBean。问题是我想以漂亮的方式来做,我不想寻找一些“奇怪的解决方案”。也许问题出在我的应用程序中?也许还有其他一些好的解决方案来创建必须从数据库获取数据并在转换器中使用的对象?我还想提一下,我更喜欢使用DependencyInjection,而不是使用new 语句创建新对象(它更容易测试和维护)。有什么建议吗?
【问题讨论】: