在FacesConverter中使用ManagedBean [英] Use ManagedBean in FacesConverter

查看:131
本文介绍了在FacesConverter中使用ManagedBean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Converter中使用ManagedBean. ManagedBean负责从数据库获取数据.在Converter中,我想将字符串转换为必须从数据库获取的对象.

I want to use ManagedBean in my Converter. The ManagedBean is responsible for getting data from database. In Converter I want to convert string into object which must be get from database.

这是我的转换器

@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语句创建新对象(它更易于测试和维护).有什么建议吗?

I know that galleryContainer will be null and if I want to inject ManagedBean into Converter I can mark it as ManagedBean too. The problem is that I want to do it in beautiful way, I don't want to look for some 'strange solution'. Maybe the problem is in my application? Maybe there is some other good solution to create object which must get data from database and used in converter? I want also to mention that I will prefer to use DependencyInjection instead of creating new object using new statement (it is easier to test and maintain). Any suggestions?

推荐答案

您应该使用@ManagedBean,而不是使用@FacesConverter,因为当前面对转换器不是有效的注入目标.尽管如此,您仍可以选择将转换器作为托管bean,因此在视图中将其称为converter="#{yourConverter}"(按托管bean名称)而不是converter="yourConverter"(按转换器id).

Instead of using @FacesConverter you should use @ManagedBean, because currently faces converter isn't a valid injection target. Nonetheless, you can choose your converter to be a managed bean, thus refer to it in your view as converter="#{yourConverter}" (by managed bean name) instead of converter="yourConverter" (by converter id).

基本用法示例:

@ManagedBean
@RequestScoped
public class YourConverter implements Converter {

    @ManagedProperty...
    ...

    //implementation of converter methods

}

当然,阅读BalusC宝贵的<​​a href="http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ProcessingGETRequestParameters"> JSF 2.0中的通信也为这个问题提供了一些启示.

Of course, reading BalusC's invaluable Communication in JSF 2.0 will shed some light on this question as well.

还值得一提的是,如果不应将转换器bean的范围更改为例如应用程序或会话,则其范围应为

It is also worth mentioning that the scope of your converter bean may be changed to, for example, application or session, if it is not supposed to hold any state.

这篇关于在FacesConverter中使用ManagedBean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆