用于非持久实体的JSF Converter [英] JSF Converter for non-persisted entity

查看:55
本文介绍了用于非持久实体的JSF Converter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个采用以下方法的JSF转换器:

I have a JSF converter with the following method:

@Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        Actor a = getEjbCasos().getActorByName(value);        
        return a;
    }

这是通过SelectOneMenu调用的:

This is called from a SelectOneMenu:

<h:selectOneMenu id="actores" value="#{crearCasosBean.currentActor}">
   <f:selectItem noSelectionOption="true"/>
   <f:selectItems value="#{crearCasosBean.actorRows}" var="actor" itemLabel="#{actor.name}" itemValue="#{actor}"/>   
   <f:converter binding="#{actorConverter}"/>
</h:selectOneMenu>

用户可以选择新的Actor名称,而不是选择actor,这将创建一个新的Actor实体,但不会将其持久化到数据库中.然后将此Actor名称添加到actorRows列表中,以便下次可以选择它.

Instead of selecting an actor, the user can enter new actor names, which creates a new Actor entity but does not persist it to the database. This Actor name is then added to the actorRows list so that it can be selected next time.

我遇到的问题是,由于用户创建的Actor尚未持久化到数据库中,因此当他们选择此Actor时,转换器将返回null.我不确定如何告诉转换器此引用应该与第一个Actor相同.

The problem I'm having is that since the Actor the user has created has not been persisted to the database, when they select this Actor the converter returns null. I'm not sure how to tell the converter that this reference should be the same as the first Actor.

推荐答案

您可以为此使用 OmniFaces SelectItemsConverter.默认情况下,它通过对象的toString()表示形式直接基于<f:selectItems>中的可用值转换提交的值.因此,如果可以保证可以提供唯一的toString()表示形式,则此转换器应该就可以做到.额外的好处是该转换器完全不会影响数据库.

You could use OmniFaces SelectItemsConverter for this. It converts the submitted value directly based on the available values in <f:selectItems> by default via object's toString() representation. So if you can guarantee that you can provide an unique toString() representation, then this converter should just do it. Additional bonus is that this converter don't hit the DB at all.

在将OmniFaces JAR放到Web应用程序的运行时类路径中之后,这只是一个问题

After dropping OmniFaces JAR in the webapp's runtime classpath, it's just a matter of

<h:selectOneMenu ... converter="omnifaces.SelectItemsConverter">

,没有任何其他配置或代码.另请参见展示示例.

without any additional configuration or code. See also the showcase example.

这篇关于用于非持久实体的JSF Converter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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